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 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 50 | /* --- Globals ------------------------------------------------------------ |
| 51 | |
| 52 | The globals are initialized by the _PyUnicode_Init() API and should |
| 53 | not be used before calling that API. |
| 54 | |
| 55 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 56 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 57 | |
| 58 | #ifdef __cplusplus |
| 59 | extern "C" { |
| 60 | #endif |
| 61 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 62 | /* Maximum code point of Unicode 6.0: 0x10ffff (1,114,111) */ |
| 63 | #define MAX_UNICODE 0x10ffff |
| 64 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 65 | #ifdef Py_DEBUG |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 66 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op, 0) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 67 | #else |
| 68 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 69 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 70 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 71 | #define _PyUnicode_UTF8(op) \ |
| 72 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 73 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 74 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 75 | assert(PyUnicode_IS_READY(op)), \ |
| 76 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 77 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 78 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 79 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 80 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 81 | #define PyUnicode_UTF8_LENGTH(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 | ((PyASCIIObject*)(op))->length : \ |
| 86 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 87 | #define _PyUnicode_WSTR(op) \ |
| 88 | (((PyASCIIObject*)(op))->wstr) |
| 89 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 90 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 91 | #define _PyUnicode_LENGTH(op) \ |
| 92 | (((PyASCIIObject *)(op))->length) |
| 93 | #define _PyUnicode_STATE(op) \ |
| 94 | (((PyASCIIObject *)(op))->state) |
| 95 | #define _PyUnicode_HASH(op) \ |
| 96 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 97 | #define _PyUnicode_KIND(op) \ |
| 98 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 99 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 100 | #define _PyUnicode_GET_LENGTH(op) \ |
| 101 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 102 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 103 | #define _PyUnicode_DATA_ANY(op) \ |
| 104 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 105 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 106 | /* Optimized version of Py_MAX() to compute the maximum character: |
| 107 | use it when your are computing the second argument of PyUnicode_New() */ |
| 108 | #define MAX_MAXCHAR(maxchar1, maxchar2) \ |
| 109 | ((maxchar1) | (maxchar2)) |
| 110 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 111 | #undef PyUnicode_READY |
| 112 | #define PyUnicode_READY(op) \ |
| 113 | (assert(_PyUnicode_CHECK(op)), \ |
| 114 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 115 | 0 : \ |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 116 | _PyUnicode_Ready(op))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 117 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 118 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 119 | (assert(_PyUnicode_CHECK(op)), \ |
| 120 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 121 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 122 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 123 | (assert(_PyUnicode_CHECK(op)), \ |
| 124 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 125 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 126 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 127 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 128 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 129 | (assert(_PyUnicode_CHECK(op)), \ |
| 130 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 131 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 132 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 133 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 134 | /* true if the Unicode object has an allocated wstr memory block |
| 135 | (not shared with other data) */ |
| 136 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 137 | (assert(_PyUnicode_CHECK(op)), \ |
| 138 | (_PyUnicode_WSTR(op) && \ |
| 139 | (!PyUnicode_IS_READY(op) || \ |
| 140 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 141 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 142 | /* Generic helper macro to convert characters of different types. |
| 143 | from_type and to_type have to be valid type names, begin and end |
| 144 | are pointers to the source characters which should be of type |
| 145 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 146 | buffer where the result characters are written to. */ |
| 147 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 148 | do { \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 149 | to_type *_to = (to_type *) to; \ |
| 150 | const from_type *_iter = (begin); \ |
| 151 | const from_type *_end = (end); \ |
| 152 | Py_ssize_t n = (_end) - (_iter); \ |
| 153 | const from_type *_unrolled_end = \ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 154 | _iter + _Py_SIZE_ROUND_DOWN(n, 4); \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 155 | while (_iter < (_unrolled_end)) { \ |
| 156 | _to[0] = (to_type) _iter[0]; \ |
| 157 | _to[1] = (to_type) _iter[1]; \ |
| 158 | _to[2] = (to_type) _iter[2]; \ |
| 159 | _to[3] = (to_type) _iter[3]; \ |
| 160 | _iter += 4; _to += 4; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 161 | } \ |
Antoine Pitrou | e459a08 | 2011-10-11 20:58:41 +0200 | [diff] [blame] | 162 | while (_iter < (_end)) \ |
| 163 | *_to++ = (to_type) *_iter++; \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 164 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 165 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 166 | /* This dictionary holds all interned unicode strings. Note that references |
| 167 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 168 | When the interned string reaches a refcnt of 0 the string deallocation |
| 169 | function will delete the reference from this dictionary. |
| 170 | |
| 171 | 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] | 172 | 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] | 173 | */ |
| 174 | static PyObject *interned; |
| 175 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 176 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 177 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 178 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 179 | /* List of static strings. */ |
| 180 | static _Py_Identifier *static_strings; |
| 181 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 182 | /* Single character Unicode strings in the Latin-1 range are being |
| 183 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 184 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 185 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 186 | /* Fast detection of the most frequent whitespace characters */ |
| 187 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 188 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 189 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 190 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 191 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 192 | /* case 0x000C: * FORM FEED */ |
| 193 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 194 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 195 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 196 | /* case 0x001C: * FILE SEPARATOR */ |
| 197 | /* case 0x001D: * GROUP SEPARATOR */ |
| 198 | /* case 0x001E: * RECORD SEPARATOR */ |
| 199 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 200 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 201 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 202 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 203 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 204 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 205 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 206 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 207 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 208 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 209 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 210 | 0, 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, |
| 214 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 217 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 218 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 219 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 220 | static int unicode_modifiable(PyObject *unicode); |
| 221 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 222 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 223 | static PyObject * |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 224 | _PyUnicode_FromUCS1(const unsigned char *s, Py_ssize_t size); |
| 225 | static PyObject * |
| 226 | _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); |
| 227 | static PyObject * |
| 228 | _PyUnicode_FromUCS4(const Py_UCS4 *s, Py_ssize_t size); |
| 229 | |
| 230 | static PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 231 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 232 | PyObject **errorHandler,const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 233 | PyObject *unicode, PyObject **exceptionObject, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 234 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 235 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 236 | static void |
| 237 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 238 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 239 | PyObject *unicode, |
| 240 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 241 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 242 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 243 | /* Same for linebreaks */ |
| 244 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 245 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 246 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 247 | /* 0x000B, * LINE TABULATION */ |
| 248 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 249 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 250 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 251 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 252 | /* 0x001C, * FILE SEPARATOR */ |
| 253 | /* 0x001D, * GROUP SEPARATOR */ |
| 254 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 255 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 256 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 257 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 258 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 259 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 260 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 261 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 262 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 263 | 0, 0, 0, 0, 0, 0, 0, 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, |
| 268 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 271 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 272 | 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] | 273 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 274 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 275 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 276 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 277 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 278 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 279 | /* This is actually an illegal character, so it should |
| 280 | not be passed to unichr. */ |
| 281 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 282 | #endif |
| 283 | } |
| 284 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 285 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 286 | int |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 287 | _PyUnicode_CheckConsistency(PyObject *op, int check_content) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 288 | { |
| 289 | PyASCIIObject *ascii; |
| 290 | unsigned int kind; |
| 291 | |
| 292 | assert(PyUnicode_Check(op)); |
| 293 | |
| 294 | ascii = (PyASCIIObject *)op; |
| 295 | kind = ascii->state.kind; |
| 296 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 297 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 298 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 299 | assert(ascii->state.ready == 1); |
| 300 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 301 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 302 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 303 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 304 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 305 | if (ascii->state.compact == 1) { |
| 306 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 307 | assert(kind == PyUnicode_1BYTE_KIND |
| 308 | || kind == PyUnicode_2BYTE_KIND |
| 309 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 310 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 311 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 312 | assert (compact->utf8 != data); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 313 | } |
| 314 | else { |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 315 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 316 | |
| 317 | data = unicode->data.any; |
| 318 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 319 | assert(ascii->length == 0); |
| 320 | assert(ascii->hash == -1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 321 | assert(ascii->state.compact == 0); |
| 322 | assert(ascii->state.ascii == 0); |
| 323 | assert(ascii->state.ready == 0); |
Victor Stinner | e30c0a1 | 2011-11-04 20:54:05 +0100 | [diff] [blame] | 324 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 325 | assert(ascii->wstr != NULL); |
| 326 | assert(data == NULL); |
| 327 | assert(compact->utf8 == NULL); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 328 | } |
| 329 | else { |
| 330 | assert(kind == PyUnicode_1BYTE_KIND |
| 331 | || kind == PyUnicode_2BYTE_KIND |
| 332 | || kind == PyUnicode_4BYTE_KIND); |
| 333 | assert(ascii->state.compact == 0); |
| 334 | assert(ascii->state.ready == 1); |
| 335 | assert(data != NULL); |
| 336 | if (ascii->state.ascii) { |
| 337 | assert (compact->utf8 == data); |
| 338 | assert (compact->utf8_length == ascii->length); |
| 339 | } |
| 340 | else |
| 341 | assert (compact->utf8 != data); |
| 342 | } |
| 343 | } |
| 344 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 345 | if ( |
| 346 | #if SIZEOF_WCHAR_T == 2 |
| 347 | kind == PyUnicode_2BYTE_KIND |
| 348 | #else |
| 349 | kind == PyUnicode_4BYTE_KIND |
| 350 | #endif |
| 351 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 352 | { |
| 353 | assert(ascii->wstr == data); |
| 354 | assert(compact->wstr_length == ascii->length); |
| 355 | } else |
| 356 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 357 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 358 | |
| 359 | if (compact->utf8 == NULL) |
| 360 | assert(compact->utf8_length == 0); |
| 361 | if (ascii->wstr == NULL) |
| 362 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 363 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 364 | /* check that the best kind is used */ |
| 365 | if (check_content && kind != PyUnicode_WCHAR_KIND) |
| 366 | { |
| 367 | Py_ssize_t i; |
| 368 | Py_UCS4 maxchar = 0; |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 369 | void *data; |
| 370 | Py_UCS4 ch; |
| 371 | |
| 372 | data = PyUnicode_DATA(ascii); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 373 | for (i=0; i < ascii->length; i++) |
| 374 | { |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 375 | ch = PyUnicode_READ(kind, data, i); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 376 | if (ch > maxchar) |
| 377 | maxchar = ch; |
| 378 | } |
| 379 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 380 | if (ascii->state.ascii == 0) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 381 | assert(maxchar >= 128); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 382 | assert(maxchar <= 255); |
| 383 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 384 | else |
| 385 | assert(maxchar < 128); |
| 386 | } |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 387 | else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 388 | assert(maxchar >= 0x100); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 389 | assert(maxchar <= 0xFFFF); |
| 390 | } |
| 391 | else { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 392 | assert(maxchar >= 0x10000); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 393 | assert(maxchar <= MAX_UNICODE); |
Victor Stinner | 77faf69 | 2011-11-20 18:56:05 +0100 | [diff] [blame] | 394 | } |
Victor Stinner | 718fbf0 | 2012-04-26 00:39:37 +0200 | [diff] [blame] | 395 | assert(PyUnicode_READ(kind, data, ascii->length) == 0); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 396 | } |
Benjamin Peterson | ccc51c1 | 2011-10-03 19:34:12 -0400 | [diff] [blame] | 397 | return 1; |
| 398 | } |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 399 | #endif |
| 400 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 401 | static PyObject* |
| 402 | unicode_result_wchar(PyObject *unicode) |
| 403 | { |
| 404 | #ifndef Py_DEBUG |
| 405 | Py_ssize_t len; |
| 406 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 407 | len = _PyUnicode_WSTR_LENGTH(unicode); |
| 408 | if (len == 0) { |
| 409 | Py_INCREF(unicode_empty); |
| 410 | Py_DECREF(unicode); |
| 411 | return unicode_empty; |
| 412 | } |
| 413 | |
| 414 | if (len == 1) { |
| 415 | wchar_t ch = _PyUnicode_WSTR(unicode)[0]; |
| 416 | if (ch < 256) { |
| 417 | PyObject *latin1_char = get_latin1_char((unsigned char)ch); |
| 418 | Py_DECREF(unicode); |
| 419 | return latin1_char; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | if (_PyUnicode_Ready(unicode) < 0) { |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 424 | Py_DECREF(unicode); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 425 | return NULL; |
| 426 | } |
| 427 | #else |
Victor Stinner | aa77127 | 2012-10-04 02:32:58 +0200 | [diff] [blame] | 428 | assert(Py_REFCNT(unicode) == 1); |
| 429 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 430 | /* don't make the result ready in debug mode to ensure that the caller |
| 431 | makes the string ready before using it */ |
| 432 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 433 | #endif |
| 434 | return unicode; |
| 435 | } |
| 436 | |
| 437 | static PyObject* |
| 438 | unicode_result_ready(PyObject *unicode) |
| 439 | { |
| 440 | Py_ssize_t length; |
| 441 | |
| 442 | length = PyUnicode_GET_LENGTH(unicode); |
| 443 | if (length == 0) { |
| 444 | if (unicode != unicode_empty) { |
| 445 | Py_INCREF(unicode_empty); |
| 446 | Py_DECREF(unicode); |
| 447 | } |
| 448 | return unicode_empty; |
| 449 | } |
| 450 | |
| 451 | if (length == 1) { |
| 452 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 453 | if (ch < 256) { |
| 454 | PyObject *latin1_char = unicode_latin1[ch]; |
| 455 | if (latin1_char != NULL) { |
| 456 | if (unicode != latin1_char) { |
| 457 | Py_INCREF(latin1_char); |
| 458 | Py_DECREF(unicode); |
| 459 | } |
| 460 | return latin1_char; |
| 461 | } |
| 462 | else { |
| 463 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 464 | Py_INCREF(unicode); |
| 465 | unicode_latin1[ch] = unicode; |
| 466 | return unicode; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 472 | return unicode; |
| 473 | } |
| 474 | |
| 475 | static PyObject* |
| 476 | unicode_result(PyObject *unicode) |
| 477 | { |
| 478 | assert(_PyUnicode_CHECK(unicode)); |
| 479 | if (PyUnicode_IS_READY(unicode)) |
| 480 | return unicode_result_ready(unicode); |
| 481 | else |
| 482 | return unicode_result_wchar(unicode); |
| 483 | } |
| 484 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 485 | static PyObject* |
| 486 | unicode_result_unchanged(PyObject *unicode) |
| 487 | { |
| 488 | if (PyUnicode_CheckExact(unicode)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 489 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 490 | return NULL; |
| 491 | Py_INCREF(unicode); |
| 492 | return unicode; |
| 493 | } |
| 494 | else |
| 495 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 496 | return _PyUnicode_Copy(unicode); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 499 | #ifdef HAVE_MBCS |
| 500 | static OSVERSIONINFOEX winver; |
| 501 | #endif |
| 502 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 503 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 504 | |
| 505 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 506 | to keep things simple, we use a single bitmask, using the least 5 |
| 507 | bits from each unicode characters as the bit index. */ |
| 508 | |
| 509 | /* the linebreak mask is set up by Unicode_Init below */ |
| 510 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 511 | #if LONG_BIT >= 128 |
| 512 | #define BLOOM_WIDTH 128 |
| 513 | #elif LONG_BIT >= 64 |
| 514 | #define BLOOM_WIDTH 64 |
| 515 | #elif LONG_BIT >= 32 |
| 516 | #define BLOOM_WIDTH 32 |
| 517 | #else |
| 518 | #error "LONG_BIT is smaller than 32" |
| 519 | #endif |
| 520 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 521 | #define BLOOM_MASK unsigned long |
| 522 | |
| 523 | static BLOOM_MASK bloom_linebreak; |
| 524 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 525 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 526 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 527 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 528 | #define BLOOM_LINEBREAK(ch) \ |
| 529 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 530 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 531 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 532 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 533 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 534 | { |
| 535 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 536 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 537 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 538 | Py_ssize_t i; |
| 539 | |
| 540 | mask = 0; |
| 541 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 542 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 543 | |
| 544 | return mask; |
| 545 | } |
| 546 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 547 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 548 | (BLOOM(mask, chr) \ |
| 549 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 550 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 551 | /* Compilation of templated routines */ |
| 552 | |
| 553 | #include "stringlib/asciilib.h" |
| 554 | #include "stringlib/fastsearch.h" |
| 555 | #include "stringlib/partition.h" |
| 556 | #include "stringlib/split.h" |
| 557 | #include "stringlib/count.h" |
| 558 | #include "stringlib/find.h" |
| 559 | #include "stringlib/find_max_char.h" |
| 560 | #include "stringlib/localeutil.h" |
| 561 | #include "stringlib/undef.h" |
| 562 | |
| 563 | #include "stringlib/ucs1lib.h" |
| 564 | #include "stringlib/fastsearch.h" |
| 565 | #include "stringlib/partition.h" |
| 566 | #include "stringlib/split.h" |
| 567 | #include "stringlib/count.h" |
| 568 | #include "stringlib/find.h" |
| 569 | #include "stringlib/find_max_char.h" |
| 570 | #include "stringlib/localeutil.h" |
| 571 | #include "stringlib/undef.h" |
| 572 | |
| 573 | #include "stringlib/ucs2lib.h" |
| 574 | #include "stringlib/fastsearch.h" |
| 575 | #include "stringlib/partition.h" |
| 576 | #include "stringlib/split.h" |
| 577 | #include "stringlib/count.h" |
| 578 | #include "stringlib/find.h" |
| 579 | #include "stringlib/find_max_char.h" |
| 580 | #include "stringlib/localeutil.h" |
| 581 | #include "stringlib/undef.h" |
| 582 | |
| 583 | #include "stringlib/ucs4lib.h" |
| 584 | #include "stringlib/fastsearch.h" |
| 585 | #include "stringlib/partition.h" |
| 586 | #include "stringlib/split.h" |
| 587 | #include "stringlib/count.h" |
| 588 | #include "stringlib/find.h" |
| 589 | #include "stringlib/find_max_char.h" |
| 590 | #include "stringlib/localeutil.h" |
| 591 | #include "stringlib/undef.h" |
| 592 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 593 | #include "stringlib/unicodedefs.h" |
| 594 | #include "stringlib/fastsearch.h" |
| 595 | #include "stringlib/count.h" |
| 596 | #include "stringlib/find.h" |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 597 | #include "stringlib/undef.h" |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 599 | /* --- Unicode Object ----------------------------------------------------- */ |
| 600 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 601 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 602 | fixup(PyObject *self, Py_UCS4 (*fixfct)(PyObject *s)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 603 | |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 604 | Py_LOCAL_INLINE(Py_ssize_t) findchar(void *s, int kind, |
| 605 | Py_ssize_t size, Py_UCS4 ch, |
| 606 | int direction) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 607 | { |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 608 | int mode = (direction == 1) ? FAST_SEARCH : FAST_RSEARCH; |
| 609 | |
| 610 | switch (kind) { |
| 611 | case PyUnicode_1BYTE_KIND: |
| 612 | { |
| 613 | Py_UCS1 ch1 = (Py_UCS1) ch; |
| 614 | if (ch1 == ch) |
| 615 | return ucs1lib_fastsearch((Py_UCS1 *) s, size, &ch1, 1, 0, mode); |
| 616 | else |
| 617 | return -1; |
| 618 | } |
| 619 | case PyUnicode_2BYTE_KIND: |
| 620 | { |
| 621 | Py_UCS2 ch2 = (Py_UCS2) ch; |
| 622 | if (ch2 == ch) |
| 623 | return ucs2lib_fastsearch((Py_UCS2 *) s, size, &ch2, 1, 0, mode); |
| 624 | else |
| 625 | return -1; |
| 626 | } |
| 627 | case PyUnicode_4BYTE_KIND: |
| 628 | return ucs4lib_fastsearch((Py_UCS4 *) s, size, &ch, 1, 0, mode); |
| 629 | default: |
| 630 | assert(0); |
| 631 | return -1; |
Victor Stinner | 9e7a1bc | 2011-10-13 00:18:12 +0200 | [diff] [blame] | 632 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 633 | } |
| 634 | |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 635 | #ifdef Py_DEBUG |
| 636 | /* Fill the data of an Unicode string with invalid characters to detect bugs |
| 637 | earlier. |
| 638 | |
| 639 | _PyUnicode_CheckConsistency(str, 1) detects invalid characters, at least for |
| 640 | ASCII and UCS-4 strings. U+00FF is invalid in ASCII and U+FFFFFFFF is an |
| 641 | invalid character in Unicode 6.0. */ |
| 642 | static void |
| 643 | unicode_fill_invalid(PyObject *unicode, Py_ssize_t old_length) |
| 644 | { |
| 645 | int kind = PyUnicode_KIND(unicode); |
| 646 | Py_UCS1 *data = PyUnicode_1BYTE_DATA(unicode); |
| 647 | Py_ssize_t length = _PyUnicode_LENGTH(unicode); |
| 648 | if (length <= old_length) |
| 649 | return; |
| 650 | memset(data + old_length * kind, 0xff, (length - old_length) * kind); |
| 651 | } |
| 652 | #endif |
| 653 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 654 | static PyObject* |
| 655 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 656 | { |
| 657 | Py_ssize_t char_size; |
| 658 | Py_ssize_t struct_size; |
| 659 | Py_ssize_t new_size; |
| 660 | int share_wstr; |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 661 | PyObject *new_unicode; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 662 | #ifdef Py_DEBUG |
| 663 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 664 | #endif |
| 665 | |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 666 | assert(unicode_modifiable(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 667 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 668 | assert(PyUnicode_IS_COMPACT(unicode)); |
| 669 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 670 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 671 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 672 | struct_size = sizeof(PyASCIIObject); |
| 673 | else |
| 674 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 675 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 676 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 677 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 678 | PyErr_NoMemory(); |
| 679 | return NULL; |
| 680 | } |
| 681 | new_size = (struct_size + (length + 1) * char_size); |
| 682 | |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 683 | _Py_DEC_REFTOTAL; |
| 684 | _Py_ForgetReference(unicode); |
| 685 | |
| 686 | new_unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 687 | if (new_unicode == NULL) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 688 | _Py_NewReference(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 689 | PyErr_NoMemory(); |
| 690 | return NULL; |
| 691 | } |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 692 | unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 693 | _Py_NewReference(unicode); |
Victor Stinner | 84def37 | 2011-12-11 20:04:56 +0100 | [diff] [blame] | 694 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 695 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 696 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 697 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 698 | if (!PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 699 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 700 | } |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 701 | #ifdef Py_DEBUG |
| 702 | unicode_fill_invalid(unicode, old_length); |
| 703 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 704 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 705 | length, 0); |
Victor Stinner | 7989157 | 2012-05-03 13:43:07 +0200 | [diff] [blame] | 706 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 707 | return unicode; |
| 708 | } |
| 709 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 710 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 711 | resize_inplace(PyObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 712 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 713 | wchar_t *wstr; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 714 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 715 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 716 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 717 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 718 | if (PyUnicode_IS_READY(unicode)) { |
| 719 | Py_ssize_t char_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 720 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 721 | void *data; |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 722 | #ifdef Py_DEBUG |
| 723 | Py_ssize_t old_length = _PyUnicode_LENGTH(unicode); |
| 724 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 725 | |
| 726 | data = _PyUnicode_DATA_ANY(unicode); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 727 | char_size = PyUnicode_KIND(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 728 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 729 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 730 | |
| 731 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 732 | PyErr_NoMemory(); |
| 733 | return -1; |
| 734 | } |
| 735 | new_size = (length + 1) * char_size; |
| 736 | |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 737 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 738 | { |
| 739 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 740 | _PyUnicode_UTF8(unicode) = NULL; |
| 741 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 742 | } |
| 743 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 744 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 745 | if (data == NULL) { |
| 746 | PyErr_NoMemory(); |
| 747 | return -1; |
| 748 | } |
| 749 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 750 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 751 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 752 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 753 | } |
| 754 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 755 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 756 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 757 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 758 | _PyUnicode_LENGTH(unicode) = length; |
| 759 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 760 | #ifdef Py_DEBUG |
| 761 | unicode_fill_invalid(unicode, old_length); |
| 762 | #endif |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 763 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 764 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 765 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 766 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 767 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 768 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 769 | |
| 770 | /* check for integer overflow */ |
| 771 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 772 | PyErr_NoMemory(); |
| 773 | return -1; |
| 774 | } |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 775 | new_size = sizeof(wchar_t) * (length + 1); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 776 | wstr = _PyUnicode_WSTR(unicode); |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 777 | wstr = PyObject_REALLOC(wstr, new_size); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 778 | if (!wstr) { |
| 779 | PyErr_NoMemory(); |
| 780 | return -1; |
| 781 | } |
| 782 | _PyUnicode_WSTR(unicode) = wstr; |
| 783 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 784 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 785 | assert(_PyUnicode_CheckConsistency(unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 786 | return 0; |
| 787 | } |
| 788 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 789 | static PyObject* |
| 790 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 791 | { |
| 792 | Py_ssize_t copy_length; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 793 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 794 | PyObject *copy; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 795 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 796 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 797 | return NULL; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 798 | |
| 799 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 800 | if (copy == NULL) |
| 801 | return NULL; |
| 802 | |
| 803 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 804 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 805 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 806 | } |
| 807 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 808 | PyObject *w; |
Victor Stinner | 7a9105a | 2011-12-12 00:13:42 +0100 | [diff] [blame] | 809 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 810 | w = (PyObject*)_PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 811 | if (w == NULL) |
| 812 | return NULL; |
| 813 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 814 | copy_length = Py_MIN(copy_length, length); |
Victor Stinner | c6cf1ba | 2012-10-23 02:54:47 +0200 | [diff] [blame] | 815 | Py_MEMCPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 816 | copy_length * sizeof(wchar_t)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 817 | return w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 821 | /* 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] | 822 | Ux0000 terminated; some code (e.g. new_identifier) |
| 823 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 824 | |
| 825 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 826 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 827 | |
| 828 | */ |
| 829 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 830 | static PyUnicodeObject * |
| 831 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 832 | { |
| 833 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 834 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 835 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 836 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 837 | if (length == 0 && unicode_empty != NULL) { |
| 838 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 839 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 842 | /* Ensure we won't overflow the size. */ |
| 843 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 844 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 845 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 846 | if (length < 0) { |
| 847 | PyErr_SetString(PyExc_SystemError, |
| 848 | "Negative size passed to _PyUnicode_New"); |
| 849 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 852 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 853 | if (unicode == NULL) |
| 854 | return NULL; |
| 855 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 856 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 857 | if (!_PyUnicode_WSTR(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 858 | Py_DECREF(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 859 | PyErr_NoMemory(); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 860 | return NULL; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 861 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 862 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 863 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 864 | * the caller fails before initializing str -- unicode_resize() |
| 865 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 866 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 867 | * We don't want unicode_resize to read uninitialized memory in |
| 868 | * that case. |
| 869 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 870 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 871 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 872 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 873 | _PyUnicode_HASH(unicode) = -1; |
| 874 | _PyUnicode_STATE(unicode).interned = 0; |
| 875 | _PyUnicode_STATE(unicode).kind = 0; |
| 876 | _PyUnicode_STATE(unicode).compact = 0; |
| 877 | _PyUnicode_STATE(unicode).ready = 0; |
| 878 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 879 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 880 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 881 | _PyUnicode_UTF8(unicode) = NULL; |
| 882 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 883 | assert(_PyUnicode_CheckConsistency((PyObject *)unicode, 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 884 | return unicode; |
| 885 | } |
| 886 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 887 | static const char* |
| 888 | unicode_kind_name(PyObject *unicode) |
| 889 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 890 | /* don't check consistency: unicode_kind_name() is called from |
| 891 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 892 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 893 | { |
| 894 | if (!PyUnicode_IS_READY(unicode)) |
| 895 | return "wstr"; |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 896 | switch (PyUnicode_KIND(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 897 | { |
| 898 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 899 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 900 | return "legacy ascii"; |
| 901 | else |
| 902 | return "legacy latin1"; |
| 903 | case PyUnicode_2BYTE_KIND: |
| 904 | return "legacy UCS2"; |
| 905 | case PyUnicode_4BYTE_KIND: |
| 906 | return "legacy UCS4"; |
| 907 | default: |
| 908 | return "<legacy invalid kind>"; |
| 909 | } |
| 910 | } |
| 911 | assert(PyUnicode_IS_READY(unicode)); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 912 | switch (PyUnicode_KIND(unicode)) { |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 913 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 914 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 915 | return "ascii"; |
| 916 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 917 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 918 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 919 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 920 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 921 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 922 | default: |
| 923 | return "<invalid compact kind>"; |
| 924 | } |
| 925 | } |
| 926 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 927 | #ifdef Py_DEBUG |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 928 | /* Functions wrapping macros for use in debugger */ |
| 929 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 930 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | void *_PyUnicode_compact_data(void *unicode) { |
| 934 | return _PyUnicode_COMPACT_DATA(unicode); |
| 935 | } |
| 936 | void *_PyUnicode_data(void *unicode){ |
| 937 | printf("obj %p\n", unicode); |
| 938 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 939 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 940 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 941 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 942 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 943 | return PyUnicode_DATA(unicode); |
| 944 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 945 | |
| 946 | void |
| 947 | _PyUnicode_Dump(PyObject *op) |
| 948 | { |
| 949 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 950 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 951 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 952 | void *data; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 953 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 954 | if (ascii->state.compact) |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 955 | { |
| 956 | if (ascii->state.ascii) |
| 957 | data = (ascii + 1); |
| 958 | else |
| 959 | data = (compact + 1); |
| 960 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 961 | else |
| 962 | data = unicode->data.any; |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 963 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 964 | |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 965 | if (ascii->wstr == data) |
| 966 | printf("shared "); |
| 967 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | 0d60e87 | 2011-10-23 19:47:19 +0200 | [diff] [blame] | 968 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 969 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 970 | printf(" (%zu), ", compact->wstr_length); |
| 971 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 972 | printf("shared "); |
| 973 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 974 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 975 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 976 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 977 | #endif |
| 978 | |
| 979 | PyObject * |
| 980 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 981 | { |
| 982 | PyObject *obj; |
| 983 | PyCompactUnicodeObject *unicode; |
| 984 | void *data; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 985 | enum PyUnicode_Kind kind; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 986 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 987 | Py_ssize_t char_size; |
| 988 | Py_ssize_t struct_size; |
| 989 | |
| 990 | /* Optimization for empty strings */ |
| 991 | if (size == 0 && unicode_empty != NULL) { |
| 992 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 993 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 994 | } |
| 995 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 996 | is_ascii = 0; |
| 997 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 998 | struct_size = sizeof(PyCompactUnicodeObject); |
| 999 | if (maxchar < 128) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1000 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1001 | char_size = 1; |
| 1002 | is_ascii = 1; |
| 1003 | struct_size = sizeof(PyASCIIObject); |
| 1004 | } |
| 1005 | else if (maxchar < 256) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1006 | kind = PyUnicode_1BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1007 | char_size = 1; |
| 1008 | } |
| 1009 | else if (maxchar < 65536) { |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1010 | kind = PyUnicode_2BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1011 | char_size = 2; |
| 1012 | if (sizeof(wchar_t) == 2) |
| 1013 | is_sharing = 1; |
| 1014 | } |
| 1015 | else { |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 1016 | if (maxchar > MAX_UNICODE) { |
| 1017 | PyErr_SetString(PyExc_SystemError, |
| 1018 | "invalid maximum character passed to PyUnicode_New"); |
| 1019 | return NULL; |
| 1020 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1021 | kind = PyUnicode_4BYTE_KIND; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1022 | char_size = 4; |
| 1023 | if (sizeof(wchar_t) == 4) |
| 1024 | is_sharing = 1; |
| 1025 | } |
| 1026 | |
| 1027 | /* Ensure we won't overflow the size. */ |
| 1028 | if (size < 0) { |
| 1029 | PyErr_SetString(PyExc_SystemError, |
| 1030 | "Negative size passed to PyUnicode_New"); |
| 1031 | return NULL; |
| 1032 | } |
| 1033 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 1034 | return PyErr_NoMemory(); |
| 1035 | |
| 1036 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 1037 | * PyObject_New() so we are able to allocate space for the object and |
| 1038 | * it's data buffer. |
| 1039 | */ |
| 1040 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 1041 | if (obj == NULL) |
| 1042 | return PyErr_NoMemory(); |
| 1043 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 1044 | if (obj == NULL) |
| 1045 | return NULL; |
| 1046 | |
| 1047 | unicode = (PyCompactUnicodeObject *)obj; |
| 1048 | if (is_ascii) |
| 1049 | data = ((PyASCIIObject*)obj) + 1; |
| 1050 | else |
| 1051 | data = unicode + 1; |
| 1052 | _PyUnicode_LENGTH(unicode) = size; |
| 1053 | _PyUnicode_HASH(unicode) = -1; |
| 1054 | _PyUnicode_STATE(unicode).interned = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1055 | _PyUnicode_STATE(unicode).kind = kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1056 | _PyUnicode_STATE(unicode).compact = 1; |
| 1057 | _PyUnicode_STATE(unicode).ready = 1; |
| 1058 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 1059 | if (is_ascii) { |
| 1060 | ((char*)data)[size] = 0; |
| 1061 | _PyUnicode_WSTR(unicode) = NULL; |
| 1062 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1063 | else if (kind == PyUnicode_1BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1064 | ((char*)data)[size] = 0; |
| 1065 | _PyUnicode_WSTR(unicode) = NULL; |
| 1066 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1067 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1068 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1069 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1070 | else { |
| 1071 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 1072 | unicode->utf8_length = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1073 | if (kind == PyUnicode_2BYTE_KIND) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1074 | ((Py_UCS2*)data)[size] = 0; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1075 | else /* kind == PyUnicode_4BYTE_KIND */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1076 | ((Py_UCS4*)data)[size] = 0; |
| 1077 | if (is_sharing) { |
| 1078 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 1079 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 1080 | } |
| 1081 | else { |
| 1082 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1083 | _PyUnicode_WSTR(unicode) = NULL; |
| 1084 | } |
| 1085 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1086 | #ifdef Py_DEBUG |
Victor Stinner | afffce4 | 2012-10-03 23:03:17 +0200 | [diff] [blame] | 1087 | unicode_fill_invalid((PyObject*)unicode, 0); |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1088 | #endif |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1089 | assert(_PyUnicode_CheckConsistency((PyObject*)unicode, 0)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1090 | return obj; |
| 1091 | } |
| 1092 | |
| 1093 | #if SIZEOF_WCHAR_T == 2 |
| 1094 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 1095 | will decode surrogate pairs, the other conversions are implemented as macros |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1096 | for efficiency. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1097 | |
| 1098 | This function assumes that unicode can hold one more code point than wstr |
| 1099 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1100 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1101 | 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] | 1102 | PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1103 | { |
| 1104 | const wchar_t *iter; |
| 1105 | Py_UCS4 *ucs4_out; |
| 1106 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1107 | assert(unicode != NULL); |
| 1108 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1109 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 1110 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 1111 | |
| 1112 | for (iter = begin; iter < end; ) { |
| 1113 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 1114 | _PyUnicode_GET_LENGTH(unicode))); |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1115 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1116 | && (iter+1) < end |
| 1117 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1118 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 1119 | *ucs4_out++ = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1120 | iter += 2; |
| 1121 | } |
| 1122 | else { |
| 1123 | *ucs4_out++ = *iter; |
| 1124 | iter++; |
| 1125 | } |
| 1126 | } |
| 1127 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 1128 | _PyUnicode_GET_LENGTH(unicode))); |
| 1129 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1130 | } |
| 1131 | #endif |
| 1132 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1133 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1134 | unicode_check_modifiable(PyObject *unicode) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1135 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1136 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1137 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1138 | "Cannot modify a string currently used"); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1139 | return -1; |
| 1140 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 1141 | return 0; |
| 1142 | } |
| 1143 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1144 | static int |
| 1145 | _copy_characters(PyObject *to, Py_ssize_t to_start, |
| 1146 | PyObject *from, Py_ssize_t from_start, |
| 1147 | Py_ssize_t how_many, int check_maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1148 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1149 | unsigned int from_kind, to_kind; |
| 1150 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1151 | |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1152 | assert(0 <= how_many); |
| 1153 | assert(0 <= from_start); |
| 1154 | assert(0 <= to_start); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1155 | assert(PyUnicode_Check(from)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1156 | assert(PyUnicode_IS_READY(from)); |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 1157 | assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1158 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1159 | assert(PyUnicode_Check(to)); |
| 1160 | assert(PyUnicode_IS_READY(to)); |
| 1161 | assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); |
| 1162 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1163 | if (how_many == 0) |
| 1164 | return 0; |
| 1165 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1166 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1167 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1168 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1169 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1170 | |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1171 | #ifdef Py_DEBUG |
| 1172 | if (!check_maxchar |
| 1173 | && PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)) |
| 1174 | { |
| 1175 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1176 | Py_UCS4 ch; |
| 1177 | Py_ssize_t i; |
| 1178 | for (i=0; i < how_many; i++) { |
| 1179 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1180 | assert(ch <= to_maxchar); |
| 1181 | } |
| 1182 | } |
| 1183 | #endif |
| 1184 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1185 | if (from_kind == to_kind) { |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1186 | if (check_maxchar |
| 1187 | && !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) |
| 1188 | { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1189 | /* Writing Latin-1 characters into an ASCII string requires to |
| 1190 | check that all written characters are pure ASCII */ |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1191 | Py_UCS4 max_char; |
| 1192 | max_char = ucs1lib_find_max_char(from_data, |
| 1193 | (Py_UCS1*)from_data + how_many); |
| 1194 | if (max_char >= 128) |
| 1195 | return -1; |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1196 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 1197 | Py_MEMCPY((char*)to_data + to_kind * to_start, |
| 1198 | (char*)from_data + from_kind * from_start, |
| 1199 | to_kind * how_many); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1200 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1201 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 1202 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1203 | { |
| 1204 | _PyUnicode_CONVERT_BYTES( |
| 1205 | Py_UCS1, Py_UCS2, |
| 1206 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1207 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1208 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1209 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1210 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 1211 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1212 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1213 | { |
| 1214 | _PyUnicode_CONVERT_BYTES( |
| 1215 | Py_UCS1, Py_UCS4, |
| 1216 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 1217 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 1218 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1219 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1220 | } |
| 1221 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 1222 | && to_kind == PyUnicode_4BYTE_KIND) |
| 1223 | { |
| 1224 | _PyUnicode_CONVERT_BYTES( |
| 1225 | Py_UCS2, Py_UCS4, |
| 1226 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1227 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1228 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1229 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1230 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1231 | else { |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1232 | assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to)); |
| 1233 | |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1234 | if (!check_maxchar) { |
| 1235 | if (from_kind == PyUnicode_2BYTE_KIND |
| 1236 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1237 | { |
| 1238 | _PyUnicode_CONVERT_BYTES( |
| 1239 | Py_UCS2, Py_UCS1, |
| 1240 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1241 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1242 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1243 | ); |
| 1244 | } |
| 1245 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1246 | && to_kind == PyUnicode_1BYTE_KIND) |
| 1247 | { |
| 1248 | _PyUnicode_CONVERT_BYTES( |
| 1249 | Py_UCS4, Py_UCS1, |
| 1250 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1251 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1252 | PyUnicode_1BYTE_DATA(to) + to_start |
| 1253 | ); |
| 1254 | } |
| 1255 | else if (from_kind == PyUnicode_4BYTE_KIND |
| 1256 | && to_kind == PyUnicode_2BYTE_KIND) |
| 1257 | { |
| 1258 | _PyUnicode_CONVERT_BYTES( |
| 1259 | Py_UCS4, Py_UCS2, |
| 1260 | PyUnicode_4BYTE_DATA(from) + from_start, |
| 1261 | PyUnicode_4BYTE_DATA(from) + from_start + how_many, |
| 1262 | PyUnicode_2BYTE_DATA(to) + to_start |
| 1263 | ); |
| 1264 | } |
| 1265 | else { |
| 1266 | assert(0); |
| 1267 | return -1; |
| 1268 | } |
| 1269 | } |
Victor Stinner | f185226 | 2012-06-16 16:38:26 +0200 | [diff] [blame] | 1270 | else { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1271 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1272 | Py_UCS4 ch; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1273 | Py_ssize_t i; |
| 1274 | |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1275 | for (i=0; i < how_many; i++) { |
| 1276 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 1277 | if (ch > to_maxchar) |
| 1278 | return -1; |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1279 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1280 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1281 | } |
| 1282 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1283 | return 0; |
| 1284 | } |
| 1285 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1286 | void |
| 1287 | _PyUnicode_FastCopyCharacters( |
| 1288 | PyObject *to, Py_ssize_t to_start, |
| 1289 | PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1290 | { |
| 1291 | (void)_copy_characters(to, to_start, from, from_start, how_many, 0); |
| 1292 | } |
| 1293 | |
| 1294 | Py_ssize_t |
| 1295 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 1296 | PyObject *from, Py_ssize_t from_start, |
| 1297 | Py_ssize_t how_many) |
| 1298 | { |
| 1299 | int err; |
| 1300 | |
| 1301 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 1302 | PyErr_BadInternalCall(); |
| 1303 | return -1; |
| 1304 | } |
| 1305 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1306 | if (PyUnicode_READY(from) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1307 | return -1; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 1308 | if (PyUnicode_READY(to) == -1) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1309 | return -1; |
| 1310 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1311 | if (from_start < 0) { |
| 1312 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1313 | return -1; |
| 1314 | } |
| 1315 | if (to_start < 0) { |
| 1316 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 1317 | return -1; |
| 1318 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1319 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
| 1320 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 1321 | PyErr_Format(PyExc_SystemError, |
| 1322 | "Cannot write %zi characters at %zi " |
| 1323 | "in a string of %zi characters", |
| 1324 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 1325 | return -1; |
| 1326 | } |
| 1327 | |
| 1328 | if (how_many == 0) |
| 1329 | return 0; |
| 1330 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1331 | if (unicode_check_modifiable(to)) |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1332 | return -1; |
| 1333 | |
| 1334 | err = _copy_characters(to, to_start, from, from_start, how_many, 1); |
| 1335 | if (err) { |
| 1336 | PyErr_Format(PyExc_SystemError, |
| 1337 | "Cannot copy %s characters " |
| 1338 | "into a string of %s characters", |
| 1339 | unicode_kind_name(from), |
| 1340 | unicode_kind_name(to)); |
| 1341 | return -1; |
| 1342 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1343 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1344 | } |
| 1345 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1346 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1347 | correct string length can be computed before converting a string to UCS4. |
| 1348 | This function counts single surrogates as a character and not as a pair. |
| 1349 | |
| 1350 | Return 0 on success, or -1 on error. */ |
| 1351 | static int |
| 1352 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1353 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1354 | { |
| 1355 | const wchar_t *iter; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1356 | Py_UCS4 ch; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1357 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1358 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1359 | *num_surrogates = 0; |
| 1360 | *maxchar = 0; |
| 1361 | |
| 1362 | for (iter = begin; iter < end; ) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1363 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | ca4f207 | 2011-11-22 03:38:40 +0100 | [diff] [blame] | 1364 | if (Py_UNICODE_IS_HIGH_SURROGATE(iter[0]) |
| 1365 | && (iter+1) < end |
| 1366 | && Py_UNICODE_IS_LOW_SURROGATE(iter[1])) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1367 | { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1368 | ch = Py_UNICODE_JOIN_SURROGATES(iter[0], iter[1]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1369 | ++(*num_surrogates); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1370 | iter += 2; |
| 1371 | } |
| 1372 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1373 | #endif |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1374 | { |
| 1375 | ch = *iter; |
| 1376 | iter++; |
| 1377 | } |
| 1378 | if (ch > *maxchar) { |
| 1379 | *maxchar = ch; |
| 1380 | if (*maxchar > MAX_UNICODE) { |
| 1381 | PyErr_Format(PyExc_ValueError, |
| 1382 | "character U+%x is not in range [U+0000; U+10ffff]", |
| 1383 | ch); |
| 1384 | return -1; |
| 1385 | } |
| 1386 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1387 | } |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1391 | int |
| 1392 | _PyUnicode_Ready(PyObject *unicode) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1393 | { |
| 1394 | wchar_t *end; |
| 1395 | Py_UCS4 maxchar = 0; |
| 1396 | Py_ssize_t num_surrogates; |
| 1397 | #if SIZEOF_WCHAR_T == 2 |
| 1398 | Py_ssize_t length_wo_surrogates; |
| 1399 | #endif |
| 1400 | |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 1401 | /* _PyUnicode_Ready() is only intended for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1402 | strings were created using _PyObject_New() and where no canonical |
| 1403 | representation (the str field) has been set yet aka strings |
| 1404 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1405 | assert(_PyUnicode_CHECK(unicode)); |
| 1406 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1407 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1408 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1409 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1410 | /* Actually, it should neither be interned nor be anything else: */ |
| 1411 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1412 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1413 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1414 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1415 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1416 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1417 | |
| 1418 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1419 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1420 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1421 | PyErr_NoMemory(); |
| 1422 | return -1; |
| 1423 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1424 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1425 | _PyUnicode_WSTR(unicode), end, |
| 1426 | PyUnicode_1BYTE_DATA(unicode)); |
| 1427 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1428 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1429 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1430 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1431 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1432 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1433 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1434 | } |
| 1435 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1436 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1437 | _PyUnicode_UTF8(unicode) = NULL; |
| 1438 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1439 | } |
| 1440 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1441 | _PyUnicode_WSTR(unicode) = NULL; |
| 1442 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1443 | } |
| 1444 | /* In this case we might have to convert down from 4-byte native |
| 1445 | wchar_t to 2-byte unicode. */ |
| 1446 | else if (maxchar < 65536) { |
| 1447 | assert(num_surrogates == 0 && |
| 1448 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1449 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1450 | #if SIZEOF_WCHAR_T == 2 |
| 1451 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1452 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1453 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1454 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1455 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1456 | _PyUnicode_UTF8(unicode) = NULL; |
| 1457 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1458 | #else |
| 1459 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1460 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1461 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1462 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1463 | PyErr_NoMemory(); |
| 1464 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1465 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1466 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1467 | _PyUnicode_WSTR(unicode), end, |
| 1468 | PyUnicode_2BYTE_DATA(unicode)); |
| 1469 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1470 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1471 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_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 | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1474 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1475 | _PyUnicode_WSTR(unicode) = NULL; |
| 1476 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1477 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1478 | } |
| 1479 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1480 | else { |
| 1481 | #if SIZEOF_WCHAR_T == 2 |
| 1482 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1483 | new normalized 4-byte version. */ |
| 1484 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1485 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1486 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1487 | PyErr_NoMemory(); |
| 1488 | return -1; |
| 1489 | } |
| 1490 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1491 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1492 | _PyUnicode_UTF8(unicode) = NULL; |
| 1493 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1494 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1495 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1496 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1497 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1498 | _PyUnicode_WSTR(unicode) = NULL; |
| 1499 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1500 | #else |
| 1501 | assert(num_surrogates == 0); |
| 1502 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1503 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1504 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1505 | _PyUnicode_UTF8(unicode) = NULL; |
| 1506 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1507 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1508 | #endif |
| 1509 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1510 | } |
| 1511 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1512 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1513 | return 0; |
| 1514 | } |
| 1515 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1516 | static void |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1517 | unicode_dealloc(register PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1518 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1519 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1520 | case SSTATE_NOT_INTERNED: |
| 1521 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1522 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1523 | case SSTATE_INTERNED_MORTAL: |
| 1524 | /* revive dead object temporarily for DelItem */ |
| 1525 | Py_REFCNT(unicode) = 3; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 1526 | if (PyDict_DelItem(interned, unicode) != 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1527 | Py_FatalError( |
| 1528 | "deletion of interned string failed"); |
| 1529 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1530 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1531 | case SSTATE_INTERNED_IMMORTAL: |
| 1532 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1533 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1534 | default: |
| 1535 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1538 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1539 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1540 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1541 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1542 | if (!PyUnicode_IS_COMPACT(unicode) && _PyUnicode_DATA_ANY(unicode)) |
| 1543 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1544 | |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1545 | Py_TYPE(unicode)->tp_free(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1548 | #ifdef Py_DEBUG |
| 1549 | static int |
| 1550 | unicode_is_singleton(PyObject *unicode) |
| 1551 | { |
| 1552 | PyASCIIObject *ascii = (PyASCIIObject *)unicode; |
| 1553 | if (unicode == unicode_empty) |
| 1554 | return 1; |
| 1555 | if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) |
| 1556 | { |
| 1557 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
| 1558 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1559 | return 1; |
| 1560 | } |
| 1561 | return 0; |
| 1562 | } |
| 1563 | #endif |
| 1564 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1565 | static int |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1566 | unicode_modifiable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1567 | { |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1568 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1569 | if (Py_REFCNT(unicode) != 1) |
| 1570 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1571 | if (_PyUnicode_HASH(unicode) != -1) |
| 1572 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1573 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1574 | return 0; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1575 | if (!PyUnicode_CheckExact(unicode)) |
| 1576 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1577 | #ifdef Py_DEBUG |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 1578 | /* singleton refcount is greater than 1 */ |
| 1579 | assert(!unicode_is_singleton(unicode)); |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1580 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1581 | return 1; |
| 1582 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1583 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1584 | static int |
| 1585 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1586 | { |
| 1587 | PyObject *unicode; |
| 1588 | Py_ssize_t old_length; |
| 1589 | |
| 1590 | assert(p_unicode != NULL); |
| 1591 | unicode = *p_unicode; |
| 1592 | |
| 1593 | assert(unicode != NULL); |
| 1594 | assert(PyUnicode_Check(unicode)); |
| 1595 | assert(0 <= length); |
| 1596 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1597 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1598 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1599 | else |
| 1600 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1601 | if (old_length == length) |
| 1602 | return 0; |
| 1603 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1604 | if (length == 0) { |
| 1605 | Py_DECREF(*p_unicode); |
| 1606 | *p_unicode = unicode_empty; |
| 1607 | Py_INCREF(*p_unicode); |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 1611 | if (!unicode_modifiable(unicode)) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1612 | PyObject *copy = resize_copy(unicode, length); |
| 1613 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1614 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1615 | Py_DECREF(*p_unicode); |
| 1616 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1617 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1620 | if (PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1621 | PyObject *new_unicode = resize_compact(unicode, length); |
| 1622 | if (new_unicode == NULL) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1623 | return -1; |
Victor Stinner | b0a82a6 | 2011-12-12 13:08:33 +0100 | [diff] [blame] | 1624 | *p_unicode = new_unicode; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1625 | return 0; |
Benjamin Peterson | 4bfce8f | 2011-10-03 19:35:07 -0400 | [diff] [blame] | 1626 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1627 | return resize_inplace(unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1630 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1631 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1632 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1633 | PyObject *unicode; |
| 1634 | if (p_unicode == NULL) { |
| 1635 | PyErr_BadInternalCall(); |
| 1636 | return -1; |
| 1637 | } |
| 1638 | unicode = *p_unicode; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 1639 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1640 | { |
| 1641 | PyErr_BadInternalCall(); |
| 1642 | return -1; |
| 1643 | } |
| 1644 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1645 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1646 | |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1647 | /* Copy a ASCII or latin1 char* string into a Python Unicode string. |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1648 | |
Victor Stinner | b429d3b | 2012-02-22 21:22:20 +0100 | [diff] [blame] | 1649 | WARNING: The function doesn't copy the terminating null character and |
| 1650 | doesn't check the maximum character (may write a latin1 character in an |
| 1651 | ASCII string). */ |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1652 | static void |
| 1653 | unicode_write_cstr(PyObject *unicode, Py_ssize_t index, |
| 1654 | const char *str, Py_ssize_t len) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1655 | { |
| 1656 | enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 1657 | void *data = PyUnicode_DATA(unicode); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1658 | const char *end = str + len; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1659 | |
| 1660 | switch (kind) { |
| 1661 | case PyUnicode_1BYTE_KIND: { |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1662 | assert(index + len <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 8c6db45 | 2012-10-06 00:40:45 +0200 | [diff] [blame] | 1663 | #ifdef Py_DEBUG |
| 1664 | if (PyUnicode_IS_ASCII(unicode)) { |
| 1665 | Py_UCS4 maxchar = ucs1lib_find_max_char( |
| 1666 | (const Py_UCS1*)str, |
| 1667 | (const Py_UCS1*)str + len); |
| 1668 | assert(maxchar < 128); |
| 1669 | } |
| 1670 | #endif |
Antoine Pitrou | ba6bafc | 2012-02-22 16:41:50 +0100 | [diff] [blame] | 1671 | memcpy((char *) data + index, str, len); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1672 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1673 | } |
| 1674 | case PyUnicode_2BYTE_KIND: { |
| 1675 | Py_UCS2 *start = (Py_UCS2 *)data + index; |
| 1676 | Py_UCS2 *ucs2 = start; |
| 1677 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1678 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1679 | for (; str < end; ++ucs2, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1680 | *ucs2 = (Py_UCS2)*str; |
| 1681 | |
| 1682 | assert((ucs2 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1683 | break; |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1684 | } |
| 1685 | default: { |
| 1686 | Py_UCS4 *start = (Py_UCS4 *)data + index; |
| 1687 | Py_UCS4 *ucs4 = start; |
| 1688 | assert(kind == PyUnicode_4BYTE_KIND); |
| 1689 | assert(index <= PyUnicode_GET_LENGTH(unicode)); |
| 1690 | |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 1691 | for (; str < end; ++ucs4, ++str) |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1692 | *ucs4 = (Py_UCS4)*str; |
| 1693 | |
| 1694 | assert((ucs4 - start) <= PyUnicode_GET_LENGTH(unicode)); |
Victor Stinner | c516610 | 2012-02-22 13:55:02 +0100 | [diff] [blame] | 1695 | } |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1700 | static PyObject* |
| 1701 | get_latin1_char(unsigned char ch) |
| 1702 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1703 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1704 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1705 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1706 | if (!unicode) |
| 1707 | return NULL; |
| 1708 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1709 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1710 | unicode_latin1[ch] = unicode; |
| 1711 | } |
| 1712 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1713 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1714 | } |
| 1715 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1716 | PyObject * |
| 1717 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1718 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 1719 | PyObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1720 | Py_UCS4 maxchar = 0; |
| 1721 | Py_ssize_t num_surrogates; |
| 1722 | |
| 1723 | if (u == NULL) |
| 1724 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1725 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1726 | /* If the Unicode data is known at construction time, we can apply |
| 1727 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1728 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1729 | /* Optimization for empty strings */ |
| 1730 | if (size == 0 && unicode_empty != NULL) { |
| 1731 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1732 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1733 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1734 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1735 | /* Single character Unicode objects in the Latin-1 range are |
| 1736 | shared when using this constructor */ |
| 1737 | if (size == 1 && *u < 256) |
| 1738 | return get_latin1_char((unsigned char)*u); |
| 1739 | |
| 1740 | /* If not empty and not single character, copy the Unicode data |
| 1741 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1742 | if (find_maxchar_surrogates(u, u + size, |
| 1743 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1744 | return NULL; |
| 1745 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1746 | unicode = PyUnicode_New(size - num_surrogates, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1747 | if (!unicode) |
| 1748 | return NULL; |
| 1749 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1750 | switch (PyUnicode_KIND(unicode)) { |
| 1751 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1752 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1753 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1754 | break; |
| 1755 | case PyUnicode_2BYTE_KIND: |
| 1756 | #if Py_UNICODE_SIZE == 2 |
| 1757 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1758 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1759 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1760 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1761 | #endif |
| 1762 | break; |
| 1763 | case PyUnicode_4BYTE_KIND: |
| 1764 | #if SIZEOF_WCHAR_T == 2 |
| 1765 | /* This is the only case which has to process surrogates, thus |
| 1766 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1767 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1768 | #else |
| 1769 | assert(num_surrogates == 0); |
| 1770 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1771 | #endif |
| 1772 | break; |
| 1773 | default: |
| 1774 | assert(0 && "Impossible state"); |
| 1775 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1776 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1777 | return unicode_result(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1780 | PyObject * |
| 1781 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1782 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1783 | if (size < 0) { |
| 1784 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1785 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1786 | return NULL; |
| 1787 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1788 | if (u != NULL) |
| 1789 | return PyUnicode_DecodeUTF8Stateful(u, size, NULL, NULL); |
| 1790 | else |
| 1791 | return (PyObject *)_PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1794 | PyObject * |
| 1795 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1796 | { |
| 1797 | size_t size = strlen(u); |
| 1798 | if (size > PY_SSIZE_T_MAX) { |
| 1799 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1800 | return NULL; |
| 1801 | } |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 1802 | return PyUnicode_DecodeUTF8Stateful(u, (Py_ssize_t)size, NULL, NULL); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1805 | PyObject * |
| 1806 | _PyUnicode_FromId(_Py_Identifier *id) |
| 1807 | { |
| 1808 | if (!id->object) { |
Victor Stinner | d1cd99b | 2012-02-07 23:05:55 +0100 | [diff] [blame] | 1809 | id->object = PyUnicode_DecodeUTF8Stateful(id->string, |
| 1810 | strlen(id->string), |
| 1811 | NULL, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1812 | if (!id->object) |
| 1813 | return NULL; |
| 1814 | PyUnicode_InternInPlace(&id->object); |
| 1815 | assert(!id->next); |
| 1816 | id->next = static_strings; |
| 1817 | static_strings = id; |
| 1818 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1819 | return id->object; |
| 1820 | } |
| 1821 | |
| 1822 | void |
| 1823 | _PyUnicode_ClearStaticStrings() |
| 1824 | { |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1825 | _Py_Identifier *tmp, *s = static_strings; |
| 1826 | while (s) { |
| 1827 | Py_DECREF(s->object); |
| 1828 | s->object = NULL; |
| 1829 | tmp = s->next; |
| 1830 | s->next = NULL; |
| 1831 | s = tmp; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1832 | } |
Benjamin Peterson | 0c270a8 | 2013-01-09 09:52:01 -0600 | [diff] [blame] | 1833 | static_strings = NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1834 | } |
| 1835 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1836 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1837 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1838 | PyObject* |
| 1839 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1840 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1841 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1842 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1843 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1844 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1845 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1846 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1847 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1848 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1849 | unicode = PyUnicode_New(size, 127); |
| 1850 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1851 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1852 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1853 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1854 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1855 | } |
| 1856 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1857 | static Py_UCS4 |
| 1858 | kind_maxchar_limit(unsigned int kind) |
| 1859 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1860 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1861 | case PyUnicode_1BYTE_KIND: |
| 1862 | return 0x80; |
| 1863 | case PyUnicode_2BYTE_KIND: |
| 1864 | return 0x100; |
| 1865 | case PyUnicode_4BYTE_KIND: |
| 1866 | return 0x10000; |
| 1867 | default: |
| 1868 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1869 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1873 | Py_LOCAL_INLINE(Py_UCS4) |
| 1874 | align_maxchar(Py_UCS4 maxchar) |
| 1875 | { |
| 1876 | if (maxchar <= 127) |
| 1877 | return 127; |
| 1878 | else if (maxchar <= 255) |
| 1879 | return 255; |
| 1880 | else if (maxchar <= 65535) |
| 1881 | return 65535; |
| 1882 | else |
| 1883 | return MAX_UNICODE; |
| 1884 | } |
| 1885 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1886 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1887 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1888 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1889 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1890 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1891 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1892 | if (size == 0) { |
| 1893 | Py_INCREF(unicode_empty); |
| 1894 | return unicode_empty; |
| 1895 | } |
| 1896 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1897 | if (size == 1) |
| 1898 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1899 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1900 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1901 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1902 | if (!res) |
| 1903 | return NULL; |
| 1904 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1905 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1906 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1909 | static PyObject* |
| 1910 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1911 | { |
| 1912 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1913 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1914 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1915 | if (size == 0) { |
| 1916 | Py_INCREF(unicode_empty); |
| 1917 | return unicode_empty; |
| 1918 | } |
| 1919 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1920 | if (size == 1) { |
| 1921 | Py_UCS4 ch = u[0]; |
| 1922 | if (ch < 256) |
| 1923 | return get_latin1_char((unsigned char)ch); |
| 1924 | |
| 1925 | res = PyUnicode_New(1, ch); |
| 1926 | if (res == NULL) |
| 1927 | return NULL; |
| 1928 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1929 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1930 | return res; |
| 1931 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1932 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1933 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1934 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1935 | if (!res) |
| 1936 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1937 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1938 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1939 | else { |
| 1940 | _PyUnicode_CONVERT_BYTES( |
| 1941 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1942 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1943 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1944 | return res; |
| 1945 | } |
| 1946 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1947 | static PyObject* |
| 1948 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1949 | { |
| 1950 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1951 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1952 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1953 | if (size == 0) { |
| 1954 | Py_INCREF(unicode_empty); |
| 1955 | return unicode_empty; |
| 1956 | } |
| 1957 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1958 | if (size == 1) { |
| 1959 | Py_UCS4 ch = u[0]; |
| 1960 | if (ch < 256) |
| 1961 | return get_latin1_char((unsigned char)ch); |
| 1962 | |
| 1963 | res = PyUnicode_New(1, ch); |
| 1964 | if (res == NULL) |
| 1965 | return NULL; |
| 1966 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1967 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1968 | return res; |
| 1969 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1970 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1971 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1972 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1973 | if (!res) |
| 1974 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1975 | if (max_char < 256) |
| 1976 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1977 | PyUnicode_1BYTE_DATA(res)); |
| 1978 | else if (max_char < 0x10000) |
| 1979 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1980 | PyUnicode_2BYTE_DATA(res)); |
| 1981 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1982 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1983 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1984 | return res; |
| 1985 | } |
| 1986 | |
| 1987 | PyObject* |
| 1988 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1989 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1990 | if (size < 0) { |
| 1991 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1992 | return NULL; |
| 1993 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1994 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1995 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1996 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1997 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1998 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1999 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 2000 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2001 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 2002 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2003 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2004 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2005 | } |
| 2006 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2007 | Py_UCS4 |
| 2008 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2009 | { |
| 2010 | enum PyUnicode_Kind kind; |
| 2011 | void *startptr, *endptr; |
| 2012 | |
| 2013 | assert(PyUnicode_IS_READY(unicode)); |
| 2014 | assert(0 <= start); |
| 2015 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2016 | assert(start <= end); |
| 2017 | |
| 2018 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2019 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2020 | |
| 2021 | if (start == end) |
| 2022 | return 127; |
| 2023 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2024 | if (PyUnicode_IS_ASCII(unicode)) |
| 2025 | return 127; |
| 2026 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2027 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2028 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2029 | endptr = (char *)startptr + end * kind; |
| 2030 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2031 | switch(kind) { |
| 2032 | case PyUnicode_1BYTE_KIND: |
| 2033 | return ucs1lib_find_max_char(startptr, endptr); |
| 2034 | case PyUnicode_2BYTE_KIND: |
| 2035 | return ucs2lib_find_max_char(startptr, endptr); |
| 2036 | case PyUnicode_4BYTE_KIND: |
| 2037 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2038 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2039 | assert(0); |
| 2040 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2041 | } |
| 2042 | } |
| 2043 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2044 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2045 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2046 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2047 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2048 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2049 | { |
| 2050 | PyObject *unicode, *copy; |
| 2051 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2052 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2053 | unsigned int kind; |
| 2054 | |
| 2055 | assert(p_unicode != NULL); |
| 2056 | unicode = *p_unicode; |
| 2057 | assert(PyUnicode_IS_READY(unicode)); |
| 2058 | if (PyUnicode_IS_ASCII(unicode)) |
| 2059 | return; |
| 2060 | |
| 2061 | len = PyUnicode_GET_LENGTH(unicode); |
| 2062 | kind = PyUnicode_KIND(unicode); |
| 2063 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2064 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2065 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2066 | if (max_char >= 128) |
| 2067 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2068 | } |
| 2069 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2070 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2071 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2072 | if (max_char >= 256) |
| 2073 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2074 | } |
| 2075 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2076 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2077 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2078 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2079 | if (max_char >= 0x10000) |
| 2080 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2081 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2082 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2083 | if (copy != NULL) |
| 2084 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2085 | Py_DECREF(unicode); |
| 2086 | *p_unicode = copy; |
| 2087 | } |
| 2088 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2089 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2090 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2091 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2092 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2093 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2094 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2095 | if (!PyUnicode_Check(unicode)) { |
| 2096 | PyErr_BadInternalCall(); |
| 2097 | return NULL; |
| 2098 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2099 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2100 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2101 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2102 | length = PyUnicode_GET_LENGTH(unicode); |
| 2103 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2104 | if (!copy) |
| 2105 | return NULL; |
| 2106 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2107 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2108 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2109 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2110 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2111 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2112 | } |
| 2113 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2114 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2115 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2116 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2117 | |
| 2118 | void* |
| 2119 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2120 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2121 | Py_ssize_t len; |
| 2122 | void *result; |
| 2123 | unsigned int skind; |
| 2124 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2125 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2126 | return NULL; |
| 2127 | |
| 2128 | len = PyUnicode_GET_LENGTH(s); |
| 2129 | skind = PyUnicode_KIND(s); |
| 2130 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2131 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2132 | return NULL; |
| 2133 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2134 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2135 | case PyUnicode_2BYTE_KIND: |
| 2136 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2137 | if (!result) |
| 2138 | return PyErr_NoMemory(); |
| 2139 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2140 | _PyUnicode_CONVERT_BYTES( |
| 2141 | Py_UCS1, Py_UCS2, |
| 2142 | PyUnicode_1BYTE_DATA(s), |
| 2143 | PyUnicode_1BYTE_DATA(s) + len, |
| 2144 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2145 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2146 | case PyUnicode_4BYTE_KIND: |
| 2147 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2148 | if (!result) |
| 2149 | return PyErr_NoMemory(); |
| 2150 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2151 | _PyUnicode_CONVERT_BYTES( |
| 2152 | Py_UCS2, Py_UCS4, |
| 2153 | PyUnicode_2BYTE_DATA(s), |
| 2154 | PyUnicode_2BYTE_DATA(s) + len, |
| 2155 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2156 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2157 | else { |
| 2158 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2159 | _PyUnicode_CONVERT_BYTES( |
| 2160 | Py_UCS1, Py_UCS4, |
| 2161 | PyUnicode_1BYTE_DATA(s), |
| 2162 | PyUnicode_1BYTE_DATA(s) + len, |
| 2163 | result); |
| 2164 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2165 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2166 | default: |
| 2167 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2168 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2169 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2170 | return NULL; |
| 2171 | } |
| 2172 | |
| 2173 | static Py_UCS4* |
| 2174 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2175 | int copy_null) |
| 2176 | { |
| 2177 | int kind; |
| 2178 | void *data; |
| 2179 | Py_ssize_t len, targetlen; |
| 2180 | if (PyUnicode_READY(string) == -1) |
| 2181 | return NULL; |
| 2182 | kind = PyUnicode_KIND(string); |
| 2183 | data = PyUnicode_DATA(string); |
| 2184 | len = PyUnicode_GET_LENGTH(string); |
| 2185 | targetlen = len; |
| 2186 | if (copy_null) |
| 2187 | targetlen++; |
| 2188 | if (!target) { |
| 2189 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2190 | PyErr_NoMemory(); |
| 2191 | return NULL; |
| 2192 | } |
| 2193 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2194 | if (!target) { |
| 2195 | PyErr_NoMemory(); |
| 2196 | return NULL; |
| 2197 | } |
| 2198 | } |
| 2199 | else { |
| 2200 | if (targetsize < targetlen) { |
| 2201 | PyErr_Format(PyExc_SystemError, |
| 2202 | "string is longer than the buffer"); |
| 2203 | if (copy_null && 0 < targetsize) |
| 2204 | target[0] = 0; |
| 2205 | return NULL; |
| 2206 | } |
| 2207 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2208 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2209 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2210 | _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] | 2211 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2212 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2213 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2214 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2215 | } |
| 2216 | else { |
| 2217 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2218 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2219 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2220 | if (copy_null) |
| 2221 | target[len] = 0; |
| 2222 | return target; |
| 2223 | } |
| 2224 | |
| 2225 | Py_UCS4* |
| 2226 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2227 | int copy_null) |
| 2228 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2229 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2230 | PyErr_BadInternalCall(); |
| 2231 | return NULL; |
| 2232 | } |
| 2233 | return as_ucs4(string, target, targetsize, copy_null); |
| 2234 | } |
| 2235 | |
| 2236 | Py_UCS4* |
| 2237 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2238 | { |
| 2239 | return as_ucs4(string, NULL, 0, 1); |
| 2240 | } |
| 2241 | |
| 2242 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2243 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2244 | PyObject * |
| 2245 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2246 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2247 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2248 | if (size == 0) { |
| 2249 | Py_INCREF(unicode_empty); |
| 2250 | return unicode_empty; |
| 2251 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2252 | PyErr_BadInternalCall(); |
| 2253 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2256 | if (size == -1) { |
| 2257 | size = wcslen(w); |
| 2258 | } |
| 2259 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2260 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2263 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2264 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2265 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2266 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2267 | char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2268 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2269 | *fmt++ = '%'; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2270 | if (longflag) |
| 2271 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2272 | else if (longlongflag) { |
| 2273 | /* longlongflag should only ever be nonzero on machines with |
| 2274 | HAVE_LONG_LONG defined */ |
| 2275 | #ifdef HAVE_LONG_LONG |
| 2276 | char *f = PY_FORMAT_LONG_LONG; |
| 2277 | while (*f) |
| 2278 | *fmt++ = *f++; |
| 2279 | #else |
| 2280 | /* we shouldn't ever get here */ |
| 2281 | assert(0); |
| 2282 | *fmt++ = 'l'; |
| 2283 | #endif |
| 2284 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2285 | else if (size_tflag) { |
| 2286 | char *f = PY_FORMAT_SIZE_T; |
| 2287 | while (*f) |
| 2288 | *fmt++ = *f++; |
| 2289 | } |
| 2290 | *fmt++ = c; |
| 2291 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2294 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2295 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2296 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2297 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2298 | |
| 2299 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2300 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2301 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2302 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2303 | const char *p; |
| 2304 | Py_ssize_t len; |
| 2305 | int zeropad; |
| 2306 | int width; |
| 2307 | int precision; |
| 2308 | int longflag; |
| 2309 | int longlongflag; |
| 2310 | int size_tflag; |
| 2311 | int fill; |
| 2312 | |
| 2313 | p = f; |
| 2314 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2315 | zeropad = 0; |
| 2316 | if (*f == '0') { |
| 2317 | zeropad = 1; |
| 2318 | f++; |
| 2319 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2320 | |
| 2321 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2322 | width = 0; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2323 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2324 | if (width > (INT_MAX - ((int)*f - '0')) / 10) { |
| 2325 | PyErr_SetString(PyExc_ValueError, |
| 2326 | "width too big"); |
| 2327 | return NULL; |
| 2328 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2329 | width = (width*10) + (*f - '0'); |
| 2330 | f++; |
| 2331 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2332 | precision = 0; |
| 2333 | if (*f == '.') { |
| 2334 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2335 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2336 | if (precision > (INT_MAX - ((int)*f - '0')) / 10) { |
| 2337 | PyErr_SetString(PyExc_ValueError, |
| 2338 | "precision too big"); |
| 2339 | return NULL; |
| 2340 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2341 | precision = (precision*10) + (*f - '0'); |
| 2342 | f++; |
| 2343 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2344 | if (*f == '%') { |
| 2345 | /* "%.3%s" => f points to "3" */ |
| 2346 | f--; |
| 2347 | } |
| 2348 | } |
| 2349 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2350 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2351 | f--; |
| 2352 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2353 | |
| 2354 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2355 | longflag = 0; |
| 2356 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2357 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2358 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2359 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2360 | longflag = 1; |
| 2361 | ++f; |
| 2362 | } |
| 2363 | #ifdef HAVE_LONG_LONG |
| 2364 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2365 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2366 | longlongflag = 1; |
| 2367 | f += 2; |
| 2368 | } |
| 2369 | #endif |
| 2370 | } |
| 2371 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2372 | 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] | 2373 | size_tflag = 1; |
| 2374 | ++f; |
| 2375 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2376 | |
| 2377 | if (f[1] == '\0') |
| 2378 | writer->overallocate = 0; |
| 2379 | |
| 2380 | switch (*f) { |
| 2381 | case 'c': |
| 2382 | { |
| 2383 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2384 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
| 2385 | PyErr_SetString(PyExc_ValueError, |
| 2386 | "character argument not in range(0x110000)"); |
| 2387 | return NULL; |
| 2388 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2389 | if (_PyUnicodeWriter_Prepare(writer, 1, ordinal) == -1) |
| 2390 | return NULL; |
| 2391 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ordinal); |
| 2392 | writer->pos++; |
| 2393 | break; |
| 2394 | } |
| 2395 | |
| 2396 | case 'i': |
| 2397 | case 'd': |
| 2398 | case 'u': |
| 2399 | case 'x': |
| 2400 | { |
| 2401 | /* used by sprintf */ |
| 2402 | char fmt[10]; /* should be enough for "%0lld\0" */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2403 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2404 | |
| 2405 | if (*f == 'u') { |
| 2406 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2407 | |
| 2408 | if (longflag) |
| 2409 | len = sprintf(buffer, fmt, |
| 2410 | va_arg(*vargs, unsigned long)); |
| 2411 | #ifdef HAVE_LONG_LONG |
| 2412 | else if (longlongflag) |
| 2413 | len = sprintf(buffer, fmt, |
| 2414 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2415 | #endif |
| 2416 | else if (size_tflag) |
| 2417 | len = sprintf(buffer, fmt, |
| 2418 | va_arg(*vargs, size_t)); |
| 2419 | else |
| 2420 | len = sprintf(buffer, fmt, |
| 2421 | va_arg(*vargs, unsigned int)); |
| 2422 | } |
| 2423 | else if (*f == 'x') { |
| 2424 | makefmt(fmt, 0, 0, 0, 'x'); |
| 2425 | len = sprintf(buffer, fmt, va_arg(*vargs, int)); |
| 2426 | } |
| 2427 | else { |
| 2428 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2429 | |
| 2430 | if (longflag) |
| 2431 | len = sprintf(buffer, fmt, |
| 2432 | va_arg(*vargs, long)); |
| 2433 | #ifdef HAVE_LONG_LONG |
| 2434 | else if (longlongflag) |
| 2435 | len = sprintf(buffer, fmt, |
| 2436 | va_arg(*vargs, PY_LONG_LONG)); |
| 2437 | #endif |
| 2438 | else if (size_tflag) |
| 2439 | len = sprintf(buffer, fmt, |
| 2440 | va_arg(*vargs, Py_ssize_t)); |
| 2441 | else |
| 2442 | len = sprintf(buffer, fmt, |
| 2443 | va_arg(*vargs, int)); |
| 2444 | } |
| 2445 | assert(len >= 0); |
| 2446 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2447 | if (precision < len) |
| 2448 | precision = len; |
| 2449 | if (width > precision) { |
| 2450 | Py_UCS4 fillchar; |
| 2451 | fill = width - precision; |
| 2452 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2453 | if (_PyUnicodeWriter_Prepare(writer, fill, fillchar) == -1) |
| 2454 | return NULL; |
| 2455 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2456 | return NULL; |
| 2457 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2458 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2459 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2460 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2461 | if (_PyUnicodeWriter_Prepare(writer, fill, '0') == -1) |
| 2462 | return NULL; |
| 2463 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2464 | return NULL; |
| 2465 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2466 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2467 | if (_PyUnicodeWriter_WriteCstr(writer, buffer, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2468 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2469 | break; |
| 2470 | } |
| 2471 | |
| 2472 | case 'p': |
| 2473 | { |
| 2474 | char number[MAX_LONG_LONG_CHARS]; |
| 2475 | |
| 2476 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2477 | assert(len >= 0); |
| 2478 | |
| 2479 | /* %p is ill-defined: ensure leading 0x. */ |
| 2480 | if (number[1] == 'X') |
| 2481 | number[1] = 'x'; |
| 2482 | else if (number[1] != 'x') { |
| 2483 | memmove(number + 2, number, |
| 2484 | strlen(number) + 1); |
| 2485 | number[0] = '0'; |
| 2486 | number[1] = 'x'; |
| 2487 | len += 2; |
| 2488 | } |
| 2489 | |
| 2490 | if (_PyUnicodeWriter_WriteCstr(writer, number, len) == -1) |
| 2491 | return NULL; |
| 2492 | break; |
| 2493 | } |
| 2494 | |
| 2495 | case 's': |
| 2496 | { |
| 2497 | /* UTF-8 */ |
| 2498 | const char *s = va_arg(*vargs, const char*); |
| 2499 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
| 2500 | if (!str) |
| 2501 | return NULL; |
| 2502 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) { |
| 2503 | Py_DECREF(str); |
| 2504 | return NULL; |
| 2505 | } |
| 2506 | Py_DECREF(str); |
| 2507 | break; |
| 2508 | } |
| 2509 | |
| 2510 | case 'U': |
| 2511 | { |
| 2512 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2513 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2514 | |
| 2515 | if (_PyUnicodeWriter_WriteStr(writer, obj) == -1) |
| 2516 | return NULL; |
| 2517 | break; |
| 2518 | } |
| 2519 | |
| 2520 | case 'V': |
| 2521 | { |
| 2522 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2523 | const char *str = va_arg(*vargs, const char *); |
| 2524 | PyObject *str_obj; |
| 2525 | assert(obj || str); |
| 2526 | if (obj) { |
| 2527 | assert(_PyUnicode_CHECK(obj)); |
| 2528 | if (_PyUnicodeWriter_WriteStr(writer, obj) == -1) |
| 2529 | return NULL; |
| 2530 | } |
| 2531 | else { |
| 2532 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
| 2533 | if (!str_obj) |
| 2534 | return NULL; |
| 2535 | if (_PyUnicodeWriter_WriteStr(writer, str_obj) == -1) { |
| 2536 | Py_DECREF(str_obj); |
| 2537 | return NULL; |
| 2538 | } |
| 2539 | Py_DECREF(str_obj); |
| 2540 | } |
| 2541 | break; |
| 2542 | } |
| 2543 | |
| 2544 | case 'S': |
| 2545 | { |
| 2546 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2547 | PyObject *str; |
| 2548 | assert(obj); |
| 2549 | str = PyObject_Str(obj); |
| 2550 | if (!str) |
| 2551 | return NULL; |
| 2552 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) { |
| 2553 | Py_DECREF(str); |
| 2554 | return NULL; |
| 2555 | } |
| 2556 | Py_DECREF(str); |
| 2557 | break; |
| 2558 | } |
| 2559 | |
| 2560 | case 'R': |
| 2561 | { |
| 2562 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2563 | PyObject *repr; |
| 2564 | assert(obj); |
| 2565 | repr = PyObject_Repr(obj); |
| 2566 | if (!repr) |
| 2567 | return NULL; |
| 2568 | if (_PyUnicodeWriter_WriteStr(writer, repr) == -1) { |
| 2569 | Py_DECREF(repr); |
| 2570 | return NULL; |
| 2571 | } |
| 2572 | Py_DECREF(repr); |
| 2573 | break; |
| 2574 | } |
| 2575 | |
| 2576 | case 'A': |
| 2577 | { |
| 2578 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2579 | PyObject *ascii; |
| 2580 | assert(obj); |
| 2581 | ascii = PyObject_ASCII(obj); |
| 2582 | if (!ascii) |
| 2583 | return NULL; |
| 2584 | if (_PyUnicodeWriter_WriteStr(writer, ascii) == -1) { |
| 2585 | Py_DECREF(ascii); |
| 2586 | return NULL; |
| 2587 | } |
| 2588 | Py_DECREF(ascii); |
| 2589 | break; |
| 2590 | } |
| 2591 | |
| 2592 | case '%': |
| 2593 | if (_PyUnicodeWriter_Prepare(writer, 1, '%') == 1) |
| 2594 | return NULL; |
| 2595 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '%'); |
| 2596 | writer->pos++; |
| 2597 | break; |
| 2598 | |
| 2599 | default: |
| 2600 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2601 | of the format string to the output string. (we cannot just |
| 2602 | skip the code, since there's no way to know what's in the |
| 2603 | argument list) */ |
| 2604 | len = strlen(p); |
| 2605 | if (_PyUnicodeWriter_WriteCstr(writer, p, len) == -1) |
| 2606 | return NULL; |
| 2607 | f = p+len; |
| 2608 | return f; |
| 2609 | } |
| 2610 | |
| 2611 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2612 | return f; |
| 2613 | } |
| 2614 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2615 | PyObject * |
| 2616 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2617 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2618 | va_list vargs2; |
| 2619 | const char *f; |
| 2620 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2621 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2622 | _PyUnicodeWriter_Init(&writer, strlen(format) + 100); |
| 2623 | |
| 2624 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2625 | Copy it to be able to pass a reference to a subfunction. */ |
| 2626 | Py_VA_COPY(vargs2, vargs); |
| 2627 | |
| 2628 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2629 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2630 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2631 | if (f == NULL) |
| 2632 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2633 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2634 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2635 | const char *p; |
| 2636 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2637 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2638 | p = f; |
| 2639 | do |
| 2640 | { |
| 2641 | if ((unsigned char)*p > 127) { |
| 2642 | PyErr_Format(PyExc_ValueError, |
| 2643 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2644 | "string, got a non-ASCII byte: 0x%02x", |
| 2645 | (unsigned char)*p); |
| 2646 | return NULL; |
| 2647 | } |
| 2648 | p++; |
| 2649 | } |
| 2650 | while (*p != '\0' && *p != '%'); |
| 2651 | len = p - f; |
| 2652 | |
| 2653 | if (*p == '\0') |
| 2654 | writer.overallocate = 0; |
| 2655 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
| 2656 | goto fail; |
| 2657 | unicode_write_cstr(writer.buffer, writer.pos, f, len); |
| 2658 | writer.pos += len; |
| 2659 | |
| 2660 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2661 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2662 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2663 | return _PyUnicodeWriter_Finish(&writer); |
| 2664 | |
| 2665 | fail: |
| 2666 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2667 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2670 | PyObject * |
| 2671 | PyUnicode_FromFormat(const char *format, ...) |
| 2672 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2673 | PyObject* ret; |
| 2674 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2675 | |
| 2676 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2677 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2678 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2679 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2680 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2681 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2682 | va_end(vargs); |
| 2683 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2686 | #ifdef HAVE_WCHAR_H |
| 2687 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2688 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2689 | convert a Unicode object to a wide character string. |
| 2690 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2691 | - 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] | 2692 | character) required to convert the unicode object. Ignore size argument. |
| 2693 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2694 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2695 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2696 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2697 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2698 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2699 | wchar_t *w, |
| 2700 | Py_ssize_t size) |
| 2701 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2702 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2703 | const wchar_t *wstr; |
| 2704 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2705 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2706 | if (wstr == NULL) |
| 2707 | return -1; |
| 2708 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2709 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2710 | if (size > res) |
| 2711 | size = res + 1; |
| 2712 | else |
| 2713 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2714 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2715 | return res; |
| 2716 | } |
| 2717 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2718 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2722 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2723 | wchar_t *w, |
| 2724 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2725 | { |
| 2726 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2727 | PyErr_BadInternalCall(); |
| 2728 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2729 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2730 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2733 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2734 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2735 | Py_ssize_t *size) |
| 2736 | { |
| 2737 | wchar_t* buffer; |
| 2738 | Py_ssize_t buflen; |
| 2739 | |
| 2740 | if (unicode == NULL) { |
| 2741 | PyErr_BadInternalCall(); |
| 2742 | return NULL; |
| 2743 | } |
| 2744 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2745 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2746 | if (buflen == -1) |
| 2747 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2748 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2749 | PyErr_NoMemory(); |
| 2750 | return NULL; |
| 2751 | } |
| 2752 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2753 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2754 | if (buffer == NULL) { |
| 2755 | PyErr_NoMemory(); |
| 2756 | return NULL; |
| 2757 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2758 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2759 | if (buflen == -1) { |
| 2760 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2761 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2762 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2763 | if (size != NULL) |
| 2764 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2765 | return buffer; |
| 2766 | } |
| 2767 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2768 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2769 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2770 | PyObject * |
| 2771 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2772 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2773 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2774 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2775 | PyErr_SetString(PyExc_ValueError, |
| 2776 | "chr() arg not in range(0x110000)"); |
| 2777 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2778 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2779 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2780 | if (ordinal < 256) |
| 2781 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2782 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2783 | v = PyUnicode_New(1, ordinal); |
| 2784 | if (v == NULL) |
| 2785 | return NULL; |
| 2786 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2787 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2788 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2791 | PyObject * |
| 2792 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2793 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2794 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2795 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2796 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2797 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2798 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2799 | Py_INCREF(obj); |
| 2800 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2801 | } |
| 2802 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2803 | /* For a Unicode subtype that's not a Unicode object, |
| 2804 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2805 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2806 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2807 | PyErr_Format(PyExc_TypeError, |
| 2808 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2809 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2810 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2813 | PyObject * |
| 2814 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2815 | const char *encoding, |
| 2816 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2817 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2818 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2819 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2820 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2821 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2822 | PyErr_BadInternalCall(); |
| 2823 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2824 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2825 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2826 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2827 | if (PyBytes_Check(obj)) { |
| 2828 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2829 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2830 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2831 | } |
| 2832 | else { |
| 2833 | v = PyUnicode_Decode( |
| 2834 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2835 | encoding, errors); |
| 2836 | } |
| 2837 | return v; |
| 2838 | } |
| 2839 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2840 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2841 | PyErr_SetString(PyExc_TypeError, |
| 2842 | "decoding str is not supported"); |
| 2843 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2844 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2845 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2846 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2847 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2848 | PyErr_Format(PyExc_TypeError, |
| 2849 | "coercing to str: need bytes, bytearray " |
| 2850 | "or buffer-like object, %.80s found", |
| 2851 | Py_TYPE(obj)->tp_name); |
| 2852 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2853 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2854 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2855 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2856 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2857 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2858 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2859 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2860 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2861 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2862 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2863 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2866 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2867 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2868 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2869 | int |
| 2870 | _Py_normalize_encoding(const char *encoding, |
| 2871 | char *lower, |
| 2872 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2873 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2874 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2875 | char *l; |
| 2876 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2877 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2878 | if (encoding == NULL) { |
| 2879 | strcpy(lower, "utf-8"); |
| 2880 | return 1; |
| 2881 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2882 | e = encoding; |
| 2883 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2884 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2885 | while (*e) { |
| 2886 | if (l == l_end) |
| 2887 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2888 | if (Py_ISUPPER(*e)) { |
| 2889 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2890 | } |
| 2891 | else if (*e == '_') { |
| 2892 | *l++ = '-'; |
| 2893 | e++; |
| 2894 | } |
| 2895 | else { |
| 2896 | *l++ = *e++; |
| 2897 | } |
| 2898 | } |
| 2899 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2900 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2903 | PyObject * |
| 2904 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2905 | Py_ssize_t size, |
| 2906 | const char *encoding, |
| 2907 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2908 | { |
| 2909 | PyObject *buffer = NULL, *unicode; |
| 2910 | Py_buffer info; |
| 2911 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2912 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2913 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2914 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2915 | if ((strcmp(lower, "utf-8") == 0) || |
| 2916 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2917 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2918 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2919 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2920 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2921 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2922 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2923 | else if (strcmp(lower, "mbcs") == 0) |
| 2924 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2925 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2926 | else if (strcmp(lower, "ascii") == 0) |
| 2927 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2928 | else if (strcmp(lower, "utf-16") == 0) |
| 2929 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2930 | else if (strcmp(lower, "utf-32") == 0) |
| 2931 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2932 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2933 | |
| 2934 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2935 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2936 | 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] | 2937 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2938 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2939 | if (buffer == NULL) |
| 2940 | goto onError; |
| 2941 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2942 | if (unicode == NULL) |
| 2943 | goto onError; |
| 2944 | if (!PyUnicode_Check(unicode)) { |
| 2945 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2946 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2947 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2948 | Py_DECREF(unicode); |
| 2949 | goto onError; |
| 2950 | } |
| 2951 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2952 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2953 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2954 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2955 | Py_XDECREF(buffer); |
| 2956 | return NULL; |
| 2957 | } |
| 2958 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2959 | PyObject * |
| 2960 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2961 | const char *encoding, |
| 2962 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2963 | { |
| 2964 | PyObject *v; |
| 2965 | |
| 2966 | if (!PyUnicode_Check(unicode)) { |
| 2967 | PyErr_BadArgument(); |
| 2968 | goto onError; |
| 2969 | } |
| 2970 | |
| 2971 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2972 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2973 | |
| 2974 | /* Decode via the codec registry */ |
| 2975 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2976 | if (v == NULL) |
| 2977 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2978 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2979 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2980 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2981 | return NULL; |
| 2982 | } |
| 2983 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2984 | PyObject * |
| 2985 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2986 | const char *encoding, |
| 2987 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2988 | { |
| 2989 | PyObject *v; |
| 2990 | |
| 2991 | if (!PyUnicode_Check(unicode)) { |
| 2992 | PyErr_BadArgument(); |
| 2993 | goto onError; |
| 2994 | } |
| 2995 | |
| 2996 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2997 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2998 | |
| 2999 | /* Decode via the codec registry */ |
| 3000 | v = PyCodec_Decode(unicode, encoding, errors); |
| 3001 | if (v == NULL) |
| 3002 | goto onError; |
| 3003 | if (!PyUnicode_Check(v)) { |
| 3004 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3005 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3006 | Py_TYPE(v)->tp_name); |
| 3007 | Py_DECREF(v); |
| 3008 | goto onError; |
| 3009 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3010 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3011 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3012 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3013 | return NULL; |
| 3014 | } |
| 3015 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3016 | PyObject * |
| 3017 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3018 | Py_ssize_t size, |
| 3019 | const char *encoding, |
| 3020 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3021 | { |
| 3022 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3023 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3024 | unicode = PyUnicode_FromUnicode(s, size); |
| 3025 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3026 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3027 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3028 | Py_DECREF(unicode); |
| 3029 | return v; |
| 3030 | } |
| 3031 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3032 | PyObject * |
| 3033 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3034 | const char *encoding, |
| 3035 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3036 | { |
| 3037 | PyObject *v; |
| 3038 | |
| 3039 | if (!PyUnicode_Check(unicode)) { |
| 3040 | PyErr_BadArgument(); |
| 3041 | goto onError; |
| 3042 | } |
| 3043 | |
| 3044 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3045 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3046 | |
| 3047 | /* Encode via the codec registry */ |
| 3048 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3049 | if (v == NULL) |
| 3050 | goto onError; |
| 3051 | return v; |
| 3052 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3053 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3054 | return NULL; |
| 3055 | } |
| 3056 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3057 | static size_t |
| 3058 | wcstombs_errorpos(const wchar_t *wstr) |
| 3059 | { |
| 3060 | size_t len; |
| 3061 | #if SIZEOF_WCHAR_T == 2 |
| 3062 | wchar_t buf[3]; |
| 3063 | #else |
| 3064 | wchar_t buf[2]; |
| 3065 | #endif |
| 3066 | char outbuf[MB_LEN_MAX]; |
| 3067 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3068 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3069 | #if SIZEOF_WCHAR_T == 2 |
| 3070 | buf[2] = 0; |
| 3071 | #else |
| 3072 | buf[1] = 0; |
| 3073 | #endif |
| 3074 | start = wstr; |
| 3075 | while (*wstr != L'\0') |
| 3076 | { |
| 3077 | previous = wstr; |
| 3078 | #if SIZEOF_WCHAR_T == 2 |
| 3079 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3080 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3081 | { |
| 3082 | buf[0] = wstr[0]; |
| 3083 | buf[1] = wstr[1]; |
| 3084 | wstr += 2; |
| 3085 | } |
| 3086 | else { |
| 3087 | buf[0] = *wstr; |
| 3088 | buf[1] = 0; |
| 3089 | wstr++; |
| 3090 | } |
| 3091 | #else |
| 3092 | buf[0] = *wstr; |
| 3093 | wstr++; |
| 3094 | #endif |
| 3095 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3096 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3097 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3101 | return 0; |
| 3102 | } |
| 3103 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3104 | static int |
| 3105 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3106 | { |
| 3107 | if (errors == NULL) { |
| 3108 | *surrogateescape = 0; |
| 3109 | return 0; |
| 3110 | } |
| 3111 | |
| 3112 | if (strcmp(errors, "strict") == 0) { |
| 3113 | *surrogateescape = 0; |
| 3114 | return 0; |
| 3115 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3116 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3117 | *surrogateescape = 1; |
| 3118 | return 0; |
| 3119 | } |
| 3120 | PyErr_Format(PyExc_ValueError, |
| 3121 | "only 'strict' and 'surrogateescape' error handlers " |
| 3122 | "are supported, not '%s'", |
| 3123 | errors); |
| 3124 | return -1; |
| 3125 | } |
| 3126 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3127 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3128 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3129 | { |
| 3130 | Py_ssize_t wlen, wlen2; |
| 3131 | wchar_t *wstr; |
| 3132 | PyObject *bytes = NULL; |
| 3133 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3134 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3135 | PyObject *exc; |
| 3136 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3137 | int surrogateescape; |
| 3138 | |
| 3139 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3140 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3141 | |
| 3142 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3143 | if (wstr == NULL) |
| 3144 | return NULL; |
| 3145 | |
| 3146 | wlen2 = wcslen(wstr); |
| 3147 | if (wlen2 != wlen) { |
| 3148 | PyMem_Free(wstr); |
| 3149 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3150 | return NULL; |
| 3151 | } |
| 3152 | |
| 3153 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3154 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3155 | char *str; |
| 3156 | |
| 3157 | str = _Py_wchar2char(wstr, &error_pos); |
| 3158 | if (str == NULL) { |
| 3159 | if (error_pos == (size_t)-1) { |
| 3160 | PyErr_NoMemory(); |
| 3161 | PyMem_Free(wstr); |
| 3162 | return NULL; |
| 3163 | } |
| 3164 | else { |
| 3165 | goto encode_error; |
| 3166 | } |
| 3167 | } |
| 3168 | PyMem_Free(wstr); |
| 3169 | |
| 3170 | bytes = PyBytes_FromString(str); |
| 3171 | PyMem_Free(str); |
| 3172 | } |
| 3173 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3174 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3175 | size_t len, len2; |
| 3176 | |
| 3177 | len = wcstombs(NULL, wstr, 0); |
| 3178 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3179 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3180 | goto encode_error; |
| 3181 | } |
| 3182 | |
| 3183 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3184 | if (bytes == NULL) { |
| 3185 | PyMem_Free(wstr); |
| 3186 | return NULL; |
| 3187 | } |
| 3188 | |
| 3189 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3190 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3191 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3192 | goto encode_error; |
| 3193 | } |
| 3194 | PyMem_Free(wstr); |
| 3195 | } |
| 3196 | return bytes; |
| 3197 | |
| 3198 | encode_error: |
| 3199 | errmsg = strerror(errno); |
| 3200 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3201 | |
| 3202 | if (error_pos == (size_t)-1) |
| 3203 | error_pos = wcstombs_errorpos(wstr); |
| 3204 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3205 | PyMem_Free(wstr); |
| 3206 | Py_XDECREF(bytes); |
| 3207 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3208 | if (errmsg != NULL) { |
| 3209 | size_t errlen; |
| 3210 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3211 | if (wstr != NULL) { |
| 3212 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3213 | PyMem_Free(wstr); |
| 3214 | } else |
| 3215 | errmsg = NULL; |
| 3216 | } |
| 3217 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3218 | reason = PyUnicode_FromString( |
| 3219 | "wcstombs() encountered an unencodable " |
| 3220 | "wide character"); |
| 3221 | if (reason == NULL) |
| 3222 | return NULL; |
| 3223 | |
| 3224 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3225 | "locale", unicode, |
| 3226 | (Py_ssize_t)error_pos, |
| 3227 | (Py_ssize_t)(error_pos+1), |
| 3228 | reason); |
| 3229 | Py_DECREF(reason); |
| 3230 | if (exc != NULL) { |
| 3231 | PyCodec_StrictErrors(exc); |
| 3232 | Py_XDECREF(exc); |
| 3233 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3234 | return NULL; |
| 3235 | } |
| 3236 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3237 | PyObject * |
| 3238 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3239 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3240 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3241 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3242 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3243 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3244 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3245 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3246 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3247 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3248 | the Python codec requires to encode at least its own filename. Use the C |
| 3249 | version of the locale codec until the codec registry is initialized and |
| 3250 | the Python codec is loaded. |
| 3251 | |
| 3252 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3253 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3254 | subinterpreters. */ |
| 3255 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3256 | return PyUnicode_AsEncodedString(unicode, |
| 3257 | Py_FileSystemDefaultEncoding, |
| 3258 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3259 | } |
| 3260 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3261 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3262 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3263 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3266 | PyObject * |
| 3267 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3268 | const char *encoding, |
| 3269 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3270 | { |
| 3271 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3272 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3273 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3274 | if (!PyUnicode_Check(unicode)) { |
| 3275 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3276 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3277 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3278 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3279 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3280 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3281 | if ((strcmp(lower, "utf-8") == 0) || |
| 3282 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3283 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3284 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3285 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3286 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3287 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3288 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3289 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3290 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3291 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3292 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3293 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3294 | else if (strcmp(lower, "mbcs") == 0) |
| 3295 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3296 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3297 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3298 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3299 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3300 | |
| 3301 | /* Encode via the codec registry */ |
| 3302 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3303 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3304 | return NULL; |
| 3305 | |
| 3306 | /* The normal path */ |
| 3307 | if (PyBytes_Check(v)) |
| 3308 | return v; |
| 3309 | |
| 3310 | /* 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] | 3311 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3312 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3313 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3314 | |
| 3315 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3316 | "encoder %s returned bytearray instead of bytes", |
| 3317 | encoding); |
| 3318 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3319 | Py_DECREF(v); |
| 3320 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3321 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3322 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3323 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3324 | Py_DECREF(v); |
| 3325 | return b; |
| 3326 | } |
| 3327 | |
| 3328 | PyErr_Format(PyExc_TypeError, |
| 3329 | "encoder did not return a bytes object (type=%.400s)", |
| 3330 | Py_TYPE(v)->tp_name); |
| 3331 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3332 | return NULL; |
| 3333 | } |
| 3334 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3335 | PyObject * |
| 3336 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3337 | const char *encoding, |
| 3338 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3339 | { |
| 3340 | PyObject *v; |
| 3341 | |
| 3342 | if (!PyUnicode_Check(unicode)) { |
| 3343 | PyErr_BadArgument(); |
| 3344 | goto onError; |
| 3345 | } |
| 3346 | |
| 3347 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3348 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3349 | |
| 3350 | /* Encode via the codec registry */ |
| 3351 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3352 | if (v == NULL) |
| 3353 | goto onError; |
| 3354 | if (!PyUnicode_Check(v)) { |
| 3355 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3356 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3357 | Py_TYPE(v)->tp_name); |
| 3358 | Py_DECREF(v); |
| 3359 | goto onError; |
| 3360 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3361 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3362 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3363 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3364 | return NULL; |
| 3365 | } |
| 3366 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3367 | static size_t |
| 3368 | mbstowcs_errorpos(const char *str, size_t len) |
| 3369 | { |
| 3370 | #ifdef HAVE_MBRTOWC |
| 3371 | const char *start = str; |
| 3372 | mbstate_t mbs; |
| 3373 | size_t converted; |
| 3374 | wchar_t ch; |
| 3375 | |
| 3376 | memset(&mbs, 0, sizeof mbs); |
| 3377 | while (len) |
| 3378 | { |
| 3379 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3380 | if (converted == 0) |
| 3381 | /* Reached end of string */ |
| 3382 | break; |
| 3383 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3384 | /* Conversion error or incomplete character */ |
| 3385 | return str - start; |
| 3386 | } |
| 3387 | else { |
| 3388 | str += converted; |
| 3389 | len -= converted; |
| 3390 | } |
| 3391 | } |
| 3392 | /* failed to find the undecodable byte sequence */ |
| 3393 | return 0; |
| 3394 | #endif |
| 3395 | return 0; |
| 3396 | } |
| 3397 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3398 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3399 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3400 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3401 | { |
| 3402 | wchar_t smallbuf[256]; |
| 3403 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3404 | wchar_t *wstr; |
| 3405 | size_t wlen, wlen2; |
| 3406 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3407 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3408 | size_t error_pos; |
| 3409 | char *errmsg; |
| 3410 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3411 | |
| 3412 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3413 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3414 | |
| 3415 | if (str[len] != '\0' || len != strlen(str)) { |
| 3416 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3417 | return NULL; |
| 3418 | } |
| 3419 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3420 | if (surrogateescape) { |
| 3421 | /* "surrogateescape" error handler */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3422 | wstr = _Py_char2wchar(str, &wlen); |
| 3423 | if (wstr == NULL) { |
| 3424 | if (wlen == (size_t)-1) |
| 3425 | PyErr_NoMemory(); |
| 3426 | else |
| 3427 | PyErr_SetFromErrno(PyExc_OSError); |
| 3428 | return NULL; |
| 3429 | } |
| 3430 | |
| 3431 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3432 | PyMem_Free(wstr); |
| 3433 | } |
| 3434 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3435 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3436 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3437 | wlen = mbstowcs(NULL, str, 0); |
| 3438 | #else |
| 3439 | wlen = len; |
| 3440 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3441 | if (wlen == (size_t)-1) |
| 3442 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3443 | if (wlen+1 <= smallbuf_len) { |
| 3444 | wstr = smallbuf; |
| 3445 | } |
| 3446 | else { |
| 3447 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3448 | return PyErr_NoMemory(); |
| 3449 | |
| 3450 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3451 | if (!wstr) |
| 3452 | return PyErr_NoMemory(); |
| 3453 | } |
| 3454 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3455 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3456 | if (wlen2 == (size_t)-1) { |
| 3457 | if (wstr != smallbuf) |
| 3458 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3459 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3460 | } |
| 3461 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3462 | assert(wlen2 == wlen); |
| 3463 | #endif |
| 3464 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3465 | if (wstr != smallbuf) |
| 3466 | PyMem_Free(wstr); |
| 3467 | } |
| 3468 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3469 | |
| 3470 | decode_error: |
| 3471 | errmsg = strerror(errno); |
| 3472 | assert(errmsg != NULL); |
| 3473 | |
| 3474 | error_pos = mbstowcs_errorpos(str, len); |
| 3475 | if (errmsg != NULL) { |
| 3476 | size_t errlen; |
| 3477 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3478 | if (wstr != NULL) { |
| 3479 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3480 | PyMem_Free(wstr); |
| 3481 | } else |
| 3482 | errmsg = NULL; |
| 3483 | } |
| 3484 | if (errmsg == NULL) |
| 3485 | reason = PyUnicode_FromString( |
| 3486 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3487 | if (reason == NULL) |
| 3488 | return NULL; |
| 3489 | |
| 3490 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3491 | "locale", str, len, |
| 3492 | (Py_ssize_t)error_pos, |
| 3493 | (Py_ssize_t)(error_pos+1), |
| 3494 | reason); |
| 3495 | Py_DECREF(reason); |
| 3496 | if (exc != NULL) { |
| 3497 | PyCodec_StrictErrors(exc); |
| 3498 | Py_XDECREF(exc); |
| 3499 | } |
| 3500 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3504 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3505 | { |
| 3506 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3507 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | |
| 3511 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3512 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3513 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3514 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3515 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3516 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3517 | PyObject* |
| 3518 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3519 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3520 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3521 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3522 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3523 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3524 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3525 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3526 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3527 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3528 | the Python codec requires to encode at least its own filename. Use the C |
| 3529 | version of the locale codec until the codec registry is initialized and |
| 3530 | the Python codec is loaded. |
| 3531 | |
| 3532 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3533 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3534 | subinterpreters. */ |
| 3535 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3536 | return PyUnicode_Decode(s, size, |
| 3537 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3538 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3539 | } |
| 3540 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3541 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3542 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3543 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3546 | |
| 3547 | int |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3548 | _PyUnicode_HasNULChars(PyObject* str) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3549 | { |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3550 | Py_ssize_t pos; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3551 | |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3552 | if (PyUnicode_READY(str) == -1) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3553 | return -1; |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3554 | pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), |
| 3555 | PyUnicode_GET_LENGTH(str), '\0', 1); |
| 3556 | if (pos == -1) |
| 3557 | return 0; |
| 3558 | else |
| 3559 | return 1; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3560 | } |
| 3561 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3562 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3563 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3564 | { |
| 3565 | PyObject *output = NULL; |
| 3566 | Py_ssize_t size; |
| 3567 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3568 | if (arg == NULL) { |
| 3569 | Py_DECREF(*(PyObject**)addr); |
| 3570 | return 1; |
| 3571 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3572 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3573 | output = arg; |
| 3574 | Py_INCREF(output); |
| 3575 | } |
| 3576 | else { |
| 3577 | arg = PyUnicode_FromObject(arg); |
| 3578 | if (!arg) |
| 3579 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3580 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3581 | Py_DECREF(arg); |
| 3582 | if (!output) |
| 3583 | return 0; |
| 3584 | if (!PyBytes_Check(output)) { |
| 3585 | Py_DECREF(output); |
| 3586 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3587 | return 0; |
| 3588 | } |
| 3589 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3590 | size = PyBytes_GET_SIZE(output); |
| 3591 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3592 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3593 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3594 | Py_DECREF(output); |
| 3595 | return 0; |
| 3596 | } |
| 3597 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3598 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3602 | int |
| 3603 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3604 | { |
| 3605 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3606 | if (arg == NULL) { |
| 3607 | Py_DECREF(*(PyObject**)addr); |
| 3608 | return 1; |
| 3609 | } |
| 3610 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3611 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3612 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3613 | output = arg; |
| 3614 | Py_INCREF(output); |
| 3615 | } |
| 3616 | else { |
| 3617 | arg = PyBytes_FromObject(arg); |
| 3618 | if (!arg) |
| 3619 | return 0; |
| 3620 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3621 | PyBytes_GET_SIZE(arg)); |
| 3622 | Py_DECREF(arg); |
| 3623 | if (!output) |
| 3624 | return 0; |
| 3625 | if (!PyUnicode_Check(output)) { |
| 3626 | Py_DECREF(output); |
| 3627 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3628 | return 0; |
| 3629 | } |
| 3630 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3631 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3632 | Py_DECREF(output); |
| 3633 | return 0; |
| 3634 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3635 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3636 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3637 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3638 | Py_DECREF(output); |
| 3639 | return 0; |
| 3640 | } |
| 3641 | *(PyObject**)addr = output; |
| 3642 | return Py_CLEANUP_SUPPORTED; |
| 3643 | } |
| 3644 | |
| 3645 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3646 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3647 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3648 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3649 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3650 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3651 | if (!PyUnicode_Check(unicode)) { |
| 3652 | PyErr_BadArgument(); |
| 3653 | return NULL; |
| 3654 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3655 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3656 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3657 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3658 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3659 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3660 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3661 | if (bytes == NULL) |
| 3662 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3663 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3664 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3665 | Py_DECREF(bytes); |
| 3666 | return NULL; |
| 3667 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3668 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3669 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3670 | PyBytes_AS_STRING(bytes), |
| 3671 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3672 | Py_DECREF(bytes); |
| 3673 | } |
| 3674 | |
| 3675 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3676 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3677 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3681 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3682 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3683 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3684 | } |
| 3685 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3686 | Py_UNICODE * |
| 3687 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3688 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3689 | const unsigned char *one_byte; |
| 3690 | #if SIZEOF_WCHAR_T == 4 |
| 3691 | const Py_UCS2 *two_bytes; |
| 3692 | #else |
| 3693 | const Py_UCS4 *four_bytes; |
| 3694 | const Py_UCS4 *ucs4_end; |
| 3695 | Py_ssize_t num_surrogates; |
| 3696 | #endif |
| 3697 | wchar_t *w; |
| 3698 | wchar_t *wchar_end; |
| 3699 | |
| 3700 | if (!PyUnicode_Check(unicode)) { |
| 3701 | PyErr_BadArgument(); |
| 3702 | return NULL; |
| 3703 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3704 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3705 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3706 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3707 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3708 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3709 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3710 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3711 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3712 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3713 | num_surrogates = 0; |
| 3714 | |
| 3715 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3716 | if (*four_bytes > 0xFFFF) |
| 3717 | ++num_surrogates; |
| 3718 | } |
| 3719 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3720 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3721 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3722 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3723 | PyErr_NoMemory(); |
| 3724 | return NULL; |
| 3725 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3726 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3727 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3728 | w = _PyUnicode_WSTR(unicode); |
| 3729 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3730 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3731 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3732 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3733 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3734 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3735 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3736 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3737 | } |
| 3738 | else |
| 3739 | *w = *four_bytes; |
| 3740 | |
| 3741 | if (w > wchar_end) { |
| 3742 | assert(0 && "Miscalculated string end"); |
| 3743 | } |
| 3744 | } |
| 3745 | *w = 0; |
| 3746 | #else |
| 3747 | /* sizeof(wchar_t) == 4 */ |
| 3748 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3749 | "should share memory already."); |
| 3750 | return NULL; |
| 3751 | #endif |
| 3752 | } |
| 3753 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3754 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3755 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3756 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3757 | PyErr_NoMemory(); |
| 3758 | return NULL; |
| 3759 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3760 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3761 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3762 | w = _PyUnicode_WSTR(unicode); |
| 3763 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3764 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3765 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3766 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3767 | for (; w < wchar_end; ++one_byte, ++w) |
| 3768 | *w = *one_byte; |
| 3769 | /* null-terminate the wstr */ |
| 3770 | *w = 0; |
| 3771 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3772 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3773 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3774 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3775 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3776 | *w = *two_bytes; |
| 3777 | /* null-terminate the wstr */ |
| 3778 | *w = 0; |
| 3779 | #else |
| 3780 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3781 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3782 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3783 | Py_FatalError("Impossible unicode object state, wstr " |
| 3784 | "and str should share memory already."); |
| 3785 | return NULL; |
| 3786 | #endif |
| 3787 | } |
| 3788 | else { |
| 3789 | assert(0 && "This should never happen."); |
| 3790 | } |
| 3791 | } |
| 3792 | } |
| 3793 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3794 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3795 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3798 | Py_UNICODE * |
| 3799 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3800 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3801 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3804 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3805 | Py_ssize_t |
| 3806 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3807 | { |
| 3808 | if (!PyUnicode_Check(unicode)) { |
| 3809 | PyErr_BadArgument(); |
| 3810 | goto onError; |
| 3811 | } |
| 3812 | return PyUnicode_GET_SIZE(unicode); |
| 3813 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3814 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3815 | return -1; |
| 3816 | } |
| 3817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3818 | Py_ssize_t |
| 3819 | PyUnicode_GetLength(PyObject *unicode) |
| 3820 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3821 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3822 | PyErr_BadArgument(); |
| 3823 | return -1; |
| 3824 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3825 | if (PyUnicode_READY(unicode) == -1) |
| 3826 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3827 | return PyUnicode_GET_LENGTH(unicode); |
| 3828 | } |
| 3829 | |
| 3830 | Py_UCS4 |
| 3831 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3832 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3833 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3834 | PyErr_BadArgument(); |
| 3835 | return (Py_UCS4)-1; |
| 3836 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3837 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3838 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3839 | return (Py_UCS4)-1; |
| 3840 | } |
| 3841 | return PyUnicode_READ_CHAR(unicode, index); |
| 3842 | } |
| 3843 | |
| 3844 | int |
| 3845 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3846 | { |
| 3847 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3848 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3849 | return -1; |
| 3850 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3851 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3852 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3853 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3854 | return -1; |
| 3855 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3856 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3857 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3858 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3859 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3860 | return -1; |
| 3861 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3862 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3863 | index, ch); |
| 3864 | return 0; |
| 3865 | } |
| 3866 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3867 | const char * |
| 3868 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3869 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3870 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3873 | /* create or adjust a UnicodeDecodeError */ |
| 3874 | static void |
| 3875 | make_decode_exception(PyObject **exceptionObject, |
| 3876 | const char *encoding, |
| 3877 | const char *input, Py_ssize_t length, |
| 3878 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3879 | const char *reason) |
| 3880 | { |
| 3881 | if (*exceptionObject == NULL) { |
| 3882 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3883 | encoding, input, length, startpos, endpos, reason); |
| 3884 | } |
| 3885 | else { |
| 3886 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3887 | goto onError; |
| 3888 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3889 | goto onError; |
| 3890 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3891 | goto onError; |
| 3892 | } |
| 3893 | return; |
| 3894 | |
| 3895 | onError: |
| 3896 | Py_DECREF(*exceptionObject); |
| 3897 | *exceptionObject = NULL; |
| 3898 | } |
| 3899 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3900 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3901 | /* error handling callback helper: |
| 3902 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3903 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3904 | and adjust various state variables. |
| 3905 | return 0 on success, -1 on error |
| 3906 | */ |
| 3907 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3908 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3909 | unicode_decode_call_errorhandler_wchar( |
| 3910 | const char *errors, PyObject **errorHandler, |
| 3911 | const char *encoding, const char *reason, |
| 3912 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3913 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3914 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3915 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3916 | 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] | 3917 | |
| 3918 | PyObject *restuple = NULL; |
| 3919 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3920 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3921 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3922 | Py_ssize_t requiredsize; |
| 3923 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3924 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3925 | wchar_t *repwstr; |
| 3926 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3927 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3928 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 3929 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3930 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3931 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3932 | *errorHandler = PyCodec_LookupError(errors); |
| 3933 | if (*errorHandler == NULL) |
| 3934 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3937 | make_decode_exception(exceptionObject, |
| 3938 | encoding, |
| 3939 | *input, *inend - *input, |
| 3940 | *startinpos, *endinpos, |
| 3941 | reason); |
| 3942 | if (*exceptionObject == NULL) |
| 3943 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3944 | |
| 3945 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3946 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3947 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3948 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3949 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3950 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3951 | } |
| 3952 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3953 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3954 | |
| 3955 | /* Copy back the bytes variables, which might have been modified by the |
| 3956 | callback */ |
| 3957 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3958 | if (!inputobj) |
| 3959 | goto onError; |
| 3960 | if (!PyBytes_Check(inputobj)) { |
| 3961 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 3962 | } |
| 3963 | *input = PyBytes_AS_STRING(inputobj); |
| 3964 | insize = PyBytes_GET_SIZE(inputobj); |
| 3965 | *inend = *input + insize; |
| 3966 | /* we can DECREF safely, as the exception has another reference, |
| 3967 | so the object won't go away. */ |
| 3968 | Py_DECREF(inputobj); |
| 3969 | |
| 3970 | if (newpos<0) |
| 3971 | newpos = insize+newpos; |
| 3972 | if (newpos<0 || newpos>insize) { |
| 3973 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3974 | goto onError; |
| 3975 | } |
| 3976 | |
| 3977 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 3978 | if (repwstr == NULL) |
| 3979 | goto onError; |
| 3980 | /* need more space? (at least enough for what we |
| 3981 | have+the replacement+the rest of the string (starting |
| 3982 | at the new input position), so we won't have to check space |
| 3983 | when there are no errors in the rest of the string) */ |
| 3984 | requiredsize = *outpos + repwlen + insize-newpos; |
| 3985 | if (requiredsize > outsize) { |
| 3986 | if (requiredsize < 2*outsize) |
| 3987 | requiredsize = 2*outsize; |
| 3988 | if (unicode_resize(output, requiredsize) < 0) |
| 3989 | goto onError; |
| 3990 | } |
| 3991 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 3992 | *outpos += repwlen; |
| 3993 | |
| 3994 | *endinpos = newpos; |
| 3995 | *inptr = *input + newpos; |
| 3996 | |
| 3997 | /* we made it! */ |
| 3998 | Py_XDECREF(restuple); |
| 3999 | return 0; |
| 4000 | |
| 4001 | onError: |
| 4002 | Py_XDECREF(restuple); |
| 4003 | return -1; |
| 4004 | } |
| 4005 | #endif /* HAVE_MBCS */ |
| 4006 | |
| 4007 | static int |
| 4008 | unicode_decode_call_errorhandler_writer( |
| 4009 | const char *errors, PyObject **errorHandler, |
| 4010 | const char *encoding, const char *reason, |
| 4011 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4012 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4013 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4014 | { |
| 4015 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4016 | |
| 4017 | PyObject *restuple = NULL; |
| 4018 | PyObject *repunicode = NULL; |
| 4019 | Py_ssize_t insize; |
| 4020 | Py_ssize_t newpos; |
| 4021 | PyObject *inputobj = NULL; |
| 4022 | |
| 4023 | if (*errorHandler == NULL) { |
| 4024 | *errorHandler = PyCodec_LookupError(errors); |
| 4025 | if (*errorHandler == NULL) |
| 4026 | goto onError; |
| 4027 | } |
| 4028 | |
| 4029 | make_decode_exception(exceptionObject, |
| 4030 | encoding, |
| 4031 | *input, *inend - *input, |
| 4032 | *startinpos, *endinpos, |
| 4033 | reason); |
| 4034 | if (*exceptionObject == NULL) |
| 4035 | goto onError; |
| 4036 | |
| 4037 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4038 | if (restuple == NULL) |
| 4039 | goto onError; |
| 4040 | if (!PyTuple_Check(restuple)) { |
| 4041 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4042 | goto onError; |
| 4043 | } |
| 4044 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4045 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4046 | |
| 4047 | /* Copy back the bytes variables, which might have been modified by the |
| 4048 | callback */ |
| 4049 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4050 | if (!inputobj) |
| 4051 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4052 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4053 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4054 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4055 | *input = PyBytes_AS_STRING(inputobj); |
| 4056 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4057 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4058 | /* we can DECREF safely, as the exception has another reference, |
| 4059 | so the object won't go away. */ |
| 4060 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4061 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4062 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4063 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4064 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4065 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4066 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4067 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4068 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4069 | writer->overallocate = 1; |
| 4070 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
| 4071 | return |
| 4072 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4073 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4074 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4075 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4076 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4077 | Py_XDECREF(restuple); |
| 4078 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4079 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4080 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4081 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4082 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4085 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4086 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4087 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4088 | |
| 4089 | /* Three simple macros defining base-64. */ |
| 4090 | |
| 4091 | /* Is c a base-64 character? */ |
| 4092 | |
| 4093 | #define IS_BASE64(c) \ |
| 4094 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4095 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4096 | ((c) >= '0' && (c) <= '9') || \ |
| 4097 | (c) == '+' || (c) == '/') |
| 4098 | |
| 4099 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4100 | |
| 4101 | #define FROM_BASE64(c) \ |
| 4102 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4103 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4104 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4105 | (c) == '+' ? 62 : 63) |
| 4106 | |
| 4107 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4108 | |
| 4109 | #define TO_BASE64(n) \ |
| 4110 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4111 | |
| 4112 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4113 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4114 | * byte not decoding to itself is the + which begins a base64 |
| 4115 | * string. */ |
| 4116 | |
| 4117 | #define DECODE_DIRECT(c) \ |
| 4118 | ((c) <= 127 && (c) != '+') |
| 4119 | |
| 4120 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4121 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4122 | * the above). See RFC2152. This array identifies these different |
| 4123 | * sets: |
| 4124 | * 0 : "Set D" |
| 4125 | * alphanumeric and '(),-./:? |
| 4126 | * 1 : "Set O" |
| 4127 | * !"#$%&*;<=>@[]^_`{|} |
| 4128 | * 2 : "whitespace" |
| 4129 | * ht nl cr sp |
| 4130 | * 3 : special (must be base64 encoded) |
| 4131 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4132 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4133 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4134 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4135 | char utf7_category[128] = { |
| 4136 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4137 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4138 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4139 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4140 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4141 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4142 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4143 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4144 | /* @ A B C D E F G H I J K L M N O */ |
| 4145 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4146 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4147 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4148 | /* ` a b c d e f g h i j k l m n o */ |
| 4149 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4150 | /* p q r s t u v w x y z { | } ~ del */ |
| 4151 | 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] | 4152 | }; |
| 4153 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4154 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4155 | * answer depends on whether we are encoding set O as itself, and also |
| 4156 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4157 | * clear that the answers to these questions vary between |
| 4158 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4159 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4160 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4161 | ((c) < 128 && (c) > 0 && \ |
| 4162 | ((utf7_category[(c)] == 0) || \ |
| 4163 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4164 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4165 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4166 | PyObject * |
| 4167 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4168 | Py_ssize_t size, |
| 4169 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4170 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4171 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4172 | } |
| 4173 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4174 | /* The decoder. The only state we preserve is our read position, |
| 4175 | * i.e. how many characters we have consumed. So if we end in the |
| 4176 | * middle of a shift sequence we have to back off the read position |
| 4177 | * and the output to the beginning of the sequence, otherwise we lose |
| 4178 | * all the shift state (seen bits, number of bits seen, high |
| 4179 | * surrogate). */ |
| 4180 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4181 | PyObject * |
| 4182 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4183 | Py_ssize_t size, |
| 4184 | const char *errors, |
| 4185 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4186 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4187 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4188 | Py_ssize_t startinpos; |
| 4189 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4190 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4191 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4192 | const char *errmsg = ""; |
| 4193 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4194 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4195 | unsigned int base64bits = 0; |
| 4196 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4197 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4198 | PyObject *errorHandler = NULL; |
| 4199 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4200 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4201 | if (size == 0) { |
| 4202 | if (consumed) |
| 4203 | *consumed = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4204 | Py_INCREF(unicode_empty); |
| 4205 | return unicode_empty; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4206 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4207 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4208 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4209 | _PyUnicodeWriter_Init(&writer, 0); |
| 4210 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
| 4211 | goto onError; |
| 4212 | |
| 4213 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4214 | e = s + size; |
| 4215 | |
| 4216 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4217 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4218 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4219 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4220 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4221 | if (inShift) { /* in a base-64 section */ |
| 4222 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4223 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4224 | base64bits += 6; |
| 4225 | s++; |
| 4226 | if (base64bits >= 16) { |
| 4227 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4228 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4229 | base64bits -= 16; |
| 4230 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4231 | if (surrogate) { |
| 4232 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4233 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4234 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4235 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch2) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4236 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4237 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch2); |
| 4238 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4239 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4240 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4241 | } |
| 4242 | else { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4243 | if (_PyUnicodeWriter_Prepare(&writer, 1, surrogate) == -1) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4244 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4245 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, surrogate); |
| 4246 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4247 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4248 | } |
| 4249 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4250 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4251 | /* first surrogate */ |
| 4252 | surrogate = outCh; |
| 4253 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4254 | else { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4255 | if (_PyUnicodeWriter_Prepare(&writer, 1, outCh) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4256 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4257 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, outCh); |
| 4258 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4259 | } |
| 4260 | } |
| 4261 | } |
| 4262 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4263 | inShift = 0; |
| 4264 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4265 | if (surrogate) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4266 | if (_PyUnicodeWriter_Prepare(&writer, 1, surrogate) == -1) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4267 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4268 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, surrogate); |
| 4269 | writer.pos++; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4270 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4271 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4272 | if (base64bits > 0) { /* left-over bits */ |
| 4273 | if (base64bits >= 6) { |
| 4274 | /* We've seen at least one base-64 character */ |
| 4275 | errmsg = "partial character in shift sequence"; |
| 4276 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4277 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4278 | else { |
| 4279 | /* Some bits remain; they should be zero */ |
| 4280 | if (base64buffer != 0) { |
| 4281 | errmsg = "non-zero padding bits in shift sequence"; |
| 4282 | goto utf7Error; |
| 4283 | } |
| 4284 | } |
| 4285 | } |
| 4286 | if (ch != '-') { |
| 4287 | /* '-' is absorbed; other terminating |
| 4288 | characters are preserved */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4289 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4290 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4291 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4292 | writer.pos++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4293 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4294 | } |
| 4295 | } |
| 4296 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4297 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4298 | s++; /* consume '+' */ |
| 4299 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4300 | s++; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4301 | if (_PyUnicodeWriter_Prepare(&writer, 1, '+') == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4302 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4303 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '+'); |
| 4304 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4305 | } |
| 4306 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4307 | inShift = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4308 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4309 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4310 | } |
| 4311 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4312 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4313 | s++; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4314 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
| 4315 | goto onError; |
| 4316 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4317 | writer.pos++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4318 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4319 | else { |
| 4320 | startinpos = s-starts; |
| 4321 | s++; |
| 4322 | errmsg = "unexpected special character"; |
| 4323 | goto utf7Error; |
| 4324 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4325 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4326 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4327 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4328 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4329 | errors, &errorHandler, |
| 4330 | "utf7", errmsg, |
| 4331 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4332 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4333 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4334 | } |
| 4335 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4336 | /* end of string */ |
| 4337 | |
| 4338 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4339 | /* if we're in an inconsistent state, that's an error */ |
| 4340 | if (surrogate || |
| 4341 | (base64bits >= 6) || |
| 4342 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4343 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4344 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4345 | errors, &errorHandler, |
| 4346 | "utf7", "unterminated shift sequence", |
| 4347 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4348 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4349 | goto onError; |
| 4350 | if (s < e) |
| 4351 | goto restart; |
| 4352 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4353 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4354 | |
| 4355 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4356 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4357 | if (inShift) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4358 | writer.pos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4359 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4360 | } |
| 4361 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4362 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4363 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4364 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4365 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4366 | Py_XDECREF(errorHandler); |
| 4367 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4368 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4369 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4370 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4371 | Py_XDECREF(errorHandler); |
| 4372 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4373 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4374 | return NULL; |
| 4375 | } |
| 4376 | |
| 4377 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4378 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4379 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4380 | int base64SetO, |
| 4381 | int base64WhiteSpace, |
| 4382 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4383 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4384 | int kind; |
| 4385 | void *data; |
| 4386 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4387 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4388 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4389 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4390 | unsigned int base64bits = 0; |
| 4391 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4392 | char * out; |
| 4393 | char * start; |
| 4394 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4395 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4396 | return NULL; |
| 4397 | kind = PyUnicode_KIND(str); |
| 4398 | data = PyUnicode_DATA(str); |
| 4399 | len = PyUnicode_GET_LENGTH(str); |
| 4400 | |
| 4401 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4402 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4403 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4404 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4405 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4406 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4407 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4408 | if (v == NULL) |
| 4409 | return NULL; |
| 4410 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4411 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4412 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4413 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4414 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4415 | if (inShift) { |
| 4416 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4417 | /* shifting out */ |
| 4418 | if (base64bits) { /* output remaining bits */ |
| 4419 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4420 | base64buffer = 0; |
| 4421 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4422 | } |
| 4423 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4424 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4425 | so no '-' is required, except if the character is itself a '-' */ |
| 4426 | if (IS_BASE64(ch) || ch == '-') { |
| 4427 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4428 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4429 | *out++ = (char) ch; |
| 4430 | } |
| 4431 | else { |
| 4432 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4433 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4434 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4435 | else { /* not in a shift sequence */ |
| 4436 | if (ch == '+') { |
| 4437 | *out++ = '+'; |
| 4438 | *out++ = '-'; |
| 4439 | } |
| 4440 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4441 | *out++ = (char) ch; |
| 4442 | } |
| 4443 | else { |
| 4444 | *out++ = '+'; |
| 4445 | inShift = 1; |
| 4446 | goto encode_char; |
| 4447 | } |
| 4448 | } |
| 4449 | continue; |
| 4450 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4451 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4452 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4453 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4454 | /* code first surrogate */ |
| 4455 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4456 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4457 | while (base64bits >= 6) { |
| 4458 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4459 | base64bits -= 6; |
| 4460 | } |
| 4461 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4462 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4463 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4464 | base64bits += 16; |
| 4465 | base64buffer = (base64buffer << 16) | ch; |
| 4466 | while (base64bits >= 6) { |
| 4467 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4468 | base64bits -= 6; |
| 4469 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4470 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4471 | if (base64bits) |
| 4472 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4473 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4474 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4475 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4476 | return NULL; |
| 4477 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4478 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4479 | PyObject * |
| 4480 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4481 | Py_ssize_t size, |
| 4482 | int base64SetO, |
| 4483 | int base64WhiteSpace, |
| 4484 | const char *errors) |
| 4485 | { |
| 4486 | PyObject *result; |
| 4487 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4488 | if (tmp == NULL) |
| 4489 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4490 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4491 | base64WhiteSpace, errors); |
| 4492 | Py_DECREF(tmp); |
| 4493 | return result; |
| 4494 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4495 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4496 | #undef IS_BASE64 |
| 4497 | #undef FROM_BASE64 |
| 4498 | #undef TO_BASE64 |
| 4499 | #undef DECODE_DIRECT |
| 4500 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4501 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4502 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4503 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4504 | PyObject * |
| 4505 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4506 | Py_ssize_t size, |
| 4507 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4508 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4509 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4510 | } |
| 4511 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4512 | #include "stringlib/asciilib.h" |
| 4513 | #include "stringlib/codecs.h" |
| 4514 | #include "stringlib/undef.h" |
| 4515 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4516 | #include "stringlib/ucs1lib.h" |
| 4517 | #include "stringlib/codecs.h" |
| 4518 | #include "stringlib/undef.h" |
| 4519 | |
| 4520 | #include "stringlib/ucs2lib.h" |
| 4521 | #include "stringlib/codecs.h" |
| 4522 | #include "stringlib/undef.h" |
| 4523 | |
| 4524 | #include "stringlib/ucs4lib.h" |
| 4525 | #include "stringlib/codecs.h" |
| 4526 | #include "stringlib/undef.h" |
| 4527 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4528 | /* Mask to quickly check whether a C 'long' contains a |
| 4529 | non-ASCII, UTF8-encoded char. */ |
| 4530 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4531 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4532 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4533 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4534 | #else |
| 4535 | # error C 'long' size should be either 4 or 8! |
| 4536 | #endif |
| 4537 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4538 | static Py_ssize_t |
| 4539 | 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] | 4540 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4541 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4542 | const char *aligned_end = (const char *) _Py_ALIGN_DOWN(end, SIZEOF_LONG); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4543 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4544 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4545 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4546 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4547 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4548 | an explanation. */ |
| 4549 | /* Help register allocation */ |
| 4550 | register const char *_p = p; |
| 4551 | register Py_UCS1 * q = dest; |
| 4552 | while (_p < aligned_end) { |
| 4553 | unsigned long value = *(const unsigned long *) _p; |
| 4554 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4555 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4556 | *((unsigned long *)q) = value; |
| 4557 | _p += SIZEOF_LONG; |
| 4558 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4559 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4560 | p = _p; |
| 4561 | while (p < end) { |
| 4562 | if ((unsigned char)*p & 0x80) |
| 4563 | break; |
| 4564 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4565 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4566 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4567 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4568 | #endif |
| 4569 | while (p < end) { |
| 4570 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4571 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4572 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4573 | /* Help register allocation */ |
| 4574 | register const char *_p = p; |
| 4575 | while (_p < aligned_end) { |
| 4576 | unsigned long value = *(unsigned long *) _p; |
| 4577 | if (value & ASCII_CHAR_MASK) |
| 4578 | break; |
| 4579 | _p += SIZEOF_LONG; |
| 4580 | } |
| 4581 | p = _p; |
| 4582 | if (_p == end) |
| 4583 | break; |
| 4584 | } |
| 4585 | if ((unsigned char)*p & 0x80) |
| 4586 | break; |
| 4587 | ++p; |
| 4588 | } |
| 4589 | memcpy(dest, start, p - start); |
| 4590 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4591 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4592 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4593 | PyObject * |
| 4594 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4595 | Py_ssize_t size, |
| 4596 | const char *errors, |
| 4597 | Py_ssize_t *consumed) |
| 4598 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4599 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4600 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4601 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4602 | |
| 4603 | Py_ssize_t startinpos; |
| 4604 | Py_ssize_t endinpos; |
| 4605 | const char *errmsg = ""; |
| 4606 | PyObject *errorHandler = NULL; |
| 4607 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4608 | |
| 4609 | if (size == 0) { |
| 4610 | if (consumed) |
| 4611 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4612 | Py_INCREF(unicode_empty); |
| 4613 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4614 | } |
| 4615 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4616 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4617 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4618 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4619 | *consumed = 1; |
| 4620 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4621 | } |
| 4622 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4623 | _PyUnicodeWriter_Init(&writer, 0); |
| 4624 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
| 4625 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4626 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4627 | writer.pos = ascii_decode(s, end, writer.data); |
| 4628 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4629 | while (s < end) { |
| 4630 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4631 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4632 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4633 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4634 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4635 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4636 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4637 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4638 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4639 | } else { |
| 4640 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4641 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | switch (ch) { |
| 4645 | case 0: |
| 4646 | if (s == end || consumed) |
| 4647 | goto End; |
| 4648 | errmsg = "unexpected end of data"; |
| 4649 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4650 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4651 | break; |
| 4652 | case 1: |
| 4653 | errmsg = "invalid start byte"; |
| 4654 | startinpos = s - starts; |
| 4655 | endinpos = startinpos + 1; |
| 4656 | break; |
| 4657 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4658 | case 3: |
| 4659 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4660 | errmsg = "invalid continuation byte"; |
| 4661 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4662 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4663 | break; |
| 4664 | default: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4665 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4666 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4667 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4668 | writer.pos++; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4669 | continue; |
| 4670 | } |
| 4671 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4672 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4673 | errors, &errorHandler, |
| 4674 | "utf-8", errmsg, |
| 4675 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4676 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4677 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4678 | } |
| 4679 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4680 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4681 | if (consumed) |
| 4682 | *consumed = s - starts; |
| 4683 | |
| 4684 | Py_XDECREF(errorHandler); |
| 4685 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4686 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4687 | |
| 4688 | onError: |
| 4689 | Py_XDECREF(errorHandler); |
| 4690 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4691 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4692 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4693 | } |
| 4694 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4695 | #ifdef __APPLE__ |
| 4696 | |
| 4697 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4698 | used to decode the command line arguments on Mac OS X. |
| 4699 | |
| 4700 | Return a pointer to a newly allocated wide character string (use |
| 4701 | PyMem_Free() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4702 | |
| 4703 | wchar_t* |
| 4704 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4705 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4706 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4707 | wchar_t *unicode; |
| 4708 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4709 | |
| 4710 | /* Note: size will always be longer than the resulting Unicode |
| 4711 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4712 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4713 | return NULL; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4714 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4715 | if (!unicode) |
| 4716 | return NULL; |
| 4717 | |
| 4718 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4719 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4720 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4721 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4722 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4723 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4724 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4725 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4726 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4727 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4728 | if (ch > 0xFF) { |
| 4729 | #if SIZEOF_WCHAR_T == 4 |
| 4730 | assert(0); |
| 4731 | #else |
| 4732 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4733 | /* compute and append the two surrogates: */ |
| 4734 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4735 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4736 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4737 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4738 | else { |
| 4739 | if (!ch && s == e) |
| 4740 | break; |
| 4741 | /* surrogateescape */ |
| 4742 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4743 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4744 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4745 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4746 | return unicode; |
| 4747 | } |
| 4748 | |
| 4749 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4751 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4752 | |
| 4753 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4754 | and allocate exactly as much space needed at the end. Else allocate the |
| 4755 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4756 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4757 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4758 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4759 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4760 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4761 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4762 | void *data; |
| 4763 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4764 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4765 | if (!PyUnicode_Check(unicode)) { |
| 4766 | PyErr_BadArgument(); |
| 4767 | return NULL; |
| 4768 | } |
| 4769 | |
| 4770 | if (PyUnicode_READY(unicode) == -1) |
| 4771 | return NULL; |
| 4772 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4773 | if (PyUnicode_UTF8(unicode)) |
| 4774 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4775 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4776 | |
| 4777 | kind = PyUnicode_KIND(unicode); |
| 4778 | data = PyUnicode_DATA(unicode); |
| 4779 | size = PyUnicode_GET_LENGTH(unicode); |
| 4780 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4781 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4782 | default: |
| 4783 | assert(0); |
| 4784 | case PyUnicode_1BYTE_KIND: |
| 4785 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4786 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4787 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4788 | case PyUnicode_2BYTE_KIND: |
| 4789 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4790 | case PyUnicode_4BYTE_KIND: |
| 4791 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4792 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4795 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4796 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4797 | Py_ssize_t size, |
| 4798 | const char *errors) |
| 4799 | { |
| 4800 | PyObject *v, *unicode; |
| 4801 | |
| 4802 | unicode = PyUnicode_FromUnicode(s, size); |
| 4803 | if (unicode == NULL) |
| 4804 | return NULL; |
| 4805 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4806 | Py_DECREF(unicode); |
| 4807 | return v; |
| 4808 | } |
| 4809 | |
| 4810 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4811 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4812 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4813 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4816 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4817 | |
| 4818 | PyObject * |
| 4819 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4820 | Py_ssize_t size, |
| 4821 | const char *errors, |
| 4822 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4823 | { |
| 4824 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4825 | } |
| 4826 | |
| 4827 | PyObject * |
| 4828 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4829 | Py_ssize_t size, |
| 4830 | const char *errors, |
| 4831 | int *byteorder, |
| 4832 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4833 | { |
| 4834 | const char *starts = s; |
| 4835 | Py_ssize_t startinpos; |
| 4836 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4837 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4838 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4839 | int le, bo = 0; /* assume native ordering by default */ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4840 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4841 | PyObject *errorHandler = NULL; |
| 4842 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4843 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4844 | q = (unsigned char *)s; |
| 4845 | e = q + size; |
| 4846 | |
| 4847 | if (byteorder) |
| 4848 | bo = *byteorder; |
| 4849 | |
| 4850 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4851 | byte order setting accordingly. In native mode, the leading BOM |
| 4852 | mark is skipped, in all other modes, it is copied to the output |
| 4853 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4854 | if (bo == 0 && size >= 4) { |
| 4855 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4856 | if (bom == 0x0000FEFF) { |
| 4857 | bo = -1; |
| 4858 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4859 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4860 | else if (bom == 0xFFFE0000) { |
| 4861 | bo = 1; |
| 4862 | q += 4; |
| 4863 | } |
| 4864 | if (byteorder) |
| 4865 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4866 | } |
| 4867 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4868 | if (q == e) { |
| 4869 | if (consumed) |
| 4870 | *consumed = size; |
| 4871 | Py_INCREF(unicode_empty); |
| 4872 | return unicode_empty; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4873 | } |
| 4874 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4875 | #ifdef WORDS_BIGENDIAN |
| 4876 | le = bo < 0; |
| 4877 | #else |
| 4878 | le = bo <= 0; |
| 4879 | #endif |
| 4880 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4881 | _PyUnicodeWriter_Init(&writer, 0); |
| 4882 | if (_PyUnicodeWriter_Prepare(&writer, (e - q + 3) / 4, 127) == -1) |
| 4883 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4884 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4885 | while (1) { |
| 4886 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4887 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4888 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4889 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4890 | enum PyUnicode_Kind kind = writer.kind; |
| 4891 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4892 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4893 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4894 | if (le) { |
| 4895 | do { |
| 4896 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4897 | if (ch > maxch) |
| 4898 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4899 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4900 | q += 4; |
| 4901 | } while (q <= last); |
| 4902 | } |
| 4903 | else { |
| 4904 | do { |
| 4905 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 4906 | if (ch > maxch) |
| 4907 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4908 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4909 | q += 4; |
| 4910 | } while (q <= last); |
| 4911 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4912 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4913 | } |
| 4914 | |
| 4915 | if (ch <= maxch) { |
| 4916 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4917 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4918 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4919 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4920 | startinpos = ((const char *)q) - starts; |
| 4921 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4922 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4923 | else { |
| 4924 | if (ch < 0x110000) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4925 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4926 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4927 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4928 | writer.pos++; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4929 | q += 4; |
| 4930 | continue; |
| 4931 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4932 | errmsg = "codepoint not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4933 | startinpos = ((const char *)q) - starts; |
| 4934 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4935 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4936 | |
| 4937 | /* The remaining input chars are ignored if the callback |
| 4938 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4939 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4940 | errors, &errorHandler, |
| 4941 | "utf32", errmsg, |
| 4942 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4943 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4944 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4945 | } |
| 4946 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4947 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4948 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4949 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4950 | Py_XDECREF(errorHandler); |
| 4951 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4952 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4953 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4954 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4955 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4956 | Py_XDECREF(errorHandler); |
| 4957 | Py_XDECREF(exc); |
| 4958 | return NULL; |
| 4959 | } |
| 4960 | |
| 4961 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4962 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 4963 | const char *errors, |
| 4964 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4965 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4966 | int kind; |
| 4967 | void *data; |
| 4968 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4969 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4970 | unsigned char *p; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4971 | Py_ssize_t nsize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4972 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 4973 | #if PY_LITTLE_ENDIAN |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4974 | int iorder[] = {0, 1, 2, 3}; |
| 4975 | #else |
| 4976 | int iorder[] = {3, 2, 1, 0}; |
| 4977 | #endif |
| 4978 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4979 | #define STORECHAR(CH) \ |
| 4980 | do { \ |
| 4981 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4982 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4983 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4984 | p[iorder[0]] = (CH) & 0xff; \ |
| 4985 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4986 | } while(0) |
| 4987 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4988 | if (!PyUnicode_Check(str)) { |
| 4989 | PyErr_BadArgument(); |
| 4990 | return NULL; |
| 4991 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4992 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4993 | return NULL; |
| 4994 | kind = PyUnicode_KIND(str); |
| 4995 | data = PyUnicode_DATA(str); |
| 4996 | len = PyUnicode_GET_LENGTH(str); |
| 4997 | |
| 4998 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4999 | if (nsize > PY_SSIZE_T_MAX / 4) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5000 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 5001 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5002 | if (v == NULL) |
| 5003 | return NULL; |
| 5004 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5005 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5006 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5007 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5008 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5009 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5010 | |
| 5011 | if (byteorder == -1) { |
| 5012 | /* force LE */ |
| 5013 | iorder[0] = 0; |
| 5014 | iorder[1] = 1; |
| 5015 | iorder[2] = 2; |
| 5016 | iorder[3] = 3; |
| 5017 | } |
| 5018 | else if (byteorder == 1) { |
| 5019 | /* force BE */ |
| 5020 | iorder[0] = 3; |
| 5021 | iorder[1] = 2; |
| 5022 | iorder[2] = 1; |
| 5023 | iorder[3] = 0; |
| 5024 | } |
| 5025 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5026 | for (i = 0; i < len; i++) |
| 5027 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5028 | |
| 5029 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5030 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5031 | #undef STORECHAR |
| 5032 | } |
| 5033 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5034 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5035 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5036 | Py_ssize_t size, |
| 5037 | const char *errors, |
| 5038 | int byteorder) |
| 5039 | { |
| 5040 | PyObject *result; |
| 5041 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5042 | if (tmp == NULL) |
| 5043 | return NULL; |
| 5044 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5045 | Py_DECREF(tmp); |
| 5046 | return result; |
| 5047 | } |
| 5048 | |
| 5049 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5050 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5051 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5052 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5053 | } |
| 5054 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5055 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5056 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5057 | PyObject * |
| 5058 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5059 | Py_ssize_t size, |
| 5060 | const char *errors, |
| 5061 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5062 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5063 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5064 | } |
| 5065 | |
| 5066 | PyObject * |
| 5067 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5068 | Py_ssize_t size, |
| 5069 | const char *errors, |
| 5070 | int *byteorder, |
| 5071 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5072 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5073 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5074 | Py_ssize_t startinpos; |
| 5075 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5076 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5077 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5078 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5079 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5080 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5081 | PyObject *errorHandler = NULL; |
| 5082 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5083 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5084 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5085 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5086 | |
| 5087 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5088 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5089 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5090 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5091 | byte order setting accordingly. In native mode, the leading BOM |
| 5092 | mark is skipped, in all other modes, it is copied to the output |
| 5093 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5094 | if (bo == 0 && size >= 2) { |
| 5095 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5096 | if (bom == 0xFEFF) { |
| 5097 | q += 2; |
| 5098 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5099 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5100 | else if (bom == 0xFFFE) { |
| 5101 | q += 2; |
| 5102 | bo = 1; |
| 5103 | } |
| 5104 | if (byteorder) |
| 5105 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5106 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5107 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5108 | if (q == e) { |
| 5109 | if (consumed) |
| 5110 | *consumed = size; |
| 5111 | Py_INCREF(unicode_empty); |
| 5112 | return unicode_empty; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5113 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5114 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5115 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5116 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5117 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5118 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5119 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5120 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5121 | /* Note: size will always be longer than the resulting Unicode |
| 5122 | character count */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5123 | _PyUnicodeWriter_Init(&writer, 0); |
| 5124 | if (_PyUnicodeWriter_Prepare(&writer, (e - q + 1) / 2, 127) == -1) |
| 5125 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5126 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5127 | while (1) { |
| 5128 | Py_UCS4 ch = 0; |
| 5129 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5130 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5131 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5132 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5133 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5134 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5135 | native_ordering); |
| 5136 | else |
| 5137 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5138 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5139 | native_ordering); |
| 5140 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5141 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5142 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5143 | native_ordering); |
| 5144 | } else { |
| 5145 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5146 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5147 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5148 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5149 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5150 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5151 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5152 | switch (ch) |
| 5153 | { |
| 5154 | case 0: |
| 5155 | /* remaining byte at the end? (size should be even) */ |
| 5156 | if (q == e || consumed) |
| 5157 | goto End; |
| 5158 | errmsg = "truncated data"; |
| 5159 | startinpos = ((const char *)q) - starts; |
| 5160 | endinpos = ((const char *)e) - starts; |
| 5161 | break; |
| 5162 | /* The remaining input chars are ignored if the callback |
| 5163 | chooses to skip the input */ |
| 5164 | case 1: |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5165 | q -= 2; |
| 5166 | if (consumed) |
Serhiy Storchaka | ae3b32a | 2013-01-08 23:40:52 +0200 | [diff] [blame] | 5167 | goto End; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5168 | errmsg = "unexpected end of data"; |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 5169 | startinpos = ((const char *)q) - starts; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5170 | endinpos = ((const char *)e) - starts; |
| 5171 | break; |
| 5172 | case 2: |
| 5173 | errmsg = "illegal encoding"; |
| 5174 | startinpos = ((const char *)q) - 2 - starts; |
| 5175 | endinpos = startinpos + 2; |
| 5176 | break; |
| 5177 | case 3: |
| 5178 | errmsg = "illegal UTF-16 surrogate"; |
| 5179 | startinpos = ((const char *)q) - 4 - starts; |
| 5180 | endinpos = startinpos + 2; |
| 5181 | break; |
| 5182 | default: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5183 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5184 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5185 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 5186 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5187 | continue; |
| 5188 | } |
| 5189 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5190 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5191 | errors, |
| 5192 | &errorHandler, |
| 5193 | "utf16", errmsg, |
| 5194 | &starts, |
| 5195 | (const char **)&e, |
| 5196 | &startinpos, |
| 5197 | &endinpos, |
| 5198 | &exc, |
| 5199 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5200 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5201 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5202 | } |
| 5203 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5204 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5205 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5206 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5207 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5208 | Py_XDECREF(errorHandler); |
| 5209 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5210 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5211 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5212 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5213 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5214 | Py_XDECREF(errorHandler); |
| 5215 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5216 | return NULL; |
| 5217 | } |
| 5218 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5219 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5220 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5221 | const char *errors, |
| 5222 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5223 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5224 | enum PyUnicode_Kind kind; |
| 5225 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5226 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5227 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5228 | unsigned short *out; |
| 5229 | Py_ssize_t bytesize; |
| 5230 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5231 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5232 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5233 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5234 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5235 | #endif |
| 5236 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5237 | if (!PyUnicode_Check(str)) { |
| 5238 | PyErr_BadArgument(); |
| 5239 | return NULL; |
| 5240 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5241 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5242 | return NULL; |
| 5243 | kind = PyUnicode_KIND(str); |
| 5244 | data = PyUnicode_DATA(str); |
| 5245 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5246 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5247 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5248 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5249 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5250 | const Py_UCS4 *end = in + len; |
| 5251 | while (in < end) |
| 5252 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5253 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5254 | } |
| 5255 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5256 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5257 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5258 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5259 | if (v == NULL) |
| 5260 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5261 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5262 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5263 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5264 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5265 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5266 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5267 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5268 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5269 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5270 | switch (kind) { |
| 5271 | case PyUnicode_1BYTE_KIND: { |
| 5272 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5273 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5274 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5275 | case PyUnicode_2BYTE_KIND: { |
| 5276 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5277 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5278 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5279 | case PyUnicode_4BYTE_KIND: { |
| 5280 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5281 | break; |
| 5282 | } |
| 5283 | default: |
| 5284 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5285 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5286 | |
| 5287 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5288 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5289 | } |
| 5290 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5291 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5292 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5293 | Py_ssize_t size, |
| 5294 | const char *errors, |
| 5295 | int byteorder) |
| 5296 | { |
| 5297 | PyObject *result; |
| 5298 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5299 | if (tmp == NULL) |
| 5300 | return NULL; |
| 5301 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5302 | Py_DECREF(tmp); |
| 5303 | return result; |
| 5304 | } |
| 5305 | |
| 5306 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5307 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5308 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5309 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5313 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5314 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5315 | if all the escapes in the string make it still a valid ASCII string. |
| 5316 | Returns -1 if any escapes were found which cause the string to |
| 5317 | pop out of ASCII range. Otherwise returns the length of the |
| 5318 | required buffer to hold the string. |
| 5319 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5320 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5321 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5322 | { |
| 5323 | const unsigned char *p = (const unsigned char *)s; |
| 5324 | const unsigned char *end = p + size; |
| 5325 | Py_ssize_t length = 0; |
| 5326 | |
| 5327 | if (size < 0) |
| 5328 | return -1; |
| 5329 | |
| 5330 | for (; p < end; ++p) { |
| 5331 | if (*p > 127) { |
| 5332 | /* Non-ASCII */ |
| 5333 | return -1; |
| 5334 | } |
| 5335 | else if (*p != '\\') { |
| 5336 | /* Normal character */ |
| 5337 | ++length; |
| 5338 | } |
| 5339 | else { |
| 5340 | /* Backslash-escape, check next char */ |
| 5341 | ++p; |
| 5342 | /* Escape sequence reaches till end of string or |
| 5343 | non-ASCII follow-up. */ |
| 5344 | if (p >= end || *p > 127) |
| 5345 | return -1; |
| 5346 | switch (*p) { |
| 5347 | case '\n': |
| 5348 | /* backslash + \n result in zero characters */ |
| 5349 | break; |
| 5350 | case '\\': case '\'': case '\"': |
| 5351 | case 'b': case 'f': case 't': |
| 5352 | case 'n': case 'r': case 'v': case 'a': |
| 5353 | ++length; |
| 5354 | break; |
| 5355 | case '0': case '1': case '2': case '3': |
| 5356 | case '4': case '5': case '6': case '7': |
| 5357 | case 'x': case 'u': case 'U': case 'N': |
| 5358 | /* these do not guarantee ASCII characters */ |
| 5359 | return -1; |
| 5360 | default: |
| 5361 | /* count the backslash + the other character */ |
| 5362 | length += 2; |
| 5363 | } |
| 5364 | } |
| 5365 | } |
| 5366 | return length; |
| 5367 | } |
| 5368 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5369 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5370 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5371 | PyObject * |
| 5372 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5373 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5374 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5375 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5376 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5377 | Py_ssize_t startinpos; |
| 5378 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5379 | int j; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5380 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5382 | char* message; |
| 5383 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5384 | PyObject *errorHandler = NULL; |
| 5385 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5386 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5387 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5388 | len = length_of_escaped_ascii_string(s, size); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5389 | if (len == 0) { |
| 5390 | Py_INCREF(unicode_empty); |
| 5391 | return unicode_empty; |
| 5392 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5393 | |
| 5394 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5395 | either the string is pure ASCII with named escapes like \n, etc. |
| 5396 | and we determined it's exact size (common case) |
| 5397 | or it contains \x, \u, ... escape sequences. then we create a |
| 5398 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5399 | _PyUnicodeWriter_Init(&writer, 0); |
| 5400 | if (len > 0) { |
| 5401 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5402 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5403 | assert(writer.kind == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5404 | } |
| 5405 | else { |
| 5406 | /* Escaped strings will always be longer than the resulting |
| 5407 | Unicode string, so we start with size here and then reduce the |
| 5408 | length after conversion to the true value. |
| 5409 | (but if the error callback returns a long replacement string |
| 5410 | we'll have to allocate more space) */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5411 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5412 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5413 | } |
| 5414 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5415 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5416 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5417 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5418 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5419 | while (s < end) { |
| 5420 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5421 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5422 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5423 | |
| 5424 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5425 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5426 | x = (unsigned char)*s; |
| 5427 | s++; |
| 5428 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5429 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5430 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5431 | writer.pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5432 | continue; |
| 5433 | } |
| 5434 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5435 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5436 | /* \ - Escapes */ |
| 5437 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5438 | c = *s++; |
| 5439 | if (s > end) |
| 5440 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5441 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5442 | /* The only case in which i == ascii_length is a backslash |
| 5443 | followed by a newline. */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5444 | assert(writer.pos < writer.size || (writer.pos == writer.size && c == '\n')); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5445 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5446 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5448 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5449 | #define WRITECHAR(ch) \ |
| 5450 | do { \ |
| 5451 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) \ |
| 5452 | goto onError; \ |
| 5453 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); \ |
| 5454 | writer.pos++; \ |
| 5455 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5457 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5458 | case '\\': WRITECHAR('\\'); break; |
| 5459 | case '\'': WRITECHAR('\''); break; |
| 5460 | case '\"': WRITECHAR('\"'); break; |
| 5461 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5462 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5463 | case 'f': WRITECHAR('\014'); break; |
| 5464 | case 't': WRITECHAR('\t'); break; |
| 5465 | case 'n': WRITECHAR('\n'); break; |
| 5466 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5467 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5468 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5469 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5470 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5471 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5472 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | case '0': case '1': case '2': case '3': |
| 5474 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5475 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5476 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5477 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5478 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5479 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5481 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5482 | break; |
| 5483 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5484 | /* hex escapes */ |
| 5485 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5486 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5487 | digits = 2; |
| 5488 | message = "truncated \\xXX escape"; |
| 5489 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5490 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5491 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5492 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5493 | digits = 4; |
| 5494 | message = "truncated \\uXXXX escape"; |
| 5495 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5497 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5498 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5499 | digits = 8; |
| 5500 | message = "truncated \\UXXXXXXXX escape"; |
| 5501 | hexescape: |
| 5502 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5503 | if (s+digits>end) { |
| 5504 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5505 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5506 | errors, &errorHandler, |
| 5507 | "unicodeescape", "end of string in escape sequence", |
| 5508 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5509 | &writer)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5510 | goto onError; |
| 5511 | goto nextByte; |
| 5512 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5513 | for (j = 0; j < digits; ++j) { |
| 5514 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5515 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5516 | endinpos = (s+j+1)-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5517 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5518 | errors, &errorHandler, |
| 5519 | "unicodeescape", message, |
| 5520 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5521 | &writer)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5522 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5523 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5524 | } |
| 5525 | chr = (chr<<4) & ~0xF; |
| 5526 | if (c >= '0' && c <= '9') |
| 5527 | chr += c - '0'; |
| 5528 | else if (c >= 'a' && c <= 'f') |
| 5529 | chr += 10 + c - 'a'; |
| 5530 | else |
| 5531 | chr += 10 + c - 'A'; |
| 5532 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5533 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5534 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5535 | /* _decoding_error will have already written into the |
| 5536 | target buffer. */ |
| 5537 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5538 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5539 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5540 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5541 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5542 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5543 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5544 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5545 | errors, &errorHandler, |
| 5546 | "unicodeescape", "illegal Unicode character", |
| 5547 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5548 | &writer)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5549 | goto onError; |
| 5550 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5551 | break; |
| 5552 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5553 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5554 | case 'N': |
| 5555 | message = "malformed \\N character escape"; |
| 5556 | if (ucnhash_CAPI == NULL) { |
| 5557 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5558 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5559 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5560 | if (ucnhash_CAPI == NULL) |
| 5561 | goto ucnhashError; |
| 5562 | } |
| 5563 | if (*s == '{') { |
| 5564 | const char *start = s+1; |
| 5565 | /* look for the closing brace */ |
| 5566 | while (*s != '}' && s < end) |
| 5567 | s++; |
| 5568 | if (s > start && s < end && *s == '}') { |
| 5569 | /* found a name. look it up in the unicode database */ |
| 5570 | message = "unknown Unicode character name"; |
| 5571 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5572 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5573 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5574 | goto store; |
| 5575 | } |
| 5576 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5577 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5578 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5579 | errors, &errorHandler, |
| 5580 | "unicodeescape", message, |
| 5581 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5582 | &writer)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5583 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5584 | break; |
| 5585 | |
| 5586 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5587 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5588 | message = "\\ at end of string"; |
| 5589 | s--; |
| 5590 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5591 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5592 | errors, &errorHandler, |
| 5593 | "unicodeescape", message, |
| 5594 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5595 | &writer)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5596 | goto onError; |
| 5597 | } |
| 5598 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5599 | WRITECHAR('\\'); |
| 5600 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5601 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5602 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5603 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5604 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5605 | ; |
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 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5608 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5609 | Py_XDECREF(errorHandler); |
| 5610 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5611 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5612 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5613 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5614 | PyErr_SetString( |
| 5615 | PyExc_UnicodeError, |
| 5616 | "\\N escapes not supported (can't load unicodedata module)" |
| 5617 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5618 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5619 | Py_XDECREF(errorHandler); |
| 5620 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5621 | return NULL; |
| 5622 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5623 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5624 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5625 | Py_XDECREF(errorHandler); |
| 5626 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | return NULL; |
| 5628 | } |
| 5629 | |
| 5630 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5631 | |
| 5632 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5633 | appropriate. |
| 5634 | |
| 5635 | */ |
| 5636 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5637 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5638 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5640 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5641 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5642 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5643 | int kind; |
| 5644 | void *data; |
| 5645 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5646 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5647 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5648 | escape. |
| 5649 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5650 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5651 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5652 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5653 | */ |
| 5654 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5655 | if (!PyUnicode_Check(unicode)) { |
| 5656 | PyErr_BadArgument(); |
| 5657 | return NULL; |
| 5658 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5659 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5660 | return NULL; |
| 5661 | len = PyUnicode_GET_LENGTH(unicode); |
| 5662 | kind = PyUnicode_KIND(unicode); |
| 5663 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5664 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5665 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5666 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5667 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5668 | } |
| 5669 | |
| 5670 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5671 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5672 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5673 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5674 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5675 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5676 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5677 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5678 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5679 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5680 | if (repr == NULL) |
| 5681 | return NULL; |
| 5682 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5683 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5685 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5686 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5687 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5688 | /* Escape backslashes */ |
| 5689 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | *p++ = '\\'; |
| 5691 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5692 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5693 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5694 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5695 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5696 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5697 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5698 | *p++ = '\\'; |
| 5699 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5700 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5701 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5702 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5703 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5704 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5705 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5706 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5707 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5708 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5709 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5710 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5711 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5712 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5713 | *p++ = '\\'; |
| 5714 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5715 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5716 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5717 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5718 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5719 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5720 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5721 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5722 | else if (ch == '\t') { |
| 5723 | *p++ = '\\'; |
| 5724 | *p++ = 't'; |
| 5725 | } |
| 5726 | else if (ch == '\n') { |
| 5727 | *p++ = '\\'; |
| 5728 | *p++ = 'n'; |
| 5729 | } |
| 5730 | else if (ch == '\r') { |
| 5731 | *p++ = '\\'; |
| 5732 | *p++ = 'r'; |
| 5733 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5734 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5735 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5736 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5738 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5739 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5740 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5741 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5742 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5743 | /* Copy everything else as-is */ |
| 5744 | else |
| 5745 | *p++ = (char) ch; |
| 5746 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5747 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5748 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5749 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5750 | return NULL; |
| 5751 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5754 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5755 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5756 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5757 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5758 | PyObject *result; |
| 5759 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5760 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5761 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5762 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5763 | Py_DECREF(tmp); |
| 5764 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5768 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5769 | PyObject * |
| 5770 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5771 | Py_ssize_t size, |
| 5772 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5773 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5774 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5775 | Py_ssize_t startinpos; |
| 5776 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5777 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5778 | const char *end; |
| 5779 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5780 | PyObject *errorHandler = NULL; |
| 5781 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5782 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5783 | if (size == 0) { |
| 5784 | Py_INCREF(unicode_empty); |
| 5785 | return unicode_empty; |
| 5786 | } |
| 5787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5788 | /* Escaped strings will always be longer than the resulting |
| 5789 | 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] | 5790 | length after conversion to the true value. (But decoding error |
| 5791 | handler might have to resize the string) */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5792 | _PyUnicodeWriter_Init(&writer, 1); |
| 5793 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5794 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5795 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5796 | end = s + size; |
| 5797 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5798 | unsigned char c; |
| 5799 | Py_UCS4 x; |
| 5800 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5801 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5802 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5803 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5804 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5805 | x = (unsigned char)*s++; |
| 5806 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5807 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5808 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5809 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5810 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5811 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5812 | startinpos = s-starts; |
| 5813 | |
| 5814 | /* \u-escapes are only interpreted iff the number of leading |
| 5815 | backslashes if odd */ |
| 5816 | bs = s; |
| 5817 | for (;s < end;) { |
| 5818 | if (*s != '\\') |
| 5819 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5820 | x = (unsigned char)*s++; |
| 5821 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5822 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5823 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5824 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5825 | } |
| 5826 | if (((s - bs) & 1) == 0 || |
| 5827 | s >= end || |
| 5828 | (*s != 'u' && *s != 'U')) { |
| 5829 | continue; |
| 5830 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5831 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5832 | count = *s=='u' ? 4 : 8; |
| 5833 | s++; |
| 5834 | |
| 5835 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5836 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5837 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5838 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5839 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5840 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5841 | errors, &errorHandler, |
| 5842 | "rawunicodeescape", "truncated \\uXXXX", |
| 5843 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5844 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5845 | goto onError; |
| 5846 | goto nextByte; |
| 5847 | } |
| 5848 | x = (x<<4) & ~0xF; |
| 5849 | if (c >= '0' && c <= '9') |
| 5850 | x += c - '0'; |
| 5851 | else if (c >= 'a' && c <= 'f') |
| 5852 | x += 10 + c - 'a'; |
| 5853 | else |
| 5854 | x += 10 + c - 'A'; |
| 5855 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5856 | if (x <= MAX_UNICODE) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5857 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5858 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5859 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5860 | writer.pos++; |
| 5861 | } |
| 5862 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5863 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5864 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5865 | errors, &errorHandler, |
| 5866 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5867 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5868 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5869 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5870 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5871 | nextByte: |
| 5872 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5873 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5874 | Py_XDECREF(errorHandler); |
| 5875 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5876 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5877 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5878 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5879 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5880 | Py_XDECREF(errorHandler); |
| 5881 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5882 | return NULL; |
| 5883 | } |
| 5884 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5885 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5886 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5887 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5888 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5889 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | char *p; |
| 5891 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5892 | Py_ssize_t expandsize, pos; |
| 5893 | int kind; |
| 5894 | void *data; |
| 5895 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5896 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5897 | if (!PyUnicode_Check(unicode)) { |
| 5898 | PyErr_BadArgument(); |
| 5899 | return NULL; |
| 5900 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5901 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5902 | return NULL; |
| 5903 | kind = PyUnicode_KIND(unicode); |
| 5904 | data = PyUnicode_DATA(unicode); |
| 5905 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 5906 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 5907 | bytes, and 1 byte characters 4. */ |
| 5908 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5909 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5910 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5911 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5912 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5913 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | if (repr == NULL) |
| 5915 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5916 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5917 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5918 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5919 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5920 | for (pos = 0; pos < len; pos++) { |
| 5921 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5922 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5923 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5924 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5925 | *p++ = '\\'; |
| 5926 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5927 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 5928 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 5929 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 5930 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 5931 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5932 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5933 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5934 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5935 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5936 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5937 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | *p++ = '\\'; |
| 5939 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5940 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5941 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5942 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5943 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5945 | /* Copy everything else as-is */ |
| 5946 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5947 | *p++ = (char) ch; |
| 5948 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5949 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5950 | assert(p > q); |
| 5951 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5952 | return NULL; |
| 5953 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5954 | } |
| 5955 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5956 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5957 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 5958 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5960 | PyObject *result; |
| 5961 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5962 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5963 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5964 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 5965 | Py_DECREF(tmp); |
| 5966 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5967 | } |
| 5968 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5969 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 5970 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5971 | PyObject * |
| 5972 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5973 | Py_ssize_t size, |
| 5974 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5975 | { |
| 5976 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5977 | Py_ssize_t startinpos; |
| 5978 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5979 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5980 | const char *end; |
| 5981 | const char *reason; |
| 5982 | PyObject *errorHandler = NULL; |
| 5983 | PyObject *exc = NULL; |
| 5984 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 5985 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 5986 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 5987 | 1)) |
| 5988 | return NULL; |
| 5989 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5990 | if (size == 0) { |
| 5991 | Py_INCREF(unicode_empty); |
| 5992 | return unicode_empty; |
| 5993 | } |
| 5994 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5995 | /* XXX overflow detection missing */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5996 | _PyUnicodeWriter_Init(&writer, 0); |
| 5997 | if (_PyUnicodeWriter_Prepare(&writer, (size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5998 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5999 | end = s + size; |
| 6000 | |
| 6001 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6002 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6003 | Py_UCS4 ch; |
| 6004 | /* We copy the raw representation one byte at a time because the |
| 6005 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6006 | ((char *) &uch)[0] = s[0]; |
| 6007 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6008 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6009 | ((char *) &uch)[2] = s[2]; |
| 6010 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6011 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6012 | ch = uch; |
| 6013 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6014 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6015 | some malformed UCS-4 data. */ |
| 6016 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6017 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6018 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6019 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6020 | end-s < Py_UNICODE_SIZE |
| 6021 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6022 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6023 | startinpos = s - starts; |
| 6024 | if (end-s < Py_UNICODE_SIZE) { |
| 6025 | endinpos = end-starts; |
| 6026 | reason = "truncated input"; |
| 6027 | } |
| 6028 | else { |
| 6029 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6030 | reason = "illegal code point (> 0x10FFFF)"; |
| 6031 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6032 | if (unicode_decode_call_errorhandler_writer( |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6033 | errors, &errorHandler, |
| 6034 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6035 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6036 | &writer)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6037 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6038 | continue; |
| 6039 | } |
| 6040 | |
| 6041 | s += Py_UNICODE_SIZE; |
| 6042 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6043 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6044 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6045 | Py_UNICODE uch2; |
| 6046 | ((char *) &uch2)[0] = s[0]; |
| 6047 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6048 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6049 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6050 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6051 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6052 | } |
| 6053 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6054 | #endif |
| 6055 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6056 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6057 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6058 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 6059 | writer.pos++; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6060 | } |
| 6061 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6062 | Py_XDECREF(errorHandler); |
| 6063 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6064 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6065 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6066 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6067 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6068 | Py_XDECREF(errorHandler); |
| 6069 | Py_XDECREF(exc); |
| 6070 | return NULL; |
| 6071 | } |
| 6072 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6074 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6075 | PyObject * |
| 6076 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6077 | Py_ssize_t size, |
| 6078 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6081 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | } |
| 6083 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6084 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6085 | static void |
| 6086 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6087 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6088 | PyObject *unicode, |
| 6089 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6090 | const char *reason) |
| 6091 | { |
| 6092 | if (*exceptionObject == NULL) { |
| 6093 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6094 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6095 | encoding, unicode, startpos, endpos, reason); |
| 6096 | } |
| 6097 | else { |
| 6098 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6099 | goto onError; |
| 6100 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6101 | goto onError; |
| 6102 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6103 | goto onError; |
| 6104 | return; |
| 6105 | onError: |
| 6106 | Py_DECREF(*exceptionObject); |
| 6107 | *exceptionObject = NULL; |
| 6108 | } |
| 6109 | } |
| 6110 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6111 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6112 | static void |
| 6113 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6114 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6115 | PyObject *unicode, |
| 6116 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6117 | const char *reason) |
| 6118 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6119 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6120 | encoding, unicode, startpos, endpos, reason); |
| 6121 | if (*exceptionObject != NULL) |
| 6122 | PyCodec_StrictErrors(*exceptionObject); |
| 6123 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6124 | |
| 6125 | /* error handling callback helper: |
| 6126 | build arguments, call the callback and check the arguments, |
| 6127 | put the result into newpos and return the replacement string, which |
| 6128 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6129 | static PyObject * |
| 6130 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6131 | PyObject **errorHandler, |
| 6132 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6133 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6134 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6135 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6136 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6137 | 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] | 6138 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6139 | PyObject *restuple; |
| 6140 | PyObject *resunicode; |
| 6141 | |
| 6142 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6143 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6144 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6145 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6146 | } |
| 6147 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6148 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6149 | return NULL; |
| 6150 | len = PyUnicode_GET_LENGTH(unicode); |
| 6151 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6152 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6153 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6154 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6155 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6156 | |
| 6157 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6158 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6159 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6160 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6161 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6162 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6163 | Py_DECREF(restuple); |
| 6164 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6165 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6166 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | &resunicode, newpos)) { |
| 6168 | Py_DECREF(restuple); |
| 6169 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6170 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6171 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6172 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6173 | Py_DECREF(restuple); |
| 6174 | return NULL; |
| 6175 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6176 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6177 | *newpos = len + *newpos; |
| 6178 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6179 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6180 | Py_DECREF(restuple); |
| 6181 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6182 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6183 | Py_INCREF(resunicode); |
| 6184 | Py_DECREF(restuple); |
| 6185 | return resunicode; |
| 6186 | } |
| 6187 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6188 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6189 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6190 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6191 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6192 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6193 | /* input state */ |
| 6194 | Py_ssize_t pos=0, size; |
| 6195 | int kind; |
| 6196 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6197 | /* output object */ |
| 6198 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6199 | /* pointer into the output */ |
| 6200 | char *str; |
| 6201 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6202 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6203 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6204 | 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] | 6205 | PyObject *errorHandler = NULL; |
| 6206 | PyObject *exc = NULL; |
| 6207 | /* the following variable is used for caching string comparisons |
| 6208 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6209 | int known_errorHandler = -1; |
| 6210 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6211 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6212 | return NULL; |
| 6213 | size = PyUnicode_GET_LENGTH(unicode); |
| 6214 | kind = PyUnicode_KIND(unicode); |
| 6215 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6216 | /* allocate enough for a simple encoding without |
| 6217 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6218 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6219 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6220 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6221 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6222 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6223 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6224 | ressize = size; |
| 6225 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6226 | while (pos < size) { |
| 6227 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6228 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6229 | /* can we encode this? */ |
| 6230 | if (c<limit) { |
| 6231 | /* no overflow check, because we know that the space is enough */ |
| 6232 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6233 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6234 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6235 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6236 | Py_ssize_t requiredsize; |
| 6237 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6238 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6239 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6240 | Py_ssize_t collstart = pos; |
| 6241 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6242 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6243 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6244 | ++collend; |
| 6245 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6246 | if (known_errorHandler==-1) { |
| 6247 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6248 | known_errorHandler = 1; |
| 6249 | else if (!strcmp(errors, "replace")) |
| 6250 | known_errorHandler = 2; |
| 6251 | else if (!strcmp(errors, "ignore")) |
| 6252 | known_errorHandler = 3; |
| 6253 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6254 | known_errorHandler = 4; |
| 6255 | else |
| 6256 | known_errorHandler = 0; |
| 6257 | } |
| 6258 | switch (known_errorHandler) { |
| 6259 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6260 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6261 | goto onError; |
| 6262 | case 2: /* replace */ |
| 6263 | while (collstart++<collend) |
| 6264 | *str++ = '?'; /* fall through */ |
| 6265 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6266 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6267 | break; |
| 6268 | case 4: /* xmlcharrefreplace */ |
| 6269 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6270 | /* determine replacement size */ |
| 6271 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6272 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6273 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6274 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6275 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6276 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6277 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6278 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6279 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6280 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6281 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6282 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6283 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6284 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6285 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6286 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6287 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6288 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6289 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6290 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6291 | if (requiredsize > ressize) { |
| 6292 | if (requiredsize<2*ressize) |
| 6293 | requiredsize = 2*ressize; |
| 6294 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6295 | goto onError; |
| 6296 | str = PyBytes_AS_STRING(res) + respos; |
| 6297 | ressize = requiredsize; |
| 6298 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6299 | /* generate replacement */ |
| 6300 | for (i = collstart; i < collend; ++i) { |
| 6301 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6302 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6303 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6304 | break; |
| 6305 | default: |
| 6306 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6307 | encoding, reason, unicode, &exc, |
| 6308 | collstart, collend, &newpos); |
| 6309 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6310 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6311 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6312 | if (PyBytes_Check(repunicode)) { |
| 6313 | /* Directly copy bytes result to output. */ |
| 6314 | repsize = PyBytes_Size(repunicode); |
| 6315 | if (repsize > 1) { |
| 6316 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6317 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6318 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6319 | Py_DECREF(repunicode); |
| 6320 | goto onError; |
| 6321 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6322 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6323 | ressize += repsize-1; |
| 6324 | } |
| 6325 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6326 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6327 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6328 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6329 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6330 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6331 | /* need more space? (at least enough for what we |
| 6332 | have+the replacement+the rest of the string, so |
| 6333 | we won't have to check space for encodable characters) */ |
| 6334 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6335 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6336 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6337 | if (requiredsize > ressize) { |
| 6338 | if (requiredsize<2*ressize) |
| 6339 | requiredsize = 2*ressize; |
| 6340 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6341 | Py_DECREF(repunicode); |
| 6342 | goto onError; |
| 6343 | } |
| 6344 | str = PyBytes_AS_STRING(res) + respos; |
| 6345 | ressize = requiredsize; |
| 6346 | } |
| 6347 | /* check if there is anything unencodable in the replacement |
| 6348 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6349 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6350 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6351 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6352 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6353 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6354 | Py_DECREF(repunicode); |
| 6355 | goto onError; |
| 6356 | } |
| 6357 | *str = (char)c; |
| 6358 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6359 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6360 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6361 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6362 | } |
| 6363 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6364 | /* Resize if we allocated to much */ |
| 6365 | size = str - PyBytes_AS_STRING(res); |
| 6366 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6367 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6368 | if (_PyBytes_Resize(&res, size) < 0) |
| 6369 | goto onError; |
| 6370 | } |
| 6371 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6372 | Py_XDECREF(errorHandler); |
| 6373 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6374 | return res; |
| 6375 | |
| 6376 | onError: |
| 6377 | Py_XDECREF(res); |
| 6378 | Py_XDECREF(errorHandler); |
| 6379 | Py_XDECREF(exc); |
| 6380 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6381 | } |
| 6382 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6383 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6384 | PyObject * |
| 6385 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6386 | Py_ssize_t size, |
| 6387 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6388 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6389 | PyObject *result; |
| 6390 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6391 | if (unicode == NULL) |
| 6392 | return NULL; |
| 6393 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6394 | Py_DECREF(unicode); |
| 6395 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6396 | } |
| 6397 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6398 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6399 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6400 | { |
| 6401 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6402 | PyErr_BadArgument(); |
| 6403 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6405 | if (PyUnicode_READY(unicode) == -1) |
| 6406 | return NULL; |
| 6407 | /* Fast path: if it is a one-byte string, construct |
| 6408 | bytes object directly. */ |
| 6409 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6410 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6411 | PyUnicode_GET_LENGTH(unicode)); |
| 6412 | /* Non-Latin-1 characters present. Defer to above function to |
| 6413 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6414 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6415 | } |
| 6416 | |
| 6417 | PyObject* |
| 6418 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6419 | { |
| 6420 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6421 | } |
| 6422 | |
| 6423 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6424 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6425 | PyObject * |
| 6426 | PyUnicode_DecodeASCII(const char *s, |
| 6427 | Py_ssize_t size, |
| 6428 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6429 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6430 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6431 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6432 | int kind; |
| 6433 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6434 | Py_ssize_t startinpos; |
| 6435 | Py_ssize_t endinpos; |
| 6436 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6437 | const char *e; |
| 6438 | PyObject *errorHandler = NULL; |
| 6439 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6440 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6441 | if (size == 0) { |
| 6442 | Py_INCREF(unicode_empty); |
| 6443 | return unicode_empty; |
| 6444 | } |
| 6445 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6446 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6447 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6448 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6449 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6450 | _PyUnicodeWriter_Init(&writer, 0); |
| 6451 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6452 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6453 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6454 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6455 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6456 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6457 | writer.pos = outpos; |
| 6458 | if (writer.pos == size) |
| 6459 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6460 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6461 | s += writer.pos; |
| 6462 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6463 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6464 | register unsigned char c = (unsigned char)*s; |
| 6465 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6466 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6467 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6468 | ++s; |
| 6469 | } |
| 6470 | else { |
| 6471 | startinpos = s-starts; |
| 6472 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6473 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6474 | errors, &errorHandler, |
| 6475 | "ascii", "ordinal not in range(128)", |
| 6476 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6477 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6478 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6479 | kind = writer.kind; |
| 6480 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6481 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6483 | Py_XDECREF(errorHandler); |
| 6484 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6485 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6486 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6487 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6488 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6489 | Py_XDECREF(errorHandler); |
| 6490 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6491 | return NULL; |
| 6492 | } |
| 6493 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6494 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6495 | PyObject * |
| 6496 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6497 | Py_ssize_t size, |
| 6498 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6499 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6500 | PyObject *result; |
| 6501 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6502 | if (unicode == NULL) |
| 6503 | return NULL; |
| 6504 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6505 | Py_DECREF(unicode); |
| 6506 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6507 | } |
| 6508 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6509 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6510 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6511 | { |
| 6512 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6513 | PyErr_BadArgument(); |
| 6514 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6515 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6516 | if (PyUnicode_READY(unicode) == -1) |
| 6517 | return NULL; |
| 6518 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6519 | directly. Else defer to above function to raise the exception. */ |
| 6520 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6521 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6522 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6523 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6524 | } |
| 6525 | |
| 6526 | PyObject * |
| 6527 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6528 | { |
| 6529 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6530 | } |
| 6531 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6532 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6533 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6534 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6535 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6536 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6537 | #define NEED_RETRY |
| 6538 | #endif |
| 6539 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6540 | #ifndef WC_ERR_INVALID_CHARS |
| 6541 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6542 | #endif |
| 6543 | |
| 6544 | static char* |
| 6545 | code_page_name(UINT code_page, PyObject **obj) |
| 6546 | { |
| 6547 | *obj = NULL; |
| 6548 | if (code_page == CP_ACP) |
| 6549 | return "mbcs"; |
| 6550 | if (code_page == CP_UTF7) |
| 6551 | return "CP_UTF7"; |
| 6552 | if (code_page == CP_UTF8) |
| 6553 | return "CP_UTF8"; |
| 6554 | |
| 6555 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6556 | if (*obj == NULL) |
| 6557 | return NULL; |
| 6558 | return PyBytes_AS_STRING(*obj); |
| 6559 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6560 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6561 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6562 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6563 | { |
| 6564 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6565 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6566 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6567 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6568 | return 0; |
| 6569 | |
| 6570 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6571 | if (prev == curr) |
| 6572 | return 1; |
| 6573 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6574 | as it assumes an incomplete character consists of a single |
| 6575 | byte. */ |
| 6576 | if (curr - prev == 2) |
| 6577 | return 1; |
| 6578 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6579 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6580 | return 0; |
| 6581 | } |
| 6582 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6583 | static DWORD |
| 6584 | decode_code_page_flags(UINT code_page) |
| 6585 | { |
| 6586 | if (code_page == CP_UTF7) { |
| 6587 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6588 | return 0; |
| 6589 | } |
| 6590 | else |
| 6591 | return MB_ERR_INVALID_CHARS; |
| 6592 | } |
| 6593 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6594 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6595 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6596 | * mode. |
| 6597 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6598 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6599 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6600 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6601 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6602 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6603 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6604 | const char *in, |
| 6605 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6606 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6607 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6608 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6609 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6610 | |
| 6611 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6612 | assert(insize > 0); |
| 6613 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6614 | if (outsize <= 0) |
| 6615 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6616 | |
| 6617 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6618 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6619 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6620 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6621 | if (*v == NULL) |
| 6622 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6623 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6624 | } |
| 6625 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6626 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6627 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6628 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6629 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6630 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6631 | } |
| 6632 | |
| 6633 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6634 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6635 | if (outsize <= 0) |
| 6636 | goto error; |
| 6637 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6638 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6639 | error: |
| 6640 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6641 | return -2; |
| 6642 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6643 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6644 | } |
| 6645 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6646 | /* |
| 6647 | * Decode a byte string from a code page into unicode object with an error |
| 6648 | * handler. |
| 6649 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6650 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6651 | * UnicodeDecodeError exception and returns -1 on error. |
| 6652 | */ |
| 6653 | static int |
| 6654 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6655 | PyObject **v, |
| 6656 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6657 | const char *errors) |
| 6658 | { |
| 6659 | const char *startin = in; |
| 6660 | const char *endin = in + size; |
| 6661 | const DWORD flags = decode_code_page_flags(code_page); |
| 6662 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6663 | 2000 English version of the message. */ |
| 6664 | const char *reason = "No mapping for the Unicode character exists " |
| 6665 | "in the target code page."; |
| 6666 | /* each step cannot decode more than 1 character, but a character can be |
| 6667 | represented as a surrogate pair */ |
| 6668 | wchar_t buffer[2], *startout, *out; |
| 6669 | int insize, outsize; |
| 6670 | PyObject *errorHandler = NULL; |
| 6671 | PyObject *exc = NULL; |
| 6672 | PyObject *encoding_obj = NULL; |
| 6673 | char *encoding; |
| 6674 | DWORD err; |
| 6675 | int ret = -1; |
| 6676 | |
| 6677 | assert(size > 0); |
| 6678 | |
| 6679 | encoding = code_page_name(code_page, &encoding_obj); |
| 6680 | if (encoding == NULL) |
| 6681 | return -1; |
| 6682 | |
| 6683 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6684 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6685 | UnicodeDecodeError. */ |
| 6686 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6687 | if (exc != NULL) { |
| 6688 | PyCodec_StrictErrors(exc); |
| 6689 | Py_CLEAR(exc); |
| 6690 | } |
| 6691 | goto error; |
| 6692 | } |
| 6693 | |
| 6694 | if (*v == NULL) { |
| 6695 | /* Create unicode object */ |
| 6696 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6697 | PyErr_NoMemory(); |
| 6698 | goto error; |
| 6699 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6700 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6701 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6702 | if (*v == NULL) |
| 6703 | goto error; |
| 6704 | startout = PyUnicode_AS_UNICODE(*v); |
| 6705 | } |
| 6706 | else { |
| 6707 | /* Extend unicode object */ |
| 6708 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6709 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6710 | PyErr_NoMemory(); |
| 6711 | goto error; |
| 6712 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6713 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6714 | goto error; |
| 6715 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6716 | } |
| 6717 | |
| 6718 | /* Decode the byte string character per character */ |
| 6719 | out = startout; |
| 6720 | while (in < endin) |
| 6721 | { |
| 6722 | /* Decode a character */ |
| 6723 | insize = 1; |
| 6724 | do |
| 6725 | { |
| 6726 | outsize = MultiByteToWideChar(code_page, flags, |
| 6727 | in, insize, |
| 6728 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6729 | if (outsize > 0) |
| 6730 | break; |
| 6731 | err = GetLastError(); |
| 6732 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6733 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6734 | { |
| 6735 | PyErr_SetFromWindowsErr(0); |
| 6736 | goto error; |
| 6737 | } |
| 6738 | insize++; |
| 6739 | } |
| 6740 | /* 4=maximum length of a UTF-8 sequence */ |
| 6741 | while (insize <= 4 && (in + insize) <= endin); |
| 6742 | |
| 6743 | if (outsize <= 0) { |
| 6744 | Py_ssize_t startinpos, endinpos, outpos; |
| 6745 | |
| 6746 | startinpos = in - startin; |
| 6747 | endinpos = startinpos + 1; |
| 6748 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6749 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6750 | errors, &errorHandler, |
| 6751 | encoding, reason, |
| 6752 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6753 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6754 | { |
| 6755 | goto error; |
| 6756 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6757 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6758 | } |
| 6759 | else { |
| 6760 | in += insize; |
| 6761 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6762 | out += outsize; |
| 6763 | } |
| 6764 | } |
| 6765 | |
| 6766 | /* write a NUL character at the end */ |
| 6767 | *out = 0; |
| 6768 | |
| 6769 | /* Extend unicode object */ |
| 6770 | outsize = out - startout; |
| 6771 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6772 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6773 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6774 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6775 | |
| 6776 | error: |
| 6777 | Py_XDECREF(encoding_obj); |
| 6778 | Py_XDECREF(errorHandler); |
| 6779 | Py_XDECREF(exc); |
| 6780 | return ret; |
| 6781 | } |
| 6782 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6783 | static PyObject * |
| 6784 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6785 | const char *s, Py_ssize_t size, |
| 6786 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6787 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6788 | PyObject *v = NULL; |
| 6789 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6790 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6791 | if (code_page < 0) { |
| 6792 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6793 | return NULL; |
| 6794 | } |
| 6795 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6796 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6797 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6798 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6799 | do |
| 6800 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6801 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6802 | if (size > INT_MAX) { |
| 6803 | chunk_size = INT_MAX; |
| 6804 | final = 0; |
| 6805 | done = 0; |
| 6806 | } |
| 6807 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6808 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6809 | { |
| 6810 | chunk_size = (int)size; |
| 6811 | final = (consumed == NULL); |
| 6812 | done = 1; |
| 6813 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6814 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6815 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6816 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6817 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6818 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6819 | if (chunk_size == 0 && done) { |
| 6820 | if (v != NULL) |
| 6821 | break; |
| 6822 | Py_INCREF(unicode_empty); |
| 6823 | return unicode_empty; |
| 6824 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6825 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6826 | |
| 6827 | converted = decode_code_page_strict(code_page, &v, |
| 6828 | s, chunk_size); |
| 6829 | if (converted == -2) |
| 6830 | converted = decode_code_page_errors(code_page, &v, |
| 6831 | s, chunk_size, |
| 6832 | errors); |
| 6833 | assert(converted != 0); |
| 6834 | |
| 6835 | if (converted < 0) { |
| 6836 | Py_XDECREF(v); |
| 6837 | return NULL; |
| 6838 | } |
| 6839 | |
| 6840 | if (consumed) |
| 6841 | *consumed += converted; |
| 6842 | |
| 6843 | s += converted; |
| 6844 | size -= converted; |
| 6845 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6846 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6847 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6848 | } |
| 6849 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6850 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6851 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 6852 | const char *s, |
| 6853 | Py_ssize_t size, |
| 6854 | const char *errors, |
| 6855 | Py_ssize_t *consumed) |
| 6856 | { |
| 6857 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 6858 | } |
| 6859 | |
| 6860 | PyObject * |
| 6861 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6862 | Py_ssize_t size, |
| 6863 | const char *errors, |
| 6864 | Py_ssize_t *consumed) |
| 6865 | { |
| 6866 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 6867 | } |
| 6868 | |
| 6869 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6870 | PyUnicode_DecodeMBCS(const char *s, |
| 6871 | Py_ssize_t size, |
| 6872 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6873 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6874 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6875 | } |
| 6876 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6877 | static DWORD |
| 6878 | encode_code_page_flags(UINT code_page, const char *errors) |
| 6879 | { |
| 6880 | if (code_page == CP_UTF8) { |
| 6881 | if (winver.dwMajorVersion >= 6) |
| 6882 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 6883 | and later */ |
| 6884 | return WC_ERR_INVALID_CHARS; |
| 6885 | else |
| 6886 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 6887 | return 0; |
| 6888 | } |
| 6889 | else if (code_page == CP_UTF7) { |
| 6890 | /* CP_UTF7 only supports flags=0 */ |
| 6891 | return 0; |
| 6892 | } |
| 6893 | else { |
| 6894 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 6895 | return 0; |
| 6896 | else |
| 6897 | return WC_NO_BEST_FIT_CHARS; |
| 6898 | } |
| 6899 | } |
| 6900 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6901 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6902 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 6903 | * mode. |
| 6904 | * |
| 6905 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6906 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6907 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6908 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6909 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6910 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6911 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6912 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6913 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6914 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 6915 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6916 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6917 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6918 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6919 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 6920 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6921 | /* Create a substring so that we can get the UTF-16 representation |
| 6922 | of just the slice under consideration. */ |
| 6923 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6924 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6925 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6926 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6927 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6928 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6929 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6930 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6931 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6932 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 6933 | if (substring == NULL) |
| 6934 | return -1; |
| 6935 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 6936 | if (p == NULL) { |
| 6937 | Py_DECREF(substring); |
| 6938 | return -1; |
| 6939 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6940 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6941 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6942 | outsize = WideCharToMultiByte(code_page, flags, |
| 6943 | p, size, |
| 6944 | NULL, 0, |
| 6945 | NULL, pusedDefaultChar); |
| 6946 | if (outsize <= 0) |
| 6947 | goto error; |
| 6948 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6949 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 6950 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6951 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6952 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6953 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6954 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6955 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6956 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6957 | if (*outbytes == NULL) { |
| 6958 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6959 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6960 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6961 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6962 | } |
| 6963 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6964 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6965 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 6966 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 6967 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6968 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6969 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6970 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6971 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 6972 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6973 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6974 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6975 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6976 | } |
| 6977 | |
| 6978 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6979 | outsize = WideCharToMultiByte(code_page, flags, |
| 6980 | p, size, |
| 6981 | out, outsize, |
| 6982 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6983 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6984 | if (outsize <= 0) |
| 6985 | goto error; |
| 6986 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6987 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6988 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6989 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6990 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6991 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6992 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6993 | return -2; |
| 6994 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6995 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6996 | } |
| 6997 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6998 | /* |
| 6999 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 7000 | * error handler. |
| 7001 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 7002 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7003 | * -1 on other error. |
| 7004 | */ |
| 7005 | static int |
| 7006 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7007 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7008 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7009 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7010 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7011 | Py_ssize_t pos = unicode_offset; |
| 7012 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7013 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7014 | 2000 English version of the message. */ |
| 7015 | const char *reason = "invalid character"; |
| 7016 | /* 4=maximum length of a UTF-8 sequence */ |
| 7017 | char buffer[4]; |
| 7018 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7019 | Py_ssize_t outsize; |
| 7020 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7021 | PyObject *errorHandler = NULL; |
| 7022 | PyObject *exc = NULL; |
| 7023 | PyObject *encoding_obj = NULL; |
| 7024 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7025 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7026 | PyObject *rep; |
| 7027 | int ret = -1; |
| 7028 | |
| 7029 | assert(insize > 0); |
| 7030 | |
| 7031 | encoding = code_page_name(code_page, &encoding_obj); |
| 7032 | if (encoding == NULL) |
| 7033 | return -1; |
| 7034 | |
| 7035 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7036 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7037 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7038 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7039 | if (exc != NULL) { |
| 7040 | PyCodec_StrictErrors(exc); |
| 7041 | Py_DECREF(exc); |
| 7042 | } |
| 7043 | Py_XDECREF(encoding_obj); |
| 7044 | return -1; |
| 7045 | } |
| 7046 | |
| 7047 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7048 | pusedDefaultChar = &usedDefaultChar; |
| 7049 | else |
| 7050 | pusedDefaultChar = NULL; |
| 7051 | |
| 7052 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7053 | PyErr_NoMemory(); |
| 7054 | goto error; |
| 7055 | } |
| 7056 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7057 | |
| 7058 | if (*outbytes == NULL) { |
| 7059 | /* Create string object */ |
| 7060 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7061 | if (*outbytes == NULL) |
| 7062 | goto error; |
| 7063 | out = PyBytes_AS_STRING(*outbytes); |
| 7064 | } |
| 7065 | else { |
| 7066 | /* Extend string object */ |
| 7067 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7068 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7069 | PyErr_NoMemory(); |
| 7070 | goto error; |
| 7071 | } |
| 7072 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7073 | goto error; |
| 7074 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7075 | } |
| 7076 | |
| 7077 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7078 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7079 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7080 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7081 | wchar_t chars[2]; |
| 7082 | int charsize; |
| 7083 | if (ch < 0x10000) { |
| 7084 | chars[0] = (wchar_t)ch; |
| 7085 | charsize = 1; |
| 7086 | } |
| 7087 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7088 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7089 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7090 | charsize = 2; |
| 7091 | } |
| 7092 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7093 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7094 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7095 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7096 | NULL, pusedDefaultChar); |
| 7097 | if (outsize > 0) { |
| 7098 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7099 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7100 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7101 | memcpy(out, buffer, outsize); |
| 7102 | out += outsize; |
| 7103 | continue; |
| 7104 | } |
| 7105 | } |
| 7106 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7107 | PyErr_SetFromWindowsErr(0); |
| 7108 | goto error; |
| 7109 | } |
| 7110 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7111 | rep = unicode_encode_call_errorhandler( |
| 7112 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7113 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7114 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7115 | if (rep == NULL) |
| 7116 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7117 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7118 | |
| 7119 | if (PyBytes_Check(rep)) { |
| 7120 | outsize = PyBytes_GET_SIZE(rep); |
| 7121 | if (outsize != 1) { |
| 7122 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7123 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7124 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7125 | Py_DECREF(rep); |
| 7126 | goto error; |
| 7127 | } |
| 7128 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7129 | } |
| 7130 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7131 | out += outsize; |
| 7132 | } |
| 7133 | else { |
| 7134 | Py_ssize_t i; |
| 7135 | enum PyUnicode_Kind kind; |
| 7136 | void *data; |
| 7137 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7138 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7139 | Py_DECREF(rep); |
| 7140 | goto error; |
| 7141 | } |
| 7142 | |
| 7143 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7144 | if (outsize != 1) { |
| 7145 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7146 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7147 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7148 | Py_DECREF(rep); |
| 7149 | goto error; |
| 7150 | } |
| 7151 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7152 | } |
| 7153 | kind = PyUnicode_KIND(rep); |
| 7154 | data = PyUnicode_DATA(rep); |
| 7155 | for (i=0; i < outsize; i++) { |
| 7156 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7157 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7158 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7159 | encoding, unicode, |
| 7160 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7161 | "unable to encode error handler result to ASCII"); |
| 7162 | Py_DECREF(rep); |
| 7163 | goto error; |
| 7164 | } |
| 7165 | *out = (unsigned char)ch; |
| 7166 | out++; |
| 7167 | } |
| 7168 | } |
| 7169 | Py_DECREF(rep); |
| 7170 | } |
| 7171 | /* write a NUL byte */ |
| 7172 | *out = 0; |
| 7173 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7174 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7175 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7176 | goto error; |
| 7177 | ret = 0; |
| 7178 | |
| 7179 | error: |
| 7180 | Py_XDECREF(encoding_obj); |
| 7181 | Py_XDECREF(errorHandler); |
| 7182 | Py_XDECREF(exc); |
| 7183 | return ret; |
| 7184 | } |
| 7185 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7186 | static PyObject * |
| 7187 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7188 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7189 | const char *errors) |
| 7190 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7191 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7192 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7193 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7194 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7195 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7196 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7197 | return NULL; |
| 7198 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7199 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7200 | if (code_page < 0) { |
| 7201 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7202 | return NULL; |
| 7203 | } |
| 7204 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7205 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7206 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7207 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7208 | offset = 0; |
| 7209 | do |
| 7210 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7211 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7212 | /* 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] | 7213 | chunks. */ |
| 7214 | if (len > INT_MAX/2) { |
| 7215 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7216 | done = 0; |
| 7217 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7218 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7219 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7220 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7221 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7222 | done = 1; |
| 7223 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7224 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7225 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7226 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7227 | errors); |
| 7228 | if (ret == -2) |
| 7229 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7230 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7231 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7232 | if (ret < 0) { |
| 7233 | Py_XDECREF(outbytes); |
| 7234 | return NULL; |
| 7235 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7236 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7237 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7238 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7239 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7240 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7241 | return outbytes; |
| 7242 | } |
| 7243 | |
| 7244 | PyObject * |
| 7245 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7246 | Py_ssize_t size, |
| 7247 | const char *errors) |
| 7248 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7249 | PyObject *unicode, *res; |
| 7250 | unicode = PyUnicode_FromUnicode(p, size); |
| 7251 | if (unicode == NULL) |
| 7252 | return NULL; |
| 7253 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7254 | Py_DECREF(unicode); |
| 7255 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7256 | } |
| 7257 | |
| 7258 | PyObject * |
| 7259 | PyUnicode_EncodeCodePage(int code_page, |
| 7260 | PyObject *unicode, |
| 7261 | const char *errors) |
| 7262 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7263 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7264 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7265 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7266 | PyObject * |
| 7267 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7268 | { |
| 7269 | if (!PyUnicode_Check(unicode)) { |
| 7270 | PyErr_BadArgument(); |
| 7271 | return NULL; |
| 7272 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7273 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7274 | } |
| 7275 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7276 | #undef NEED_RETRY |
| 7277 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7278 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7279 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7280 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7281 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7282 | PyObject * |
| 7283 | PyUnicode_DecodeCharmap(const char *s, |
| 7284 | Py_ssize_t size, |
| 7285 | PyObject *mapping, |
| 7286 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7287 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7288 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7289 | Py_ssize_t startinpos; |
| 7290 | Py_ssize_t endinpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7291 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7292 | _PyUnicodeWriter writer; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7293 | PyObject *errorHandler = NULL; |
| 7294 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7295 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7296 | /* Default to Latin-1 */ |
| 7297 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7298 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7299 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7300 | if (size == 0) { |
| 7301 | Py_INCREF(unicode_empty); |
| 7302 | return unicode_empty; |
| 7303 | } |
| 7304 | _PyUnicodeWriter_Init(&writer, 0); |
| 7305 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7306 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7307 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7308 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7309 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7310 | Py_ssize_t maplen; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7311 | enum PyUnicode_Kind mapkind; |
| 7312 | void *mapdata; |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7313 | Py_UCS4 x; |
| 7314 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7315 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7316 | return NULL; |
| 7317 | |
| 7318 | maplen = PyUnicode_GET_LENGTH(mapping); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7319 | mapdata = PyUnicode_DATA(mapping); |
| 7320 | mapkind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7321 | while (s < e) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7322 | unsigned char ch; |
| 7323 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7324 | enum PyUnicode_Kind outkind = writer.kind; |
| 7325 | void *outdata = writer.data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7326 | if (outkind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7327 | Py_UCS4 maxchar = writer.maxchar; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7328 | while (s < e) { |
| 7329 | unsigned char ch = *s; |
| 7330 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7331 | if (x > maxchar) |
| 7332 | goto Error; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7333 | PyUnicode_WRITE(PyUnicode_1BYTE_KIND, outdata, writer.pos, x); |
| 7334 | writer.pos++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7335 | ++s; |
| 7336 | } |
| 7337 | break; |
| 7338 | } |
| 7339 | else if (outkind == PyUnicode_2BYTE_KIND) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7340 | while (s < e) { |
| 7341 | unsigned char ch = *s; |
| 7342 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7343 | if (x == 0xFFFE) |
| 7344 | goto Error; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7345 | PyUnicode_WRITE(PyUnicode_2BYTE_KIND, outdata, writer.pos, x); |
| 7346 | writer.pos++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7347 | ++s; |
| 7348 | } |
| 7349 | break; |
| 7350 | } |
| 7351 | } |
| 7352 | ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7353 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7354 | if (ch < maplen) |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7355 | x = PyUnicode_READ(mapkind, mapdata, ch); |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7356 | else |
| 7357 | x = 0xfffe; /* invalid value */ |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7358 | Error: |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7359 | if (x == 0xfffe) |
| 7360 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7361 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7362 | startinpos = s-starts; |
| 7363 | endinpos = startinpos+1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7364 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7365 | errors, &errorHandler, |
| 7366 | "charmap", "character maps to <undefined>", |
| 7367 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7368 | &writer)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7369 | goto onError; |
| 7370 | } |
| 7371 | continue; |
| 7372 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7373 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7374 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7375 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7376 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 7377 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7378 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7379 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7380 | } |
| 7381 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7382 | while (s < e) { |
| 7383 | unsigned char ch = *s; |
| 7384 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7385 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7386 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7387 | w = PyLong_FromLong((long)ch); |
| 7388 | if (w == NULL) |
| 7389 | goto onError; |
| 7390 | x = PyObject_GetItem(mapping, w); |
| 7391 | Py_DECREF(w); |
| 7392 | if (x == NULL) { |
| 7393 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7394 | /* No mapping found means: mapping is undefined. */ |
| 7395 | PyErr_Clear(); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7396 | goto Undefined; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7397 | } else |
| 7398 | goto onError; |
| 7399 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7400 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7401 | /* Apply mapping */ |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7402 | if (x == Py_None) |
| 7403 | goto Undefined; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7404 | if (PyLong_Check(x)) { |
| 7405 | long value = PyLong_AS_LONG(x); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7406 | if (value == 0xFFFE) |
| 7407 | goto Undefined; |
Antoine Pitrou | a1f7655 | 2012-09-23 20:00:04 +0200 | [diff] [blame] | 7408 | if (value < 0 || value > MAX_UNICODE) { |
| 7409 | PyErr_Format(PyExc_TypeError, |
| 7410 | "character mapping must be in range(0x%lx)", |
| 7411 | (unsigned long)MAX_UNICODE + 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7412 | Py_DECREF(x); |
| 7413 | goto onError; |
| 7414 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7415 | |
| 7416 | if (_PyUnicodeWriter_Prepare(&writer, 1, value) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7417 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7418 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, value); |
| 7419 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7420 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7421 | else if (PyUnicode_Check(x)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7422 | if (PyUnicode_READY(x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7423 | goto onError; |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame^] | 7424 | if (PyUnicode_GET_LENGTH(x) == 1) { |
Serhiy Storchaka | 45d16d9 | 2013-01-15 15:01:20 +0200 | [diff] [blame] | 7425 | Py_UCS4 value = PyUnicode_READ_CHAR(x, 0); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7426 | if (value == 0xFFFE) |
| 7427 | goto Undefined; |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame^] | 7428 | if (_PyUnicodeWriter_Prepare(&writer, 1, value) == -1) |
| 7429 | goto onError; |
| 7430 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, value); |
| 7431 | writer.pos++; |
| 7432 | } |
| 7433 | else { |
| 7434 | writer.overallocate = 1; |
| 7435 | if (_PyUnicodeWriter_WriteStr(&writer, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7436 | goto onError; |
| 7437 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7438 | } |
| 7439 | else { |
| 7440 | /* wrong return value */ |
| 7441 | PyErr_SetString(PyExc_TypeError, |
| 7442 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7443 | Py_DECREF(x); |
| 7444 | goto onError; |
| 7445 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7446 | Py_DECREF(x); |
| 7447 | ++s; |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7448 | continue; |
| 7449 | Undefined: |
| 7450 | /* undefined mapping */ |
| 7451 | Py_XDECREF(x); |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7452 | startinpos = s-starts; |
| 7453 | endinpos = startinpos+1; |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame^] | 7454 | if (unicode_decode_call_errorhandler_writer( |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7455 | errors, &errorHandler, |
| 7456 | "charmap", "character maps to <undefined>", |
| 7457 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Serhiy Storchaka | 55e2cb4 | 2013-01-15 15:30:04 +0200 | [diff] [blame^] | 7458 | &writer)) { |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 7459 | goto onError; |
| 7460 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7461 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7462 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7463 | Py_XDECREF(errorHandler); |
| 7464 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7465 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7466 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7467 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7468 | Py_XDECREF(errorHandler); |
| 7469 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7470 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | return NULL; |
| 7472 | } |
| 7473 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7474 | /* Charmap encoding: the lookup table */ |
| 7475 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7476 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7477 | PyObject_HEAD |
| 7478 | unsigned char level1[32]; |
| 7479 | int count2, count3; |
| 7480 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7481 | }; |
| 7482 | |
| 7483 | static PyObject* |
| 7484 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7485 | { |
| 7486 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7487 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7488 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7489 | } |
| 7490 | |
| 7491 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7492 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7493 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7494 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7495 | }; |
| 7496 | |
| 7497 | static void |
| 7498 | encoding_map_dealloc(PyObject* o) |
| 7499 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7500 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7501 | } |
| 7502 | |
| 7503 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7504 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7505 | "EncodingMap", /*tp_name*/ |
| 7506 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7507 | 0, /*tp_itemsize*/ |
| 7508 | /* methods */ |
| 7509 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7510 | 0, /*tp_print*/ |
| 7511 | 0, /*tp_getattr*/ |
| 7512 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7513 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7514 | 0, /*tp_repr*/ |
| 7515 | 0, /*tp_as_number*/ |
| 7516 | 0, /*tp_as_sequence*/ |
| 7517 | 0, /*tp_as_mapping*/ |
| 7518 | 0, /*tp_hash*/ |
| 7519 | 0, /*tp_call*/ |
| 7520 | 0, /*tp_str*/ |
| 7521 | 0, /*tp_getattro*/ |
| 7522 | 0, /*tp_setattro*/ |
| 7523 | 0, /*tp_as_buffer*/ |
| 7524 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7525 | 0, /*tp_doc*/ |
| 7526 | 0, /*tp_traverse*/ |
| 7527 | 0, /*tp_clear*/ |
| 7528 | 0, /*tp_richcompare*/ |
| 7529 | 0, /*tp_weaklistoffset*/ |
| 7530 | 0, /*tp_iter*/ |
| 7531 | 0, /*tp_iternext*/ |
| 7532 | encoding_map_methods, /*tp_methods*/ |
| 7533 | 0, /*tp_members*/ |
| 7534 | 0, /*tp_getset*/ |
| 7535 | 0, /*tp_base*/ |
| 7536 | 0, /*tp_dict*/ |
| 7537 | 0, /*tp_descr_get*/ |
| 7538 | 0, /*tp_descr_set*/ |
| 7539 | 0, /*tp_dictoffset*/ |
| 7540 | 0, /*tp_init*/ |
| 7541 | 0, /*tp_alloc*/ |
| 7542 | 0, /*tp_new*/ |
| 7543 | 0, /*tp_free*/ |
| 7544 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7545 | }; |
| 7546 | |
| 7547 | PyObject* |
| 7548 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7549 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7550 | PyObject *result; |
| 7551 | struct encoding_map *mresult; |
| 7552 | int i; |
| 7553 | int need_dict = 0; |
| 7554 | unsigned char level1[32]; |
| 7555 | unsigned char level2[512]; |
| 7556 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7557 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7558 | int kind; |
| 7559 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7560 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7561 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7562 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7563 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7564 | PyErr_BadArgument(); |
| 7565 | return NULL; |
| 7566 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7567 | kind = PyUnicode_KIND(string); |
| 7568 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7569 | length = PyUnicode_GET_LENGTH(string); |
| 7570 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7571 | memset(level1, 0xFF, sizeof level1); |
| 7572 | memset(level2, 0xFF, sizeof level2); |
| 7573 | |
| 7574 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7575 | or if there are non-BMP characters, we need to use |
| 7576 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7577 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7578 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7579 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7580 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7581 | ch = PyUnicode_READ(kind, data, i); |
| 7582 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7583 | need_dict = 1; |
| 7584 | break; |
| 7585 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7586 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7587 | /* unmapped character */ |
| 7588 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7589 | l1 = ch >> 11; |
| 7590 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7591 | if (level1[l1] == 0xFF) |
| 7592 | level1[l1] = count2++; |
| 7593 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7594 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7595 | } |
| 7596 | |
| 7597 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7598 | need_dict = 1; |
| 7599 | |
| 7600 | if (need_dict) { |
| 7601 | PyObject *result = PyDict_New(); |
| 7602 | PyObject *key, *value; |
| 7603 | if (!result) |
| 7604 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7605 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7606 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7607 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7608 | if (!key || !value) |
| 7609 | goto failed1; |
| 7610 | if (PyDict_SetItem(result, key, value) == -1) |
| 7611 | goto failed1; |
| 7612 | Py_DECREF(key); |
| 7613 | Py_DECREF(value); |
| 7614 | } |
| 7615 | return result; |
| 7616 | failed1: |
| 7617 | Py_XDECREF(key); |
| 7618 | Py_XDECREF(value); |
| 7619 | Py_DECREF(result); |
| 7620 | return NULL; |
| 7621 | } |
| 7622 | |
| 7623 | /* Create a three-level trie */ |
| 7624 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7625 | 16*count2 + 128*count3 - 1); |
| 7626 | if (!result) |
| 7627 | return PyErr_NoMemory(); |
| 7628 | PyObject_Init(result, &EncodingMapType); |
| 7629 | mresult = (struct encoding_map*)result; |
| 7630 | mresult->count2 = count2; |
| 7631 | mresult->count3 = count3; |
| 7632 | mlevel1 = mresult->level1; |
| 7633 | mlevel2 = mresult->level23; |
| 7634 | mlevel3 = mresult->level23 + 16*count2; |
| 7635 | memcpy(mlevel1, level1, 32); |
| 7636 | memset(mlevel2, 0xFF, 16*count2); |
| 7637 | memset(mlevel3, 0, 128*count3); |
| 7638 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7639 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7640 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7641 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7642 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7643 | /* unmapped character */ |
| 7644 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7645 | o1 = ch>>11; |
| 7646 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7647 | i2 = 16*mlevel1[o1] + o2; |
| 7648 | if (mlevel2[i2] == 0xFF) |
| 7649 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7650 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7651 | i3 = 128*mlevel2[i2] + o3; |
| 7652 | mlevel3[i3] = i; |
| 7653 | } |
| 7654 | return result; |
| 7655 | } |
| 7656 | |
| 7657 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7658 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7659 | { |
| 7660 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7661 | int l1 = c>>11; |
| 7662 | int l2 = (c>>7) & 0xF; |
| 7663 | int l3 = c & 0x7F; |
| 7664 | int i; |
| 7665 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7666 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7667 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7668 | if (c == 0) |
| 7669 | return 0; |
| 7670 | /* level 1*/ |
| 7671 | i = map->level1[l1]; |
| 7672 | if (i == 0xFF) { |
| 7673 | return -1; |
| 7674 | } |
| 7675 | /* level 2*/ |
| 7676 | i = map->level23[16*i+l2]; |
| 7677 | if (i == 0xFF) { |
| 7678 | return -1; |
| 7679 | } |
| 7680 | /* level 3 */ |
| 7681 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7682 | if (i == 0) { |
| 7683 | return -1; |
| 7684 | } |
| 7685 | return i; |
| 7686 | } |
| 7687 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7688 | /* Lookup the character ch in the mapping. If the character |
| 7689 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7690 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7691 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7692 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7693 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7694 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7695 | PyObject *x; |
| 7696 | |
| 7697 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7698 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7699 | x = PyObject_GetItem(mapping, w); |
| 7700 | Py_DECREF(w); |
| 7701 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7702 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7703 | /* No mapping found means: mapping is undefined. */ |
| 7704 | PyErr_Clear(); |
| 7705 | x = Py_None; |
| 7706 | Py_INCREF(x); |
| 7707 | return x; |
| 7708 | } else |
| 7709 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7710 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7711 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7712 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7713 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7714 | long value = PyLong_AS_LONG(x); |
| 7715 | if (value < 0 || value > 255) { |
| 7716 | PyErr_SetString(PyExc_TypeError, |
| 7717 | "character mapping must be in range(256)"); |
| 7718 | Py_DECREF(x); |
| 7719 | return NULL; |
| 7720 | } |
| 7721 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7722 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7723 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7724 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7725 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7726 | /* wrong return value */ |
| 7727 | PyErr_Format(PyExc_TypeError, |
| 7728 | "character mapping must return integer, bytes or None, not %.400s", |
| 7729 | x->ob_type->tp_name); |
| 7730 | Py_DECREF(x); |
| 7731 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7732 | } |
| 7733 | } |
| 7734 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7735 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7736 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7737 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7738 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7739 | /* exponentially overallocate to minimize reallocations */ |
| 7740 | if (requiredsize < 2*outsize) |
| 7741 | requiredsize = 2*outsize; |
| 7742 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7743 | return -1; |
| 7744 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7745 | } |
| 7746 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7747 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7748 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7749 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7750 | /* 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] | 7751 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7752 | space is available. Return a new reference to the object that |
| 7753 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7754 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7755 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7756 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7757 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7758 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7759 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7760 | PyObject *rep; |
| 7761 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7762 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7763 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7764 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7765 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7766 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7767 | if (res == -1) |
| 7768 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7769 | if (outsize<requiredsize) |
| 7770 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7771 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7772 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7773 | outstart[(*outpos)++] = (char)res; |
| 7774 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7775 | } |
| 7776 | |
| 7777 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7778 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7779 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7780 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7781 | Py_DECREF(rep); |
| 7782 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7783 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7784 | if (PyLong_Check(rep)) { |
| 7785 | Py_ssize_t requiredsize = *outpos+1; |
| 7786 | if (outsize<requiredsize) |
| 7787 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7788 | Py_DECREF(rep); |
| 7789 | return enc_EXCEPTION; |
| 7790 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7791 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7792 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7793 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7794 | else { |
| 7795 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7796 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7797 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7798 | if (outsize<requiredsize) |
| 7799 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7800 | Py_DECREF(rep); |
| 7801 | return enc_EXCEPTION; |
| 7802 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7803 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7804 | memcpy(outstart + *outpos, repchars, repsize); |
| 7805 | *outpos += repsize; |
| 7806 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7807 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7808 | Py_DECREF(rep); |
| 7809 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7810 | } |
| 7811 | |
| 7812 | /* handle an error in PyUnicode_EncodeCharmap |
| 7813 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7814 | static int |
| 7815 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7816 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7817 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7818 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7819 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7820 | { |
| 7821 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7822 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7823 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7824 | enum PyUnicode_Kind kind; |
| 7825 | void *data; |
| 7826 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7827 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7828 | Py_ssize_t collstartpos = *inpos; |
| 7829 | Py_ssize_t collendpos = *inpos+1; |
| 7830 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7831 | char *encoding = "charmap"; |
| 7832 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7833 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7834 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7835 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7836 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7837 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7838 | return -1; |
| 7839 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7840 | /* find all unencodable characters */ |
| 7841 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7842 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7843 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7844 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7845 | val = encoding_map_lookup(ch, mapping); |
| 7846 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7847 | break; |
| 7848 | ++collendpos; |
| 7849 | continue; |
| 7850 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7851 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7852 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 7853 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7854 | if (rep==NULL) |
| 7855 | return -1; |
| 7856 | else if (rep!=Py_None) { |
| 7857 | Py_DECREF(rep); |
| 7858 | break; |
| 7859 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7860 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7861 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7862 | } |
| 7863 | /* cache callback name lookup |
| 7864 | * (if not done yet, i.e. it's the first error) */ |
| 7865 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7866 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7867 | *known_errorHandler = 1; |
| 7868 | else if (!strcmp(errors, "replace")) |
| 7869 | *known_errorHandler = 2; |
| 7870 | else if (!strcmp(errors, "ignore")) |
| 7871 | *known_errorHandler = 3; |
| 7872 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7873 | *known_errorHandler = 4; |
| 7874 | else |
| 7875 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7876 | } |
| 7877 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7878 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7879 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7880 | return -1; |
| 7881 | case 2: /* replace */ |
| 7882 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7883 | x = charmapencode_output('?', mapping, res, respos); |
| 7884 | if (x==enc_EXCEPTION) { |
| 7885 | return -1; |
| 7886 | } |
| 7887 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7888 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7889 | return -1; |
| 7890 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7891 | } |
| 7892 | /* fall through */ |
| 7893 | case 3: /* ignore */ |
| 7894 | *inpos = collendpos; |
| 7895 | break; |
| 7896 | case 4: /* xmlcharrefreplace */ |
| 7897 | /* generate replacement (temporarily (mis)uses p) */ |
| 7898 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7899 | char buffer[2+29+1+1]; |
| 7900 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7901 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7902 | for (cp = buffer; *cp; ++cp) { |
| 7903 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7904 | if (x==enc_EXCEPTION) |
| 7905 | return -1; |
| 7906 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7907 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7908 | return -1; |
| 7909 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7910 | } |
| 7911 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7912 | *inpos = collendpos; |
| 7913 | break; |
| 7914 | default: |
| 7915 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7916 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7917 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7918 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7919 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7920 | if (PyBytes_Check(repunicode)) { |
| 7921 | /* Directly copy bytes result to output. */ |
| 7922 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7923 | Py_ssize_t requiredsize; |
| 7924 | repsize = PyBytes_Size(repunicode); |
| 7925 | requiredsize = *respos + repsize; |
| 7926 | if (requiredsize > outsize) |
| 7927 | /* Make room for all additional bytes. */ |
| 7928 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7929 | Py_DECREF(repunicode); |
| 7930 | return -1; |
| 7931 | } |
| 7932 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7933 | PyBytes_AsString(repunicode), repsize); |
| 7934 | *respos += repsize; |
| 7935 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7936 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7937 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7938 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7939 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7940 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7941 | Py_DECREF(repunicode); |
| 7942 | return -1; |
| 7943 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 7944 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7945 | data = PyUnicode_DATA(repunicode); |
| 7946 | kind = PyUnicode_KIND(repunicode); |
| 7947 | for (index = 0; index < repsize; index++) { |
| 7948 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 7949 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7950 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7951 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7952 | return -1; |
| 7953 | } |
| 7954 | else if (x==enc_FAILED) { |
| 7955 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7956 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7957 | return -1; |
| 7958 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7959 | } |
| 7960 | *inpos = newpos; |
| 7961 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7962 | } |
| 7963 | return 0; |
| 7964 | } |
| 7965 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7966 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7967 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 7968 | PyObject *mapping, |
| 7969 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7970 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7971 | /* output object */ |
| 7972 | PyObject *res = NULL; |
| 7973 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7974 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7975 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7976 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7977 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7978 | PyObject *errorHandler = NULL; |
| 7979 | PyObject *exc = NULL; |
| 7980 | /* the following variable is used for caching string comparisons |
| 7981 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7982 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7983 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7984 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7985 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7986 | return NULL; |
| 7987 | size = PyUnicode_GET_LENGTH(unicode); |
| 7988 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7989 | /* Default to Latin-1 */ |
| 7990 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7991 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7992 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7993 | /* allocate enough for a simple encoding without |
| 7994 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7995 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7996 | if (res == NULL) |
| 7997 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7998 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7999 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8000 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8001 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8002 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8003 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8004 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8005 | if (x==enc_EXCEPTION) /* error */ |
| 8006 | goto onError; |
| 8007 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8008 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8009 | &exc, |
| 8010 | &known_errorHandler, &errorHandler, errors, |
| 8011 | &res, &respos)) { |
| 8012 | goto onError; |
| 8013 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8014 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8015 | else |
| 8016 | /* done with this character => adjust input position */ |
| 8017 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8018 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8019 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8020 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8021 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8022 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8023 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8024 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8025 | Py_XDECREF(exc); |
| 8026 | Py_XDECREF(errorHandler); |
| 8027 | return res; |
| 8028 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8029 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8030 | Py_XDECREF(res); |
| 8031 | Py_XDECREF(exc); |
| 8032 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8033 | return NULL; |
| 8034 | } |
| 8035 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8036 | /* Deprecated */ |
| 8037 | PyObject * |
| 8038 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8039 | Py_ssize_t size, |
| 8040 | PyObject *mapping, |
| 8041 | const char *errors) |
| 8042 | { |
| 8043 | PyObject *result; |
| 8044 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8045 | if (unicode == NULL) |
| 8046 | return NULL; |
| 8047 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8048 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8049 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8050 | } |
| 8051 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8052 | PyObject * |
| 8053 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8054 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8055 | { |
| 8056 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8057 | PyErr_BadArgument(); |
| 8058 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8059 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8060 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8061 | } |
| 8062 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8063 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8064 | static void |
| 8065 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8066 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8067 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8068 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8069 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8070 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8071 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8072 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8073 | } |
| 8074 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8075 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8076 | goto onError; |
| 8077 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8078 | goto onError; |
| 8079 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8080 | goto onError; |
| 8081 | return; |
| 8082 | onError: |
| 8083 | Py_DECREF(*exceptionObject); |
| 8084 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8085 | } |
| 8086 | } |
| 8087 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8088 | /* error handling callback helper: |
| 8089 | build arguments, call the callback and check the arguments, |
| 8090 | put the result into newpos and return the replacement string, which |
| 8091 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8092 | static PyObject * |
| 8093 | unicode_translate_call_errorhandler(const char *errors, |
| 8094 | PyObject **errorHandler, |
| 8095 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8096 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8097 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8098 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8099 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8100 | 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] | 8101 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8102 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8103 | PyObject *restuple; |
| 8104 | PyObject *resunicode; |
| 8105 | |
| 8106 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8107 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8108 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8109 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8110 | } |
| 8111 | |
| 8112 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8113 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8114 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8115 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8116 | |
| 8117 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8118 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8119 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8120 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8121 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8122 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8123 | Py_DECREF(restuple); |
| 8124 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8125 | } |
| 8126 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8127 | &resunicode, &i_newpos)) { |
| 8128 | Py_DECREF(restuple); |
| 8129 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8130 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8131 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8132 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8133 | else |
| 8134 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8135 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8136 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8137 | Py_DECREF(restuple); |
| 8138 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8139 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8140 | Py_INCREF(resunicode); |
| 8141 | Py_DECREF(restuple); |
| 8142 | return resunicode; |
| 8143 | } |
| 8144 | |
| 8145 | /* Lookup the character ch in the mapping and put the result in result, |
| 8146 | which must be decrefed by the caller. |
| 8147 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8148 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8149 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8150 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8151 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8152 | PyObject *x; |
| 8153 | |
| 8154 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8155 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8156 | x = PyObject_GetItem(mapping, w); |
| 8157 | Py_DECREF(w); |
| 8158 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8159 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8160 | /* No mapping found means: use 1:1 mapping. */ |
| 8161 | PyErr_Clear(); |
| 8162 | *result = NULL; |
| 8163 | return 0; |
| 8164 | } else |
| 8165 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8166 | } |
| 8167 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8168 | *result = x; |
| 8169 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8170 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8171 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8172 | long value = PyLong_AS_LONG(x); |
| 8173 | long max = PyUnicode_GetMax(); |
| 8174 | if (value < 0 || value > max) { |
| 8175 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8176 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8177 | Py_DECREF(x); |
| 8178 | return -1; |
| 8179 | } |
| 8180 | *result = x; |
| 8181 | return 0; |
| 8182 | } |
| 8183 | else if (PyUnicode_Check(x)) { |
| 8184 | *result = x; |
| 8185 | return 0; |
| 8186 | } |
| 8187 | else { |
| 8188 | /* wrong return value */ |
| 8189 | PyErr_SetString(PyExc_TypeError, |
| 8190 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8191 | Py_DECREF(x); |
| 8192 | return -1; |
| 8193 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8194 | } |
| 8195 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8196 | if not reallocate and adjust various state variables. |
| 8197 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8198 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8199 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8200 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8201 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8202 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8203 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8204 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8205 | /* exponentially overallocate to minimize reallocations */ |
| 8206 | if (requiredsize < 2 * oldsize) |
| 8207 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8208 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8209 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8210 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8211 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8212 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8213 | } |
| 8214 | return 0; |
| 8215 | } |
| 8216 | /* lookup the character, put the result in the output string and adjust |
| 8217 | various state variables. Return a new reference to the object that |
| 8218 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8219 | undefined (in which case no character was written). |
| 8220 | The called must decref result. |
| 8221 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8222 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8223 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8224 | PyObject *mapping, Py_UCS4 **output, |
| 8225 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8226 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8227 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8228 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8229 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8230 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8231 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8232 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8233 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8234 | } |
| 8235 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8236 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8237 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8238 | /* 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] | 8239 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8240 | } |
| 8241 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8242 | Py_ssize_t repsize; |
| 8243 | if (PyUnicode_READY(*res) == -1) |
| 8244 | return -1; |
| 8245 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8246 | if (repsize==1) { |
| 8247 | /* 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] | 8248 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8249 | } |
| 8250 | else if (repsize!=0) { |
| 8251 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8252 | Py_ssize_t requiredsize = *opos + |
| 8253 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8254 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8255 | Py_ssize_t i; |
| 8256 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8257 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8258 | for(i = 0; i < repsize; i++) |
| 8259 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8260 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8261 | } |
| 8262 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8263 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8264 | return 0; |
| 8265 | } |
| 8266 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8267 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8268 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8269 | PyObject *mapping, |
| 8270 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8271 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8272 | /* input object */ |
| 8273 | char *idata; |
| 8274 | Py_ssize_t size, i; |
| 8275 | int kind; |
| 8276 | /* output buffer */ |
| 8277 | Py_UCS4 *output = NULL; |
| 8278 | Py_ssize_t osize; |
| 8279 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8280 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8281 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8282 | char *reason = "character maps to <undefined>"; |
| 8283 | PyObject *errorHandler = NULL; |
| 8284 | PyObject *exc = NULL; |
| 8285 | /* the following variable is used for caching string comparisons |
| 8286 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8287 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8288 | int known_errorHandler = -1; |
| 8289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8290 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8291 | PyErr_BadArgument(); |
| 8292 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8293 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8294 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8295 | if (PyUnicode_READY(input) == -1) |
| 8296 | return NULL; |
| 8297 | idata = (char*)PyUnicode_DATA(input); |
| 8298 | kind = PyUnicode_KIND(input); |
| 8299 | size = PyUnicode_GET_LENGTH(input); |
| 8300 | i = 0; |
| 8301 | |
| 8302 | if (size == 0) { |
| 8303 | Py_INCREF(input); |
| 8304 | return input; |
| 8305 | } |
| 8306 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8307 | /* allocate enough for a simple 1:1 translation without |
| 8308 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8309 | osize = size; |
| 8310 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8311 | opos = 0; |
| 8312 | if (output == NULL) { |
| 8313 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8314 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8315 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8316 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8317 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8318 | /* try to encode it */ |
| 8319 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8320 | if (charmaptranslate_output(input, i, mapping, |
| 8321 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8322 | Py_XDECREF(x); |
| 8323 | goto onError; |
| 8324 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8325 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8326 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8327 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8328 | else { /* untranslatable character */ |
| 8329 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8330 | Py_ssize_t repsize; |
| 8331 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8332 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8333 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8334 | Py_ssize_t collstart = i; |
| 8335 | Py_ssize_t collend = i+1; |
| 8336 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8337 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8338 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8339 | while (collend < size) { |
| 8340 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8341 | goto onError; |
| 8342 | Py_XDECREF(x); |
| 8343 | if (x!=Py_None) |
| 8344 | break; |
| 8345 | ++collend; |
| 8346 | } |
| 8347 | /* cache callback name lookup |
| 8348 | * (if not done yet, i.e. it's the first error) */ |
| 8349 | if (known_errorHandler==-1) { |
| 8350 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8351 | known_errorHandler = 1; |
| 8352 | else if (!strcmp(errors, "replace")) |
| 8353 | known_errorHandler = 2; |
| 8354 | else if (!strcmp(errors, "ignore")) |
| 8355 | known_errorHandler = 3; |
| 8356 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8357 | known_errorHandler = 4; |
| 8358 | else |
| 8359 | known_errorHandler = 0; |
| 8360 | } |
| 8361 | switch (known_errorHandler) { |
| 8362 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8363 | make_translate_exception(&exc, |
| 8364 | input, collstart, collend, reason); |
| 8365 | if (exc != NULL) |
| 8366 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8367 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8368 | case 2: /* replace */ |
| 8369 | /* 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] | 8370 | for (coll = collstart; coll<collend; coll++) |
| 8371 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8372 | /* fall through */ |
| 8373 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8374 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8375 | break; |
| 8376 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8377 | /* generate replacement (temporarily (mis)uses i) */ |
| 8378 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8379 | char buffer[2+29+1+1]; |
| 8380 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8381 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8382 | if (charmaptranslate_makespace(&output, &osize, |
| 8383 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8384 | goto onError; |
| 8385 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8386 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8387 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8388 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8389 | break; |
| 8390 | default: |
| 8391 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8392 | reason, input, &exc, |
| 8393 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8394 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8395 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8396 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8397 | Py_DECREF(repunicode); |
| 8398 | goto onError; |
| 8399 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8400 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8401 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8402 | if (charmaptranslate_makespace(&output, &osize, |
| 8403 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8404 | Py_DECREF(repunicode); |
| 8405 | goto onError; |
| 8406 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8407 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8408 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8409 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8410 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8411 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8412 | } |
| 8413 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8414 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8415 | if (!res) |
| 8416 | goto onError; |
| 8417 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8418 | Py_XDECREF(exc); |
| 8419 | Py_XDECREF(errorHandler); |
| 8420 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8421 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8422 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8423 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8424 | Py_XDECREF(exc); |
| 8425 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8426 | return NULL; |
| 8427 | } |
| 8428 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8429 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8430 | PyObject * |
| 8431 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8432 | Py_ssize_t size, |
| 8433 | PyObject *mapping, |
| 8434 | const char *errors) |
| 8435 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8436 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8437 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8438 | if (!unicode) |
| 8439 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8440 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8441 | Py_DECREF(unicode); |
| 8442 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8443 | } |
| 8444 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8445 | PyObject * |
| 8446 | PyUnicode_Translate(PyObject *str, |
| 8447 | PyObject *mapping, |
| 8448 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8449 | { |
| 8450 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8452 | str = PyUnicode_FromObject(str); |
| 8453 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8454 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8455 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8456 | Py_DECREF(str); |
| 8457 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8458 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8459 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8460 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8461 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8462 | { |
| 8463 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8464 | called as a callback from fixup() which does it already. */ |
| 8465 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8466 | const int kind = PyUnicode_KIND(self); |
| 8467 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8468 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8469 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8470 | Py_ssize_t i; |
| 8471 | |
| 8472 | for (i = 0; i < len; ++i) { |
| 8473 | ch = PyUnicode_READ(kind, data, i); |
| 8474 | fixed = 0; |
| 8475 | if (ch > 127) { |
| 8476 | if (Py_UNICODE_ISSPACE(ch)) |
| 8477 | fixed = ' '; |
| 8478 | else { |
| 8479 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8480 | if (decimal >= 0) |
| 8481 | fixed = '0' + decimal; |
| 8482 | } |
| 8483 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8484 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8485 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8486 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8487 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8488 | else |
| 8489 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8490 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8491 | } |
| 8492 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8493 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8494 | } |
| 8495 | |
| 8496 | PyObject * |
| 8497 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8498 | { |
| 8499 | if (!PyUnicode_Check(unicode)) { |
| 8500 | PyErr_BadInternalCall(); |
| 8501 | return NULL; |
| 8502 | } |
| 8503 | if (PyUnicode_READY(unicode) == -1) |
| 8504 | return NULL; |
| 8505 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8506 | /* If the string is already ASCII, just return the same string */ |
| 8507 | Py_INCREF(unicode); |
| 8508 | return unicode; |
| 8509 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8510 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8511 | } |
| 8512 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8513 | PyObject * |
| 8514 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8515 | Py_ssize_t length) |
| 8516 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8517 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8518 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8519 | Py_UCS4 maxchar; |
| 8520 | enum PyUnicode_Kind kind; |
| 8521 | void *data; |
| 8522 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8523 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8524 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8525 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8526 | if (ch > 127) { |
| 8527 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8528 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8529 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8530 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8531 | } |
| 8532 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8533 | |
| 8534 | /* Copy to a new string */ |
| 8535 | decimal = PyUnicode_New(length, maxchar); |
| 8536 | if (decimal == NULL) |
| 8537 | return decimal; |
| 8538 | kind = PyUnicode_KIND(decimal); |
| 8539 | data = PyUnicode_DATA(decimal); |
| 8540 | /* Iterate over code points */ |
| 8541 | for (i = 0; i < length; i++) { |
| 8542 | Py_UNICODE ch = s[i]; |
| 8543 | if (ch > 127) { |
| 8544 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8545 | if (decimal >= 0) |
| 8546 | ch = '0' + decimal; |
| 8547 | } |
| 8548 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8549 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8550 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8551 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8552 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8553 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8554 | int |
| 8555 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8556 | Py_ssize_t length, |
| 8557 | char *output, |
| 8558 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8559 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8560 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8561 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8562 | enum PyUnicode_Kind kind; |
| 8563 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8564 | |
| 8565 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8566 | PyErr_BadArgument(); |
| 8567 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8568 | } |
| 8569 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8570 | unicode = PyUnicode_FromUnicode(s, length); |
| 8571 | if (unicode == NULL) |
| 8572 | return -1; |
| 8573 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8574 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8575 | Py_DECREF(unicode); |
| 8576 | return -1; |
| 8577 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8578 | kind = PyUnicode_KIND(unicode); |
| 8579 | data = PyUnicode_DATA(unicode); |
| 8580 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8581 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8582 | PyObject *exc; |
| 8583 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8584 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8585 | Py_ssize_t startpos; |
| 8586 | |
| 8587 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8588 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8589 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8590 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8591 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8592 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8593 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8594 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8595 | if (decimal >= 0) { |
| 8596 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8597 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8598 | continue; |
| 8599 | } |
| 8600 | if (0 < ch && ch < 256) { |
| 8601 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8602 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8603 | continue; |
| 8604 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8605 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8606 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8607 | exc = NULL; |
| 8608 | raise_encode_exception(&exc, "decimal", unicode, |
| 8609 | startpos, startpos+1, |
| 8610 | "invalid decimal Unicode string"); |
| 8611 | Py_XDECREF(exc); |
| 8612 | Py_DECREF(unicode); |
| 8613 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8614 | } |
| 8615 | /* 0-terminate the output string */ |
| 8616 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8617 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8618 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8619 | } |
| 8620 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8621 | /* --- Helpers ------------------------------------------------------------ */ |
| 8622 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8623 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8624 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8625 | Py_ssize_t start, |
| 8626 | Py_ssize_t end) |
| 8627 | { |
| 8628 | int kind1, kind2, kind; |
| 8629 | void *buf1, *buf2; |
| 8630 | Py_ssize_t len1, len2, result; |
| 8631 | |
| 8632 | kind1 = PyUnicode_KIND(s1); |
| 8633 | kind2 = PyUnicode_KIND(s2); |
| 8634 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8635 | buf1 = PyUnicode_DATA(s1); |
| 8636 | buf2 = PyUnicode_DATA(s2); |
| 8637 | if (kind1 != kind) |
| 8638 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8639 | if (!buf1) |
| 8640 | return -2; |
| 8641 | if (kind2 != kind) |
| 8642 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8643 | if (!buf2) { |
| 8644 | if (kind1 != kind) PyMem_Free(buf1); |
| 8645 | return -2; |
| 8646 | } |
| 8647 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8648 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8649 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8650 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8651 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8652 | case PyUnicode_1BYTE_KIND: |
| 8653 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8654 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8655 | else |
| 8656 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8657 | break; |
| 8658 | case PyUnicode_2BYTE_KIND: |
| 8659 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8660 | break; |
| 8661 | case PyUnicode_4BYTE_KIND: |
| 8662 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8663 | break; |
| 8664 | default: |
| 8665 | assert(0); result = -2; |
| 8666 | } |
| 8667 | } |
| 8668 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8669 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8670 | case PyUnicode_1BYTE_KIND: |
| 8671 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8672 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8673 | else |
| 8674 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8675 | break; |
| 8676 | case PyUnicode_2BYTE_KIND: |
| 8677 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8678 | break; |
| 8679 | case PyUnicode_4BYTE_KIND: |
| 8680 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8681 | break; |
| 8682 | default: |
| 8683 | assert(0); result = -2; |
| 8684 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8685 | } |
| 8686 | |
| 8687 | if (kind1 != kind) |
| 8688 | PyMem_Free(buf1); |
| 8689 | if (kind2 != kind) |
| 8690 | PyMem_Free(buf2); |
| 8691 | |
| 8692 | return result; |
| 8693 | } |
| 8694 | |
| 8695 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8696 | _PyUnicode_InsertThousandsGrouping( |
| 8697 | PyObject *unicode, Py_ssize_t index, |
| 8698 | Py_ssize_t n_buffer, |
| 8699 | void *digits, Py_ssize_t n_digits, |
| 8700 | Py_ssize_t min_width, |
| 8701 | const char *grouping, PyObject *thousands_sep, |
| 8702 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8703 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8704 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8705 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8706 | Py_ssize_t thousands_sep_len; |
| 8707 | Py_ssize_t len; |
| 8708 | |
| 8709 | if (unicode != NULL) { |
| 8710 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8711 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8712 | } |
| 8713 | else { |
| 8714 | kind = PyUnicode_1BYTE_KIND; |
| 8715 | data = NULL; |
| 8716 | } |
| 8717 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8718 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8719 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8720 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8721 | if (thousands_sep_kind < kind) { |
| 8722 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8723 | if (!thousands_sep_data) |
| 8724 | return -1; |
| 8725 | } |
| 8726 | else { |
| 8727 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8728 | if (!data) |
| 8729 | return -1; |
| 8730 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8731 | } |
| 8732 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8733 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8734 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8735 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8736 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8737 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8738 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8739 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8740 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8741 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8742 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8743 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8744 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8745 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8746 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8747 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8748 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8749 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8750 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8751 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8752 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8753 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8754 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8755 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8756 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8757 | break; |
| 8758 | default: |
| 8759 | assert(0); |
| 8760 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8761 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8762 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8763 | if (thousands_sep_kind < kind) |
| 8764 | PyMem_Free(thousands_sep_data); |
| 8765 | else |
| 8766 | PyMem_Free(data); |
| 8767 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8768 | if (unicode == NULL) { |
| 8769 | *maxchar = 127; |
| 8770 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8771 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 8772 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8773 | } |
| 8774 | } |
| 8775 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8776 | } |
| 8777 | |
| 8778 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8779 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8780 | #define ADJUST_INDICES(start, end, len) \ |
| 8781 | if (end > len) \ |
| 8782 | end = len; \ |
| 8783 | else if (end < 0) { \ |
| 8784 | end += len; \ |
| 8785 | if (end < 0) \ |
| 8786 | end = 0; \ |
| 8787 | } \ |
| 8788 | if (start < 0) { \ |
| 8789 | start += len; \ |
| 8790 | if (start < 0) \ |
| 8791 | start = 0; \ |
| 8792 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8793 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8794 | Py_ssize_t |
| 8795 | PyUnicode_Count(PyObject *str, |
| 8796 | PyObject *substr, |
| 8797 | Py_ssize_t start, |
| 8798 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8799 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8800 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8801 | PyObject* str_obj; |
| 8802 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8803 | int kind1, kind2, kind; |
| 8804 | void *buf1 = NULL, *buf2 = NULL; |
| 8805 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8806 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8807 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8808 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8809 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8810 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8811 | if (!sub_obj) { |
| 8812 | Py_DECREF(str_obj); |
| 8813 | return -1; |
| 8814 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8815 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8816 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8817 | Py_DECREF(str_obj); |
| 8818 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8819 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8820 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8821 | kind1 = PyUnicode_KIND(str_obj); |
| 8822 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8823 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8824 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8825 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8826 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8827 | if (kind2 > kind) { |
| 8828 | Py_DECREF(sub_obj); |
| 8829 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8830 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8831 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8832 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8833 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8834 | if (!buf2) |
| 8835 | goto onError; |
| 8836 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8837 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8838 | |
| 8839 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8840 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8841 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8842 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8843 | result = asciilib_count( |
| 8844 | ((Py_UCS1*)buf1) + start, end - start, |
| 8845 | buf2, len2, PY_SSIZE_T_MAX |
| 8846 | ); |
| 8847 | else |
| 8848 | result = ucs1lib_count( |
| 8849 | ((Py_UCS1*)buf1) + start, end - start, |
| 8850 | buf2, len2, PY_SSIZE_T_MAX |
| 8851 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8852 | break; |
| 8853 | case PyUnicode_2BYTE_KIND: |
| 8854 | result = ucs2lib_count( |
| 8855 | ((Py_UCS2*)buf1) + start, end - start, |
| 8856 | buf2, len2, PY_SSIZE_T_MAX |
| 8857 | ); |
| 8858 | break; |
| 8859 | case PyUnicode_4BYTE_KIND: |
| 8860 | result = ucs4lib_count( |
| 8861 | ((Py_UCS4*)buf1) + start, end - start, |
| 8862 | buf2, len2, PY_SSIZE_T_MAX |
| 8863 | ); |
| 8864 | break; |
| 8865 | default: |
| 8866 | assert(0); result = 0; |
| 8867 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8868 | |
| 8869 | Py_DECREF(sub_obj); |
| 8870 | Py_DECREF(str_obj); |
| 8871 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8872 | if (kind2 != kind) |
| 8873 | PyMem_Free(buf2); |
| 8874 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8875 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8876 | onError: |
| 8877 | Py_DECREF(sub_obj); |
| 8878 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8879 | if (kind2 != kind && buf2) |
| 8880 | PyMem_Free(buf2); |
| 8881 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8882 | } |
| 8883 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8884 | Py_ssize_t |
| 8885 | PyUnicode_Find(PyObject *str, |
| 8886 | PyObject *sub, |
| 8887 | Py_ssize_t start, |
| 8888 | Py_ssize_t end, |
| 8889 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8890 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8891 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8893 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8894 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8895 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8896 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8897 | if (!sub) { |
| 8898 | Py_DECREF(str); |
| 8899 | return -2; |
| 8900 | } |
| 8901 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 8902 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8903 | Py_DECREF(str); |
| 8904 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8905 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8906 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8907 | result = any_find_slice(direction, |
| 8908 | str, sub, start, end |
| 8909 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8910 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8911 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8912 | Py_DECREF(sub); |
| 8913 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8914 | return result; |
| 8915 | } |
| 8916 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8917 | Py_ssize_t |
| 8918 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8919 | Py_ssize_t start, Py_ssize_t end, |
| 8920 | int direction) |
| 8921 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8922 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8923 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8924 | if (PyUnicode_READY(str) == -1) |
| 8925 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8926 | if (start < 0 || end < 0) { |
| 8927 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8928 | return -2; |
| 8929 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8930 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8931 | end = PyUnicode_GET_LENGTH(str); |
| 8932 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8933 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 8934 | kind, end-start, ch, direction); |
| 8935 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8936 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8937 | else |
| 8938 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8939 | } |
| 8940 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8941 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8942 | tailmatch(PyObject *self, |
| 8943 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8944 | Py_ssize_t start, |
| 8945 | Py_ssize_t end, |
| 8946 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8947 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8948 | int kind_self; |
| 8949 | int kind_sub; |
| 8950 | void *data_self; |
| 8951 | void *data_sub; |
| 8952 | Py_ssize_t offset; |
| 8953 | Py_ssize_t i; |
| 8954 | Py_ssize_t end_sub; |
| 8955 | |
| 8956 | if (PyUnicode_READY(self) == -1 || |
| 8957 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 8958 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8959 | |
| 8960 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8961 | return 1; |
| 8962 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8963 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8964 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8965 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8966 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8967 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8968 | kind_self = PyUnicode_KIND(self); |
| 8969 | data_self = PyUnicode_DATA(self); |
| 8970 | kind_sub = PyUnicode_KIND(substring); |
| 8971 | data_sub = PyUnicode_DATA(substring); |
| 8972 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8973 | |
| 8974 | if (direction > 0) |
| 8975 | offset = end; |
| 8976 | else |
| 8977 | offset = start; |
| 8978 | |
| 8979 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8980 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8981 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8982 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8983 | /* If both are of the same kind, memcmp is sufficient */ |
| 8984 | if (kind_self == kind_sub) { |
| 8985 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8986 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8987 | data_sub, |
| 8988 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8989 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8990 | } |
| 8991 | /* otherwise we have to compare each character by first accesing it */ |
| 8992 | else { |
| 8993 | /* We do not need to compare 0 and len(substring)-1 because |
| 8994 | the if statement above ensured already that they are equal |
| 8995 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8996 | for (i = 1; i < end_sub; ++i) { |
| 8997 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8998 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8999 | return 0; |
| 9000 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9001 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9002 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9003 | } |
| 9004 | |
| 9005 | return 0; |
| 9006 | } |
| 9007 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9008 | Py_ssize_t |
| 9009 | PyUnicode_Tailmatch(PyObject *str, |
| 9010 | PyObject *substr, |
| 9011 | Py_ssize_t start, |
| 9012 | Py_ssize_t end, |
| 9013 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9014 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9015 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9017 | str = PyUnicode_FromObject(str); |
| 9018 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9019 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9020 | substr = PyUnicode_FromObject(substr); |
| 9021 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9022 | Py_DECREF(str); |
| 9023 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9024 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9025 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9026 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9027 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9028 | Py_DECREF(str); |
| 9029 | Py_DECREF(substr); |
| 9030 | return result; |
| 9031 | } |
| 9032 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9033 | /* Apply fixfct filter to the Unicode object self and return a |
| 9034 | reference to the modified object */ |
| 9035 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9036 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9037 | fixup(PyObject *self, |
| 9038 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9039 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9040 | PyObject *u; |
| 9041 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9042 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9043 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9044 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9045 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9046 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9047 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9048 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9049 | /* fix functions return the new maximum character in a string, |
| 9050 | if the kind of the resulting unicode object does not change, |
| 9051 | everything is fine. Otherwise we need to change the string kind |
| 9052 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9053 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9054 | |
| 9055 | if (maxchar_new == 0) { |
| 9056 | /* no changes */; |
| 9057 | if (PyUnicode_CheckExact(self)) { |
| 9058 | Py_DECREF(u); |
| 9059 | Py_INCREF(self); |
| 9060 | return self; |
| 9061 | } |
| 9062 | else |
| 9063 | return u; |
| 9064 | } |
| 9065 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9066 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9067 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9068 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9069 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9070 | |
| 9071 | /* In case the maximum character changed, we need to |
| 9072 | convert the string to the new category. */ |
| 9073 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9074 | if (v == NULL) { |
| 9075 | Py_DECREF(u); |
| 9076 | return NULL; |
| 9077 | } |
| 9078 | if (maxchar_new > maxchar_old) { |
| 9079 | /* If the maxchar increased so that the kind changed, not all |
| 9080 | characters are representable anymore and we need to fix the |
| 9081 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9082 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9083 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9084 | maxchar_old = fixfct(v); |
| 9085 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9086 | } |
| 9087 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9088 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9089 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9090 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9091 | Py_DECREF(u); |
| 9092 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9093 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9094 | } |
| 9095 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9096 | static PyObject * |
| 9097 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9098 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9099 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9100 | char *resdata, *data = PyUnicode_DATA(self); |
| 9101 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9102 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9103 | res = PyUnicode_New(len, 127); |
| 9104 | if (res == NULL) |
| 9105 | return NULL; |
| 9106 | resdata = PyUnicode_DATA(res); |
| 9107 | if (lower) |
| 9108 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9109 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9110 | _Py_bytes_upper(resdata, data, len); |
| 9111 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9112 | } |
| 9113 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9114 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9115 | 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] | 9116 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9117 | Py_ssize_t j; |
| 9118 | int final_sigma; |
| 9119 | Py_UCS4 c; |
| 9120 | /* 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] | 9121 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9122 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9123 | |
| 9124 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9125 | */ |
| 9126 | for (j = i - 1; j >= 0; j--) { |
| 9127 | c = PyUnicode_READ(kind, data, j); |
| 9128 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9129 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9130 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9131 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9132 | if (final_sigma) { |
| 9133 | for (j = i + 1; j < length; j++) { |
| 9134 | c = PyUnicode_READ(kind, data, j); |
| 9135 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9136 | break; |
| 9137 | } |
| 9138 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9139 | } |
| 9140 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9141 | } |
| 9142 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9143 | static int |
| 9144 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9145 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9146 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9147 | /* Obscure special case. */ |
| 9148 | if (c == 0x3A3) { |
| 9149 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9150 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9151 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9152 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9153 | } |
| 9154 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9155 | static Py_ssize_t |
| 9156 | 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] | 9157 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9158 | Py_ssize_t i, k = 0; |
| 9159 | int n_res, j; |
| 9160 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9161 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9162 | c = PyUnicode_READ(kind, data, 0); |
| 9163 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9164 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9165 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9166 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9167 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9168 | for (i = 1; i < length; i++) { |
| 9169 | c = PyUnicode_READ(kind, data, i); |
| 9170 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9171 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9172 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9173 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9174 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9175 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9176 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9177 | } |
| 9178 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9179 | static Py_ssize_t |
| 9180 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9181 | Py_ssize_t i, k = 0; |
| 9182 | |
| 9183 | for (i = 0; i < length; i++) { |
| 9184 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9185 | int n_res, j; |
| 9186 | if (Py_UNICODE_ISUPPER(c)) { |
| 9187 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9188 | } |
| 9189 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9190 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9191 | } |
| 9192 | else { |
| 9193 | n_res = 1; |
| 9194 | mapped[0] = c; |
| 9195 | } |
| 9196 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9197 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9198 | res[k++] = mapped[j]; |
| 9199 | } |
| 9200 | } |
| 9201 | return k; |
| 9202 | } |
| 9203 | |
| 9204 | static Py_ssize_t |
| 9205 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9206 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9207 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9208 | Py_ssize_t i, k = 0; |
| 9209 | |
| 9210 | for (i = 0; i < length; i++) { |
| 9211 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9212 | int n_res, j; |
| 9213 | if (lower) |
| 9214 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9215 | else |
| 9216 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9217 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9218 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9219 | res[k++] = mapped[j]; |
| 9220 | } |
| 9221 | } |
| 9222 | return k; |
| 9223 | } |
| 9224 | |
| 9225 | static Py_ssize_t |
| 9226 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9227 | { |
| 9228 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9229 | } |
| 9230 | |
| 9231 | static Py_ssize_t |
| 9232 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9233 | { |
| 9234 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9235 | } |
| 9236 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9237 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9238 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9239 | { |
| 9240 | Py_ssize_t i, k = 0; |
| 9241 | |
| 9242 | for (i = 0; i < length; i++) { |
| 9243 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9244 | Py_UCS4 mapped[3]; |
| 9245 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9246 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9247 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9248 | res[k++] = mapped[j]; |
| 9249 | } |
| 9250 | } |
| 9251 | return k; |
| 9252 | } |
| 9253 | |
| 9254 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9255 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9256 | { |
| 9257 | Py_ssize_t i, k = 0; |
| 9258 | int previous_is_cased; |
| 9259 | |
| 9260 | previous_is_cased = 0; |
| 9261 | for (i = 0; i < length; i++) { |
| 9262 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9263 | Py_UCS4 mapped[3]; |
| 9264 | int n_res, j; |
| 9265 | |
| 9266 | if (previous_is_cased) |
| 9267 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9268 | else |
| 9269 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9270 | |
| 9271 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9272 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9273 | res[k++] = mapped[j]; |
| 9274 | } |
| 9275 | |
| 9276 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9277 | } |
| 9278 | return k; |
| 9279 | } |
| 9280 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9281 | static PyObject * |
| 9282 | case_operation(PyObject *self, |
| 9283 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9284 | { |
| 9285 | PyObject *res = NULL; |
| 9286 | Py_ssize_t length, newlength = 0; |
| 9287 | int kind, outkind; |
| 9288 | void *data, *outdata; |
| 9289 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9290 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9291 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9292 | |
| 9293 | kind = PyUnicode_KIND(self); |
| 9294 | data = PyUnicode_DATA(self); |
| 9295 | length = PyUnicode_GET_LENGTH(self); |
| 9296 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9297 | if (tmp == NULL) |
| 9298 | return PyErr_NoMemory(); |
| 9299 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9300 | res = PyUnicode_New(newlength, maxchar); |
| 9301 | if (res == NULL) |
| 9302 | goto leave; |
| 9303 | tmpend = tmp + newlength; |
| 9304 | outdata = PyUnicode_DATA(res); |
| 9305 | outkind = PyUnicode_KIND(res); |
| 9306 | switch (outkind) { |
| 9307 | case PyUnicode_1BYTE_KIND: |
| 9308 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9309 | break; |
| 9310 | case PyUnicode_2BYTE_KIND: |
| 9311 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9312 | break; |
| 9313 | case PyUnicode_4BYTE_KIND: |
| 9314 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9315 | break; |
| 9316 | default: |
| 9317 | assert(0); |
| 9318 | break; |
| 9319 | } |
| 9320 | leave: |
| 9321 | PyMem_FREE(tmp); |
| 9322 | return res; |
| 9323 | } |
| 9324 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9325 | PyObject * |
| 9326 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9327 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9328 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9329 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9330 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9331 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9332 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9333 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9334 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9335 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9336 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9337 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9338 | int use_memcpy; |
| 9339 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9340 | PyObject *last_obj; |
| 9341 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9342 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9343 | fseq = PySequence_Fast(seq, ""); |
| 9344 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9345 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9346 | } |
| 9347 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9348 | /* NOTE: the following code can't call back into Python code, |
| 9349 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9350 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9351 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9352 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9353 | /* If empty sequence, return u"". */ |
| 9354 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9355 | Py_DECREF(fseq); |
| 9356 | Py_INCREF(unicode_empty); |
| 9357 | res = unicode_empty; |
| 9358 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9359 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9360 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9361 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9362 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9363 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9364 | if (seqlen == 1) { |
| 9365 | if (PyUnicode_CheckExact(items[0])) { |
| 9366 | res = items[0]; |
| 9367 | Py_INCREF(res); |
| 9368 | Py_DECREF(fseq); |
| 9369 | return res; |
| 9370 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9371 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9372 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9373 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9374 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9375 | /* Set up sep and seplen */ |
| 9376 | if (separator == NULL) { |
| 9377 | /* fall back to a blank space separator */ |
| 9378 | sep = PyUnicode_FromOrdinal(' '); |
| 9379 | if (!sep) |
| 9380 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9381 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9382 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9383 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9384 | else { |
| 9385 | if (!PyUnicode_Check(separator)) { |
| 9386 | PyErr_Format(PyExc_TypeError, |
| 9387 | "separator: expected str instance," |
| 9388 | " %.80s found", |
| 9389 | Py_TYPE(separator)->tp_name); |
| 9390 | goto onError; |
| 9391 | } |
| 9392 | if (PyUnicode_READY(separator)) |
| 9393 | goto onError; |
| 9394 | sep = separator; |
| 9395 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9396 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9397 | /* inc refcount to keep this code path symmetric with the |
| 9398 | above case of a blank separator */ |
| 9399 | Py_INCREF(sep); |
| 9400 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9401 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9402 | } |
| 9403 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9404 | /* There are at least two things to join, or else we have a subclass |
| 9405 | * of str in the sequence. |
| 9406 | * Do a pre-pass to figure out the total amount of space we'll |
| 9407 | * need (sz), and see whether all argument are strings. |
| 9408 | */ |
| 9409 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9410 | #ifdef Py_DEBUG |
| 9411 | use_memcpy = 0; |
| 9412 | #else |
| 9413 | use_memcpy = 1; |
| 9414 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9415 | for (i = 0; i < seqlen; i++) { |
| 9416 | const Py_ssize_t old_sz = sz; |
| 9417 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9418 | if (!PyUnicode_Check(item)) { |
| 9419 | PyErr_Format(PyExc_TypeError, |
| 9420 | "sequence item %zd: expected str instance," |
| 9421 | " %.80s found", |
| 9422 | i, Py_TYPE(item)->tp_name); |
| 9423 | goto onError; |
| 9424 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9425 | if (PyUnicode_READY(item) == -1) |
| 9426 | goto onError; |
| 9427 | sz += PyUnicode_GET_LENGTH(item); |
| 9428 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9429 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9430 | if (i != 0) |
| 9431 | sz += seplen; |
| 9432 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9433 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9434 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9435 | goto onError; |
| 9436 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9437 | if (use_memcpy && last_obj != NULL) { |
| 9438 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9439 | use_memcpy = 0; |
| 9440 | } |
| 9441 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9442 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9443 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9444 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9445 | if (res == NULL) |
| 9446 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9447 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9448 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9449 | #ifdef Py_DEBUG |
| 9450 | use_memcpy = 0; |
| 9451 | #else |
| 9452 | if (use_memcpy) { |
| 9453 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9454 | kind = PyUnicode_KIND(res); |
| 9455 | if (seplen != 0) |
| 9456 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9457 | } |
| 9458 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9459 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9460 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9461 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9462 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9463 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9464 | if (use_memcpy) { |
| 9465 | Py_MEMCPY(res_data, |
| 9466 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9467 | kind * seplen); |
| 9468 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9469 | } |
| 9470 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9471 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9472 | res_offset += seplen; |
| 9473 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9474 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9475 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9476 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9477 | if (use_memcpy) { |
| 9478 | Py_MEMCPY(res_data, |
| 9479 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9480 | kind * itemlen); |
| 9481 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9482 | } |
| 9483 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9484 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9485 | res_offset += itemlen; |
| 9486 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9487 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9488 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9489 | if (use_memcpy) |
| 9490 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9491 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9492 | else |
| 9493 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9494 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9495 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9496 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9497 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9498 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9499 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9500 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9501 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9502 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9503 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9504 | return NULL; |
| 9505 | } |
| 9506 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9507 | #define FILL(kind, data, value, start, length) \ |
| 9508 | do { \ |
| 9509 | Py_ssize_t i_ = 0; \ |
| 9510 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9511 | switch ((kind)) { \ |
| 9512 | case PyUnicode_1BYTE_KIND: { \ |
| 9513 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9514 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9515 | break; \ |
| 9516 | } \ |
| 9517 | case PyUnicode_2BYTE_KIND: { \ |
| 9518 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9519 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9520 | break; \ |
| 9521 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9522 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9523 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9524 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9525 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9526 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9527 | } \ |
| 9528 | } \ |
| 9529 | } while (0) |
| 9530 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9531 | void |
| 9532 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9533 | Py_UCS4 fill_char) |
| 9534 | { |
| 9535 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9536 | const void *data = PyUnicode_DATA(unicode); |
| 9537 | assert(PyUnicode_IS_READY(unicode)); |
| 9538 | assert(unicode_modifiable(unicode)); |
| 9539 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9540 | assert(start >= 0); |
| 9541 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9542 | FILL(kind, data, fill_char, start, length); |
| 9543 | } |
| 9544 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9545 | Py_ssize_t |
| 9546 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9547 | Py_UCS4 fill_char) |
| 9548 | { |
| 9549 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9550 | |
| 9551 | if (!PyUnicode_Check(unicode)) { |
| 9552 | PyErr_BadInternalCall(); |
| 9553 | return -1; |
| 9554 | } |
| 9555 | if (PyUnicode_READY(unicode) == -1) |
| 9556 | return -1; |
| 9557 | if (unicode_check_modifiable(unicode)) |
| 9558 | return -1; |
| 9559 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9560 | if (start < 0) { |
| 9561 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9562 | return -1; |
| 9563 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9564 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9565 | PyErr_SetString(PyExc_ValueError, |
| 9566 | "fill character is bigger than " |
| 9567 | "the string maximum character"); |
| 9568 | return -1; |
| 9569 | } |
| 9570 | |
| 9571 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9572 | length = Py_MIN(maxlen, length); |
| 9573 | if (length <= 0) |
| 9574 | return 0; |
| 9575 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9576 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9577 | return length; |
| 9578 | } |
| 9579 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9580 | static PyObject * |
| 9581 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9582 | Py_ssize_t left, |
| 9583 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9584 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9585 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9586 | PyObject *u; |
| 9587 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9588 | int kind; |
| 9589 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9590 | |
| 9591 | if (left < 0) |
| 9592 | left = 0; |
| 9593 | if (right < 0) |
| 9594 | right = 0; |
| 9595 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9596 | if (left == 0 && right == 0) |
| 9597 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9598 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9599 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9600 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9601 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9602 | return NULL; |
| 9603 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9604 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9605 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9606 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9607 | if (!u) |
| 9608 | return NULL; |
| 9609 | |
| 9610 | kind = PyUnicode_KIND(u); |
| 9611 | data = PyUnicode_DATA(u); |
| 9612 | if (left) |
| 9613 | FILL(kind, data, fill, 0, left); |
| 9614 | if (right) |
| 9615 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9616 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9617 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9618 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9619 | } |
| 9620 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9621 | PyObject * |
| 9622 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9623 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9624 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9625 | |
| 9626 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9627 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9628 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9629 | if (PyUnicode_READY(string) == -1) { |
| 9630 | Py_DECREF(string); |
| 9631 | return NULL; |
| 9632 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9633 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9634 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9635 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9636 | if (PyUnicode_IS_ASCII(string)) |
| 9637 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9638 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9639 | PyUnicode_GET_LENGTH(string), keepends); |
| 9640 | else |
| 9641 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9642 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9643 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9644 | break; |
| 9645 | case PyUnicode_2BYTE_KIND: |
| 9646 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9647 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9648 | PyUnicode_GET_LENGTH(string), keepends); |
| 9649 | break; |
| 9650 | case PyUnicode_4BYTE_KIND: |
| 9651 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9652 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9653 | PyUnicode_GET_LENGTH(string), keepends); |
| 9654 | break; |
| 9655 | default: |
| 9656 | assert(0); |
| 9657 | list = 0; |
| 9658 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9659 | Py_DECREF(string); |
| 9660 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9661 | } |
| 9662 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9663 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9664 | split(PyObject *self, |
| 9665 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9666 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9667 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9668 | int kind1, kind2, kind; |
| 9669 | void *buf1, *buf2; |
| 9670 | Py_ssize_t len1, len2; |
| 9671 | PyObject* out; |
| 9672 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9673 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9674 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9675 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9676 | if (PyUnicode_READY(self) == -1) |
| 9677 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9678 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9679 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9680 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9681 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9682 | if (PyUnicode_IS_ASCII(self)) |
| 9683 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9684 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9685 | PyUnicode_GET_LENGTH(self), maxcount |
| 9686 | ); |
| 9687 | else |
| 9688 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9689 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9690 | PyUnicode_GET_LENGTH(self), maxcount |
| 9691 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9692 | case PyUnicode_2BYTE_KIND: |
| 9693 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9694 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9695 | PyUnicode_GET_LENGTH(self), maxcount |
| 9696 | ); |
| 9697 | case PyUnicode_4BYTE_KIND: |
| 9698 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9699 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9700 | PyUnicode_GET_LENGTH(self), maxcount |
| 9701 | ); |
| 9702 | default: |
| 9703 | assert(0); |
| 9704 | return NULL; |
| 9705 | } |
| 9706 | |
| 9707 | if (PyUnicode_READY(substring) == -1) |
| 9708 | return NULL; |
| 9709 | |
| 9710 | kind1 = PyUnicode_KIND(self); |
| 9711 | kind2 = PyUnicode_KIND(substring); |
| 9712 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9713 | buf1 = PyUnicode_DATA(self); |
| 9714 | buf2 = PyUnicode_DATA(substring); |
| 9715 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9716 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9717 | if (!buf1) |
| 9718 | return NULL; |
| 9719 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9720 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9721 | if (!buf2) { |
| 9722 | if (kind1 != kind) PyMem_Free(buf1); |
| 9723 | return NULL; |
| 9724 | } |
| 9725 | len1 = PyUnicode_GET_LENGTH(self); |
| 9726 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9727 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9728 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9729 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9730 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9731 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9732 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9733 | else |
| 9734 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9735 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9736 | break; |
| 9737 | case PyUnicode_2BYTE_KIND: |
| 9738 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9739 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9740 | break; |
| 9741 | case PyUnicode_4BYTE_KIND: |
| 9742 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9743 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9744 | break; |
| 9745 | default: |
| 9746 | out = NULL; |
| 9747 | } |
| 9748 | if (kind1 != kind) |
| 9749 | PyMem_Free(buf1); |
| 9750 | if (kind2 != kind) |
| 9751 | PyMem_Free(buf2); |
| 9752 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9753 | } |
| 9754 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9755 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9756 | rsplit(PyObject *self, |
| 9757 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9758 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9759 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9760 | int kind1, kind2, kind; |
| 9761 | void *buf1, *buf2; |
| 9762 | Py_ssize_t len1, len2; |
| 9763 | PyObject* out; |
| 9764 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9765 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9766 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9767 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9768 | if (PyUnicode_READY(self) == -1) |
| 9769 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9770 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9771 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9772 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9773 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9774 | if (PyUnicode_IS_ASCII(self)) |
| 9775 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9776 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9777 | PyUnicode_GET_LENGTH(self), maxcount |
| 9778 | ); |
| 9779 | else |
| 9780 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9781 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9782 | PyUnicode_GET_LENGTH(self), maxcount |
| 9783 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9784 | case PyUnicode_2BYTE_KIND: |
| 9785 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9786 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9787 | PyUnicode_GET_LENGTH(self), maxcount |
| 9788 | ); |
| 9789 | case PyUnicode_4BYTE_KIND: |
| 9790 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9791 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9792 | PyUnicode_GET_LENGTH(self), maxcount |
| 9793 | ); |
| 9794 | default: |
| 9795 | assert(0); |
| 9796 | return NULL; |
| 9797 | } |
| 9798 | |
| 9799 | if (PyUnicode_READY(substring) == -1) |
| 9800 | return NULL; |
| 9801 | |
| 9802 | kind1 = PyUnicode_KIND(self); |
| 9803 | kind2 = PyUnicode_KIND(substring); |
| 9804 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9805 | buf1 = PyUnicode_DATA(self); |
| 9806 | buf2 = PyUnicode_DATA(substring); |
| 9807 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9808 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9809 | if (!buf1) |
| 9810 | return NULL; |
| 9811 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9812 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9813 | if (!buf2) { |
| 9814 | if (kind1 != kind) PyMem_Free(buf1); |
| 9815 | return NULL; |
| 9816 | } |
| 9817 | len1 = PyUnicode_GET_LENGTH(self); |
| 9818 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9819 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9820 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9821 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9822 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9823 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9824 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9825 | else |
| 9826 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9827 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9828 | break; |
| 9829 | case PyUnicode_2BYTE_KIND: |
| 9830 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9831 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9832 | break; |
| 9833 | case PyUnicode_4BYTE_KIND: |
| 9834 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9835 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9836 | break; |
| 9837 | default: |
| 9838 | out = NULL; |
| 9839 | } |
| 9840 | if (kind1 != kind) |
| 9841 | PyMem_Free(buf1); |
| 9842 | if (kind2 != kind) |
| 9843 | PyMem_Free(buf2); |
| 9844 | return out; |
| 9845 | } |
| 9846 | |
| 9847 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9848 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9849 | 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] | 9850 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9851 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9852 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9853 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9854 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9855 | else |
| 9856 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9857 | case PyUnicode_2BYTE_KIND: |
| 9858 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9859 | case PyUnicode_4BYTE_KIND: |
| 9860 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9861 | } |
| 9862 | assert(0); |
| 9863 | return -1; |
| 9864 | } |
| 9865 | |
| 9866 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9867 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9868 | 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] | 9869 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 9870 | switch (kind) { |
| 9871 | case PyUnicode_1BYTE_KIND: |
| 9872 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9873 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9874 | else |
| 9875 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9876 | case PyUnicode_2BYTE_KIND: |
| 9877 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9878 | case PyUnicode_4BYTE_KIND: |
| 9879 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9880 | } |
| 9881 | assert(0); |
| 9882 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9883 | } |
| 9884 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9885 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9886 | replace(PyObject *self, PyObject *str1, |
| 9887 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9888 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9889 | PyObject *u; |
| 9890 | char *sbuf = PyUnicode_DATA(self); |
| 9891 | char *buf1 = PyUnicode_DATA(str1); |
| 9892 | char *buf2 = PyUnicode_DATA(str2); |
| 9893 | int srelease = 0, release1 = 0, release2 = 0; |
| 9894 | int skind = PyUnicode_KIND(self); |
| 9895 | int kind1 = PyUnicode_KIND(str1); |
| 9896 | int kind2 = PyUnicode_KIND(str2); |
| 9897 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9898 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9899 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9900 | int mayshrink; |
| 9901 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9902 | |
| 9903 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9904 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9905 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9906 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9907 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 9908 | if (str1 == str2) |
| 9909 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9910 | if (skind < kind1) |
| 9911 | /* substring too wide to be present */ |
| 9912 | goto nothing; |
| 9913 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9914 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9915 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 9916 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 9917 | result string. */ |
| 9918 | mayshrink = (maxchar_str2 < maxchar); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9919 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9920 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9921 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9922 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9923 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9924 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9925 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9926 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9927 | Py_UCS4 u1, u2; |
| 9928 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9929 | Py_ssize_t index, pos; |
| 9930 | char *src; |
| 9931 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9932 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9933 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 9934 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9935 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9936 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9937 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9938 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9939 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9940 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9941 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9942 | |
| 9943 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 9944 | index = 0; |
| 9945 | src = sbuf; |
| 9946 | while (--maxcount) |
| 9947 | { |
| 9948 | pos++; |
| 9949 | src += pos * PyUnicode_KIND(self); |
| 9950 | slen -= pos; |
| 9951 | index += pos; |
| 9952 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 9953 | if (pos < 0) |
| 9954 | break; |
| 9955 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 9956 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9957 | } |
| 9958 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9959 | int rkind = skind; |
| 9960 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9961 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9962 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9963 | if (kind1 < rkind) { |
| 9964 | /* widen substring */ |
| 9965 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9966 | if (!buf1) goto error; |
| 9967 | release1 = 1; |
| 9968 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9969 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9970 | if (i < 0) |
| 9971 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9972 | if (rkind > kind2) { |
| 9973 | /* widen replacement */ |
| 9974 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9975 | if (!buf2) goto error; |
| 9976 | release2 = 1; |
| 9977 | } |
| 9978 | else if (rkind < kind2) { |
| 9979 | /* widen self and buf1 */ |
| 9980 | rkind = kind2; |
| 9981 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 9982 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9983 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9984 | if (!sbuf) goto error; |
| 9985 | srelease = 1; |
| 9986 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9987 | if (!buf1) goto error; |
| 9988 | release1 = 1; |
| 9989 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9990 | u = PyUnicode_New(slen, maxchar); |
| 9991 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9992 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9993 | assert(PyUnicode_KIND(u) == rkind); |
| 9994 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9995 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9996 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9997 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9998 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9999 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10000 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10001 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10002 | |
| 10003 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10004 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10005 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10006 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10007 | if (i == -1) |
| 10008 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10009 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10010 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10011 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10012 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10013 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10014 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10015 | } |
| 10016 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10017 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10018 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10019 | int rkind = skind; |
| 10020 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10021 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10022 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10023 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10024 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10025 | if (!buf1) goto error; |
| 10026 | release1 = 1; |
| 10027 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10028 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10029 | if (n == 0) |
| 10030 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10031 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10032 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10033 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10034 | if (!buf2) goto error; |
| 10035 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10036 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10037 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10038 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10039 | rkind = kind2; |
| 10040 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10041 | if (!sbuf) goto error; |
| 10042 | srelease = 1; |
| 10043 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10044 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10045 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10046 | if (!buf1) goto error; |
| 10047 | release1 = 1; |
| 10048 | } |
| 10049 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10050 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10051 | if (len2 > len1 && len2 - len1 > (PY_SSIZE_T_MAX - slen) / n) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10052 | PyErr_SetString(PyExc_OverflowError, |
| 10053 | "replace string is too long"); |
| 10054 | goto error; |
| 10055 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10056 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10057 | if (new_size == 0) { |
| 10058 | Py_INCREF(unicode_empty); |
| 10059 | u = unicode_empty; |
| 10060 | goto done; |
| 10061 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10062 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10063 | PyErr_SetString(PyExc_OverflowError, |
| 10064 | "replace string is too long"); |
| 10065 | goto error; |
| 10066 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10067 | u = PyUnicode_New(new_size, maxchar); |
| 10068 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10070 | assert(PyUnicode_KIND(u) == rkind); |
| 10071 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10072 | ires = i = 0; |
| 10073 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10074 | while (n-- > 0) { |
| 10075 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10076 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10077 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10078 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10079 | if (j == -1) |
| 10080 | break; |
| 10081 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10082 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10083 | memcpy(res + rkind * ires, |
| 10084 | sbuf + rkind * i, |
| 10085 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10086 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10087 | } |
| 10088 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10089 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10090 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10091 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10092 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10093 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10094 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10095 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10096 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10097 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10098 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10099 | memcpy(res + rkind * ires, |
| 10100 | sbuf + rkind * i, |
| 10101 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10102 | } |
| 10103 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10104 | /* interleave */ |
| 10105 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10106 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10107 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10108 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10109 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10110 | if (--n <= 0) |
| 10111 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10112 | memcpy(res + rkind * ires, |
| 10113 | sbuf + rkind * i, |
| 10114 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10115 | ires++; |
| 10116 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10117 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10118 | memcpy(res + rkind * ires, |
| 10119 | sbuf + rkind * i, |
| 10120 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10121 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10122 | } |
| 10123 | |
| 10124 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10125 | unicode_adjust_maxchar(&u); |
| 10126 | if (u == NULL) |
| 10127 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10128 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10129 | |
| 10130 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10131 | if (srelease) |
| 10132 | PyMem_FREE(sbuf); |
| 10133 | if (release1) |
| 10134 | PyMem_FREE(buf1); |
| 10135 | if (release2) |
| 10136 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10137 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10138 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10139 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10140 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10141 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10142 | if (srelease) |
| 10143 | PyMem_FREE(sbuf); |
| 10144 | if (release1) |
| 10145 | PyMem_FREE(buf1); |
| 10146 | if (release2) |
| 10147 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10148 | return unicode_result_unchanged(self); |
| 10149 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10150 | error: |
| 10151 | if (srelease && sbuf) |
| 10152 | PyMem_FREE(sbuf); |
| 10153 | if (release1 && buf1) |
| 10154 | PyMem_FREE(buf1); |
| 10155 | if (release2 && buf2) |
| 10156 | PyMem_FREE(buf2); |
| 10157 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10158 | } |
| 10159 | |
| 10160 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10161 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10162 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10163 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10164 | \n\ |
| 10165 | 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] | 10166 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10167 | |
| 10168 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10169 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10170 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10171 | if (PyUnicode_READY(self) == -1) |
| 10172 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10173 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10174 | } |
| 10175 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10176 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10177 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10178 | \n\ |
| 10179 | 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] | 10180 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10181 | |
| 10182 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10183 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10184 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10185 | if (PyUnicode_READY(self) == -1) |
| 10186 | return NULL; |
| 10187 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10188 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10189 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10190 | } |
| 10191 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10192 | PyDoc_STRVAR(casefold__doc__, |
| 10193 | "S.casefold() -> str\n\ |
| 10194 | \n\ |
| 10195 | Return a version of S suitable for caseless comparisons."); |
| 10196 | |
| 10197 | static PyObject * |
| 10198 | unicode_casefold(PyObject *self) |
| 10199 | { |
| 10200 | if (PyUnicode_READY(self) == -1) |
| 10201 | return NULL; |
| 10202 | if (PyUnicode_IS_ASCII(self)) |
| 10203 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10204 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10205 | } |
| 10206 | |
| 10207 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10208 | /* Argument converter. Coerces to a single unicode character */ |
| 10209 | |
| 10210 | static int |
| 10211 | convert_uc(PyObject *obj, void *addr) |
| 10212 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10213 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10214 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10215 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10216 | uniobj = PyUnicode_FromObject(obj); |
| 10217 | if (uniobj == NULL) { |
| 10218 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10219 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10220 | return 0; |
| 10221 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10222 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10223 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10224 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10225 | Py_DECREF(uniobj); |
| 10226 | return 0; |
| 10227 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10228 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10229 | Py_DECREF(uniobj); |
| 10230 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10231 | } |
| 10232 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10233 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10234 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10235 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10236 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10237 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10238 | |
| 10239 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10240 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10241 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10242 | Py_ssize_t marg, left; |
| 10243 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10244 | Py_UCS4 fillchar = ' '; |
| 10245 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10246 | 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] | 10247 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10248 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10249 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10250 | return NULL; |
| 10251 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10252 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10253 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10254 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10255 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10256 | left = marg / 2 + (marg & width & 1); |
| 10257 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10258 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10259 | } |
| 10260 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10261 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10262 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10263 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10264 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10265 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10266 | int kind1, kind2; |
| 10267 | void *data1, *data2; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10268 | Py_ssize_t len1, len2; |
| 10269 | Py_ssize_t i, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10270 | |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10271 | /* a string is equal to itself */ |
| 10272 | if (str1 == str2) |
| 10273 | return 0; |
| 10274 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10275 | kind1 = PyUnicode_KIND(str1); |
| 10276 | kind2 = PyUnicode_KIND(str2); |
| 10277 | data1 = PyUnicode_DATA(str1); |
| 10278 | data2 = PyUnicode_DATA(str2); |
| 10279 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10280 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10281 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10282 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10283 | if (kind1 == 1 && kind2 == 1) { |
| 10284 | int cmp = memcmp(data1, data2, len); |
| 10285 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10286 | if (cmp < 0) |
| 10287 | return -1; |
| 10288 | if (cmp > 0) |
| 10289 | return 1; |
| 10290 | } |
| 10291 | else { |
| 10292 | for (i = 0; i < len; ++i) { |
| 10293 | Py_UCS4 c1, c2; |
| 10294 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10295 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10296 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10297 | if (c1 != c2) |
| 10298 | return (c1 < c2) ? -1 : 1; |
| 10299 | } |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10300 | } |
| 10301 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10302 | if (len1 == len2) |
| 10303 | return 0; |
| 10304 | if (len1 < len2) |
| 10305 | return -1; |
| 10306 | else |
| 10307 | return 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10308 | } |
| 10309 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10310 | static int |
| 10311 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10312 | { |
| 10313 | int kind; |
| 10314 | void *data1, *data2; |
| 10315 | Py_ssize_t len; |
| 10316 | int cmp; |
| 10317 | |
| 10318 | /* a string is equal to itself */ |
| 10319 | if (str1 == str2) |
| 10320 | return 1; |
| 10321 | |
| 10322 | len = PyUnicode_GET_LENGTH(str1); |
| 10323 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10324 | return 0; |
| 10325 | kind = PyUnicode_KIND(str1); |
| 10326 | if (PyUnicode_KIND(str2) != kind) |
| 10327 | return 0; |
| 10328 | data1 = PyUnicode_DATA(str1); |
| 10329 | data2 = PyUnicode_DATA(str2); |
| 10330 | |
| 10331 | cmp = memcmp(data1, data2, len * kind); |
| 10332 | return (cmp == 0); |
| 10333 | } |
| 10334 | |
| 10335 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10336 | int |
| 10337 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10338 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10339 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10340 | if (PyUnicode_READY(left) == -1 || |
| 10341 | PyUnicode_READY(right) == -1) |
| 10342 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10343 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10344 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10345 | PyErr_Format(PyExc_TypeError, |
| 10346 | "Can't compare %.100s and %.100s", |
| 10347 | left->ob_type->tp_name, |
| 10348 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10349 | return -1; |
| 10350 | } |
| 10351 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10352 | int |
| 10353 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10354 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10355 | Py_ssize_t i; |
| 10356 | int kind; |
| 10357 | void *data; |
| 10358 | Py_UCS4 chr; |
| 10359 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10360 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10361 | if (PyUnicode_READY(uni) == -1) |
| 10362 | return -1; |
| 10363 | kind = PyUnicode_KIND(uni); |
| 10364 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10365 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10366 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10367 | if (chr != str[i]) |
| 10368 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10369 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10370 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10371 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10372 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10373 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10374 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10375 | return 0; |
| 10376 | } |
| 10377 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10378 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10379 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10380 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10381 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10382 | PyObject * |
| 10383 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10384 | { |
| 10385 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10386 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10387 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10388 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10389 | Py_RETURN_NOTIMPLEMENTED; |
| 10390 | |
| 10391 | if (PyUnicode_READY(left) == -1 || |
| 10392 | PyUnicode_READY(right) == -1) |
| 10393 | return NULL; |
| 10394 | |
| 10395 | if (op == Py_EQ || op == Py_NE) { |
| 10396 | result = unicode_compare_eq(left, right); |
| 10397 | if (op == Py_EQ) |
| 10398 | v = TEST_COND(result); |
| 10399 | else |
| 10400 | v = TEST_COND(!result); |
| 10401 | } |
| 10402 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10403 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10404 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10405 | /* Convert the return value to a Boolean */ |
| 10406 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10407 | case Py_LE: |
| 10408 | v = TEST_COND(result <= 0); |
| 10409 | break; |
| 10410 | case Py_GE: |
| 10411 | v = TEST_COND(result >= 0); |
| 10412 | break; |
| 10413 | case Py_LT: |
| 10414 | v = TEST_COND(result == -1); |
| 10415 | break; |
| 10416 | case Py_GT: |
| 10417 | v = TEST_COND(result == 1); |
| 10418 | break; |
| 10419 | default: |
| 10420 | PyErr_BadArgument(); |
| 10421 | return NULL; |
| 10422 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10423 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10424 | Py_INCREF(v); |
| 10425 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10426 | } |
| 10427 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10428 | int |
| 10429 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10430 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10431 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10432 | int kind1, kind2, kind; |
| 10433 | void *buf1, *buf2; |
| 10434 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10435 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10436 | |
| 10437 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10438 | sub = PyUnicode_FromObject(element); |
| 10439 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10440 | PyErr_Format(PyExc_TypeError, |
| 10441 | "'in <string>' requires string as left operand, not %s", |
| 10442 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10443 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10444 | } |
| 10445 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10446 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10447 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10448 | Py_DECREF(sub); |
| 10449 | return -1; |
| 10450 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10451 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10452 | Py_DECREF(sub); |
| 10453 | Py_DECREF(str); |
| 10454 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10455 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10456 | kind1 = PyUnicode_KIND(str); |
| 10457 | kind2 = PyUnicode_KIND(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10458 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10459 | buf1 = PyUnicode_DATA(str); |
| 10460 | buf2 = PyUnicode_DATA(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10461 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10462 | if (kind2 > kind) { |
| 10463 | Py_DECREF(sub); |
| 10464 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10465 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10466 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10467 | buf2 = _PyUnicode_AsKind(sub, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10468 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10469 | if (!buf2) { |
| 10470 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10471 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10472 | return -1; |
| 10473 | } |
| 10474 | len1 = PyUnicode_GET_LENGTH(str); |
| 10475 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10476 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10477 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10478 | case PyUnicode_1BYTE_KIND: |
| 10479 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10480 | break; |
| 10481 | case PyUnicode_2BYTE_KIND: |
| 10482 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10483 | break; |
| 10484 | case PyUnicode_4BYTE_KIND: |
| 10485 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10486 | break; |
| 10487 | default: |
| 10488 | result = -1; |
| 10489 | assert(0); |
| 10490 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10491 | |
| 10492 | Py_DECREF(str); |
| 10493 | Py_DECREF(sub); |
| 10494 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10495 | if (kind2 != kind) |
| 10496 | PyMem_Free(buf2); |
| 10497 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10498 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10499 | } |
| 10500 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10501 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10502 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10503 | PyObject * |
| 10504 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10505 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10506 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10507 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10508 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10509 | |
| 10510 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10511 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10512 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10513 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10514 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10515 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10516 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10517 | |
| 10518 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10519 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10520 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10521 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10522 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10523 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10524 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10525 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10526 | } |
| 10527 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10528 | u_len = PyUnicode_GET_LENGTH(u); |
| 10529 | v_len = PyUnicode_GET_LENGTH(v); |
| 10530 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10531 | PyErr_SetString(PyExc_OverflowError, |
| 10532 | "strings are too large to concat"); |
| 10533 | goto onError; |
| 10534 | } |
| 10535 | new_len = u_len + v_len; |
| 10536 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10537 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10538 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10539 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10540 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10541 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10542 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10543 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10544 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10545 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10546 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10547 | Py_DECREF(u); |
| 10548 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10549 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10550 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10551 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10552 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10553 | Py_XDECREF(u); |
| 10554 | Py_XDECREF(v); |
| 10555 | return NULL; |
| 10556 | } |
| 10557 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10558 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10559 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10560 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10561 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10562 | Py_UCS4 maxchar, maxchar2; |
| 10563 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10564 | |
| 10565 | if (p_left == NULL) { |
| 10566 | if (!PyErr_Occurred()) |
| 10567 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10568 | return; |
| 10569 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10570 | left = *p_left; |
Serhiy Storchaka | 6c83e73 | 2013-01-04 12:39:34 +0200 | [diff] [blame] | 10571 | if (right == NULL || left == NULL || !PyUnicode_Check(left)) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10572 | if (!PyErr_Occurred()) |
| 10573 | PyErr_BadInternalCall(); |
| 10574 | goto error; |
| 10575 | } |
| 10576 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10577 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10578 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10579 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10580 | goto error; |
| 10581 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10582 | /* Shortcuts */ |
| 10583 | if (left == unicode_empty) { |
| 10584 | Py_DECREF(left); |
| 10585 | Py_INCREF(right); |
| 10586 | *p_left = right; |
| 10587 | return; |
| 10588 | } |
| 10589 | if (right == unicode_empty) |
| 10590 | return; |
| 10591 | |
| 10592 | left_len = PyUnicode_GET_LENGTH(left); |
| 10593 | right_len = PyUnicode_GET_LENGTH(right); |
| 10594 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10595 | PyErr_SetString(PyExc_OverflowError, |
| 10596 | "strings are too large to concat"); |
| 10597 | goto error; |
| 10598 | } |
| 10599 | new_len = left_len + right_len; |
| 10600 | |
| 10601 | if (unicode_modifiable(left) |
| 10602 | && PyUnicode_CheckExact(right) |
| 10603 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10604 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10605 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10606 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10607 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10608 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10609 | { |
| 10610 | /* append inplace */ |
| 10611 | if (unicode_resize(p_left, new_len) != 0) { |
| 10612 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10613 | * deallocated so it cannot be put back into |
| 10614 | * 'variable'. The MemoryError is raised when there |
| 10615 | * is no value in 'variable', which might (very |
| 10616 | * remotely) be a cause of incompatibilities. |
| 10617 | */ |
| 10618 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10619 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10620 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10621 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10622 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10623 | else { |
| 10624 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10625 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10626 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10627 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10628 | /* Concat the two Unicode strings */ |
| 10629 | res = PyUnicode_New(new_len, maxchar); |
| 10630 | if (res == NULL) |
| 10631 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10632 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10633 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10634 | Py_DECREF(left); |
| 10635 | *p_left = res; |
| 10636 | } |
| 10637 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10638 | return; |
| 10639 | |
| 10640 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10641 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10642 | } |
| 10643 | |
| 10644 | void |
| 10645 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10646 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10647 | PyUnicode_Append(pleft, right); |
| 10648 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10649 | } |
| 10650 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10651 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10652 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10653 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10654 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10655 | 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] | 10656 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10657 | |
| 10658 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10659 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10660 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10661 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10662 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10663 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10664 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10665 | int kind1, kind2, kind; |
| 10666 | void *buf1, *buf2; |
| 10667 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10668 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10669 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10670 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10671 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10672 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10673 | kind1 = PyUnicode_KIND(self); |
| 10674 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10675 | if (kind2 > kind1) |
| 10676 | return PyLong_FromLong(0); |
| 10677 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10678 | buf1 = PyUnicode_DATA(self); |
| 10679 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10680 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10681 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10682 | if (!buf2) { |
| 10683 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10684 | return NULL; |
| 10685 | } |
| 10686 | len1 = PyUnicode_GET_LENGTH(self); |
| 10687 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10688 | |
| 10689 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10690 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10691 | case PyUnicode_1BYTE_KIND: |
| 10692 | iresult = ucs1lib_count( |
| 10693 | ((Py_UCS1*)buf1) + start, end - start, |
| 10694 | buf2, len2, PY_SSIZE_T_MAX |
| 10695 | ); |
| 10696 | break; |
| 10697 | case PyUnicode_2BYTE_KIND: |
| 10698 | iresult = ucs2lib_count( |
| 10699 | ((Py_UCS2*)buf1) + start, end - start, |
| 10700 | buf2, len2, PY_SSIZE_T_MAX |
| 10701 | ); |
| 10702 | break; |
| 10703 | case PyUnicode_4BYTE_KIND: |
| 10704 | iresult = ucs4lib_count( |
| 10705 | ((Py_UCS4*)buf1) + start, end - start, |
| 10706 | buf2, len2, PY_SSIZE_T_MAX |
| 10707 | ); |
| 10708 | break; |
| 10709 | default: |
| 10710 | assert(0); iresult = 0; |
| 10711 | } |
| 10712 | |
| 10713 | result = PyLong_FromSsize_t(iresult); |
| 10714 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10715 | if (kind2 != kind) |
| 10716 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10717 | |
| 10718 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10719 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10720 | return result; |
| 10721 | } |
| 10722 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10723 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10724 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10725 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10726 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10727 | 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] | 10728 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10729 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10730 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10731 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10732 | |
| 10733 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10734 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10735 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10736 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10737 | char *encoding = NULL; |
| 10738 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10739 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10740 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10741 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10742 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10743 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10744 | } |
| 10745 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10746 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10747 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10748 | \n\ |
| 10749 | 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] | 10750 | 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] | 10751 | |
| 10752 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10753 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10754 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10755 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10756 | Py_UCS4 ch; |
| 10757 | PyObject *u; |
| 10758 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10759 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10760 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10761 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10762 | |
| 10763 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10764 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10765 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10766 | if (PyUnicode_READY(self) == -1) |
| 10767 | return NULL; |
| 10768 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10769 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10770 | src_len = PyUnicode_GET_LENGTH(self); |
| 10771 | i = j = line_pos = 0; |
| 10772 | kind = PyUnicode_KIND(self); |
| 10773 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10774 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10775 | for (; i < src_len; i++) { |
| 10776 | ch = PyUnicode_READ(kind, src_data, i); |
| 10777 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10778 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10779 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10780 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10781 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10782 | goto overflow; |
| 10783 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10784 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10785 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10786 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10787 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10788 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10789 | goto overflow; |
| 10790 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10791 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10792 | if (ch == '\n' || ch == '\r') |
| 10793 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10794 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10795 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10796 | if (!found) |
| 10797 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10798 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10799 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10800 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10801 | if (!u) |
| 10802 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10803 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10804 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10805 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10806 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10807 | for (; i < src_len; i++) { |
| 10808 | ch = PyUnicode_READ(kind, src_data, i); |
| 10809 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10810 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10811 | incr = tabsize - (line_pos % tabsize); |
| 10812 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 10813 | FILL(kind, dest_data, ' ', j, incr); |
| 10814 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10815 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10816 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10817 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10818 | line_pos++; |
| 10819 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10820 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10821 | if (ch == '\n' || ch == '\r') |
| 10822 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10823 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10824 | } |
| 10825 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10826 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10827 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10828 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10829 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10830 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10831 | } |
| 10832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10833 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10834 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10835 | \n\ |
| 10836 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10837 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10838 | arguments start and end are interpreted as in slice notation.\n\ |
| 10839 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10840 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10841 | |
| 10842 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10843 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10844 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10845 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10846 | Py_ssize_t start; |
| 10847 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10848 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10849 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10850 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10851 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10852 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10853 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10854 | if (PyUnicode_READY(self) == -1) |
| 10855 | return NULL; |
| 10856 | if (PyUnicode_READY(substring) == -1) |
| 10857 | return NULL; |
| 10858 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10859 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10860 | |
| 10861 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10862 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10863 | if (result == -2) |
| 10864 | return NULL; |
| 10865 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10866 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10867 | } |
| 10868 | |
| 10869 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10870 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10871 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10872 | void *data; |
| 10873 | enum PyUnicode_Kind kind; |
| 10874 | Py_UCS4 ch; |
| 10875 | PyObject *res; |
| 10876 | |
| 10877 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 10878 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10879 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10880 | } |
| 10881 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 10882 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10883 | return NULL; |
| 10884 | } |
| 10885 | kind = PyUnicode_KIND(self); |
| 10886 | data = PyUnicode_DATA(self); |
| 10887 | ch = PyUnicode_READ(kind, data, index); |
| 10888 | if (ch < 256) |
| 10889 | return get_latin1_char(ch); |
| 10890 | |
| 10891 | res = PyUnicode_New(1, ch); |
| 10892 | if (res == NULL) |
| 10893 | return NULL; |
| 10894 | kind = PyUnicode_KIND(res); |
| 10895 | data = PyUnicode_DATA(res); |
| 10896 | PyUnicode_WRITE(kind, data, 0, ch); |
| 10897 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 10898 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10899 | } |
| 10900 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10901 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10902 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10903 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10904 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10905 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10906 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 10907 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10908 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 10909 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 10910 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 10911 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10912 | if (_PyUnicode_HASH(self) != -1) |
| 10913 | return _PyUnicode_HASH(self); |
| 10914 | if (PyUnicode_READY(self) == -1) |
| 10915 | return -1; |
| 10916 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 10917 | /* |
| 10918 | We make the hash of the empty string be 0, rather than using |
| 10919 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 10920 | */ |
| 10921 | if (len == 0) { |
| 10922 | _PyUnicode_HASH(self) = 0; |
| 10923 | return 0; |
| 10924 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10925 | |
| 10926 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10927 | #define HASH(P) \ |
| 10928 | x ^= (Py_uhash_t) *P << 7; \ |
| 10929 | while (--len >= 0) \ |
| 10930 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10931 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10932 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10933 | switch (PyUnicode_KIND(self)) { |
| 10934 | case PyUnicode_1BYTE_KIND: { |
| 10935 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10936 | HASH(c); |
| 10937 | break; |
| 10938 | } |
| 10939 | case PyUnicode_2BYTE_KIND: { |
| 10940 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10941 | HASH(s); |
| 10942 | break; |
| 10943 | } |
| 10944 | default: { |
| 10945 | Py_UCS4 *l; |
| 10946 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10947 | "Impossible switch case in unicode_hash"); |
| 10948 | l = PyUnicode_4BYTE_DATA(self); |
| 10949 | HASH(l); |
| 10950 | break; |
| 10951 | } |
| 10952 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10953 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 10954 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10955 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10956 | if (x == -1) |
| 10957 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10958 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10959 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10960 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10961 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10962 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10963 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10964 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10965 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10966 | 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] | 10967 | |
| 10968 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10969 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10970 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10971 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10972 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10973 | Py_ssize_t start; |
| 10974 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10975 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10976 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10977 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10978 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10979 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10980 | if (PyUnicode_READY(self) == -1) |
| 10981 | return NULL; |
| 10982 | if (PyUnicode_READY(substring) == -1) |
| 10983 | return NULL; |
| 10984 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10985 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10986 | |
| 10987 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10988 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10989 | if (result == -2) |
| 10990 | return NULL; |
| 10991 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10992 | if (result < 0) { |
| 10993 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10994 | return NULL; |
| 10995 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10996 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10997 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10998 | } |
| 10999 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11000 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11001 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11002 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11003 | 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] | 11004 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11005 | |
| 11006 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11007 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11008 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11009 | Py_ssize_t i, length; |
| 11010 | int kind; |
| 11011 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11012 | int cased; |
| 11013 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11014 | if (PyUnicode_READY(self) == -1) |
| 11015 | return NULL; |
| 11016 | length = PyUnicode_GET_LENGTH(self); |
| 11017 | kind = PyUnicode_KIND(self); |
| 11018 | data = PyUnicode_DATA(self); |
| 11019 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11020 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11021 | if (length == 1) |
| 11022 | return PyBool_FromLong( |
| 11023 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11024 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11025 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11026 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11027 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11028 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11029 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11030 | for (i = 0; i < length; i++) { |
| 11031 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11032 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11033 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11034 | return PyBool_FromLong(0); |
| 11035 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11036 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11037 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11038 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11039 | } |
| 11040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11041 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11042 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11043 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11044 | 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] | 11045 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11046 | |
| 11047 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11048 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11049 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11050 | Py_ssize_t i, length; |
| 11051 | int kind; |
| 11052 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11053 | int cased; |
| 11054 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11055 | if (PyUnicode_READY(self) == -1) |
| 11056 | return NULL; |
| 11057 | length = PyUnicode_GET_LENGTH(self); |
| 11058 | kind = PyUnicode_KIND(self); |
| 11059 | data = PyUnicode_DATA(self); |
| 11060 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11061 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11062 | if (length == 1) |
| 11063 | return PyBool_FromLong( |
| 11064 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11065 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11066 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11067 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11068 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11070 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11071 | for (i = 0; i < length; i++) { |
| 11072 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11073 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11074 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11075 | return PyBool_FromLong(0); |
| 11076 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11077 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11078 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11079 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11080 | } |
| 11081 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11082 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11083 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11084 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11085 | Return True if S is a titlecased string and there is at least one\n\ |
| 11086 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11087 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11088 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11089 | |
| 11090 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11091 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11092 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11093 | Py_ssize_t i, length; |
| 11094 | int kind; |
| 11095 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11096 | int cased, previous_is_cased; |
| 11097 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11098 | if (PyUnicode_READY(self) == -1) |
| 11099 | return NULL; |
| 11100 | length = PyUnicode_GET_LENGTH(self); |
| 11101 | kind = PyUnicode_KIND(self); |
| 11102 | data = PyUnicode_DATA(self); |
| 11103 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11104 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11105 | if (length == 1) { |
| 11106 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11107 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11108 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11109 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11110 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11111 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11112 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11113 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11114 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11115 | cased = 0; |
| 11116 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11117 | for (i = 0; i < length; i++) { |
| 11118 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11119 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11120 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11121 | if (previous_is_cased) |
| 11122 | return PyBool_FromLong(0); |
| 11123 | previous_is_cased = 1; |
| 11124 | cased = 1; |
| 11125 | } |
| 11126 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11127 | if (!previous_is_cased) |
| 11128 | return PyBool_FromLong(0); |
| 11129 | previous_is_cased = 1; |
| 11130 | cased = 1; |
| 11131 | } |
| 11132 | else |
| 11133 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11134 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11135 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11136 | } |
| 11137 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11138 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11139 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11140 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11141 | Return True if all characters in S are whitespace\n\ |
| 11142 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11143 | |
| 11144 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11145 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11146 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11147 | Py_ssize_t i, length; |
| 11148 | int kind; |
| 11149 | void *data; |
| 11150 | |
| 11151 | if (PyUnicode_READY(self) == -1) |
| 11152 | return NULL; |
| 11153 | length = PyUnicode_GET_LENGTH(self); |
| 11154 | kind = PyUnicode_KIND(self); |
| 11155 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11156 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11157 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11158 | if (length == 1) |
| 11159 | return PyBool_FromLong( |
| 11160 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11161 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11162 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11163 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11164 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11165 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11166 | for (i = 0; i < length; i++) { |
| 11167 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11168 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11169 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11170 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11171 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 | } |
| 11173 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11174 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11175 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11176 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11177 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11178 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11179 | |
| 11180 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11181 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11182 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11183 | Py_ssize_t i, length; |
| 11184 | int kind; |
| 11185 | void *data; |
| 11186 | |
| 11187 | if (PyUnicode_READY(self) == -1) |
| 11188 | return NULL; |
| 11189 | length = PyUnicode_GET_LENGTH(self); |
| 11190 | kind = PyUnicode_KIND(self); |
| 11191 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11192 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11193 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11194 | if (length == 1) |
| 11195 | return PyBool_FromLong( |
| 11196 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11197 | |
| 11198 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11199 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11200 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11201 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11202 | for (i = 0; i < length; i++) { |
| 11203 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11204 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11205 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11206 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11207 | } |
| 11208 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11209 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11210 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11211 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11212 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11213 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11214 | |
| 11215 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11216 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11217 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11218 | int kind; |
| 11219 | void *data; |
| 11220 | Py_ssize_t len, i; |
| 11221 | |
| 11222 | if (PyUnicode_READY(self) == -1) |
| 11223 | return NULL; |
| 11224 | |
| 11225 | kind = PyUnicode_KIND(self); |
| 11226 | data = PyUnicode_DATA(self); |
| 11227 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11228 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11229 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11230 | if (len == 1) { |
| 11231 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11232 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11233 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11234 | |
| 11235 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11236 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11237 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11238 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11239 | for (i = 0; i < len; i++) { |
| 11240 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11241 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11242 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11243 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11244 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11245 | } |
| 11246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11247 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11248 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11249 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11250 | 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] | 11251 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11252 | |
| 11253 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11254 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11255 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11256 | Py_ssize_t i, length; |
| 11257 | int kind; |
| 11258 | void *data; |
| 11259 | |
| 11260 | if (PyUnicode_READY(self) == -1) |
| 11261 | return NULL; |
| 11262 | length = PyUnicode_GET_LENGTH(self); |
| 11263 | kind = PyUnicode_KIND(self); |
| 11264 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11266 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11267 | if (length == 1) |
| 11268 | return PyBool_FromLong( |
| 11269 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11270 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11271 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11272 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11273 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11274 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11275 | for (i = 0; i < length; i++) { |
| 11276 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11277 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11278 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11279 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11280 | } |
| 11281 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11282 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11283 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11284 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11285 | Return True if all characters in S are digits\n\ |
| 11286 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11287 | |
| 11288 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11289 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11290 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11291 | Py_ssize_t i, length; |
| 11292 | int kind; |
| 11293 | void *data; |
| 11294 | |
| 11295 | if (PyUnicode_READY(self) == -1) |
| 11296 | return NULL; |
| 11297 | length = PyUnicode_GET_LENGTH(self); |
| 11298 | kind = PyUnicode_KIND(self); |
| 11299 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11300 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11301 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11302 | if (length == 1) { |
| 11303 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11304 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11305 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11306 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11307 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11308 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11309 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11310 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11311 | for (i = 0; i < length; i++) { |
| 11312 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11313 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11314 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11315 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11316 | } |
| 11317 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11318 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11319 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11320 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11321 | 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] | 11322 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11323 | |
| 11324 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11325 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11326 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11327 | Py_ssize_t i, length; |
| 11328 | int kind; |
| 11329 | void *data; |
| 11330 | |
| 11331 | if (PyUnicode_READY(self) == -1) |
| 11332 | return NULL; |
| 11333 | length = PyUnicode_GET_LENGTH(self); |
| 11334 | kind = PyUnicode_KIND(self); |
| 11335 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11336 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11337 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11338 | if (length == 1) |
| 11339 | return PyBool_FromLong( |
| 11340 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11341 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11342 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11343 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11344 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11345 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11346 | for (i = 0; i < length; i++) { |
| 11347 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11348 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11349 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11350 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11351 | } |
| 11352 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11353 | int |
| 11354 | PyUnicode_IsIdentifier(PyObject *self) |
| 11355 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11356 | int kind; |
| 11357 | void *data; |
| 11358 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11359 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11360 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11361 | if (PyUnicode_READY(self) == -1) { |
| 11362 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11363 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11364 | } |
| 11365 | |
| 11366 | /* Special case for empty strings */ |
| 11367 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11368 | return 0; |
| 11369 | kind = PyUnicode_KIND(self); |
| 11370 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11371 | |
| 11372 | /* PEP 3131 says that the first character must be in |
| 11373 | XID_Start and subsequent characters in XID_Continue, |
| 11374 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11375 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11376 | letters, digits, underscore). However, given the current |
| 11377 | definition of XID_Start and XID_Continue, it is sufficient |
| 11378 | to check just for these, except that _ must be allowed |
| 11379 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11380 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11381 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11382 | return 0; |
| 11383 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11384 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11385 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11386 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11387 | return 1; |
| 11388 | } |
| 11389 | |
| 11390 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11391 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11392 | \n\ |
| 11393 | Return True if S is a valid identifier according\n\ |
| 11394 | to the language definition."); |
| 11395 | |
| 11396 | static PyObject* |
| 11397 | unicode_isidentifier(PyObject *self) |
| 11398 | { |
| 11399 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11400 | } |
| 11401 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11402 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11403 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11404 | \n\ |
| 11405 | Return True if all characters in S are considered\n\ |
| 11406 | printable in repr() or S is empty, False otherwise."); |
| 11407 | |
| 11408 | static PyObject* |
| 11409 | unicode_isprintable(PyObject *self) |
| 11410 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11411 | Py_ssize_t i, length; |
| 11412 | int kind; |
| 11413 | void *data; |
| 11414 | |
| 11415 | if (PyUnicode_READY(self) == -1) |
| 11416 | return NULL; |
| 11417 | length = PyUnicode_GET_LENGTH(self); |
| 11418 | kind = PyUnicode_KIND(self); |
| 11419 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11420 | |
| 11421 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11422 | if (length == 1) |
| 11423 | return PyBool_FromLong( |
| 11424 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +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_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11428 | Py_RETURN_FALSE; |
| 11429 | } |
| 11430 | } |
| 11431 | Py_RETURN_TRUE; |
| 11432 | } |
| 11433 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11434 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11435 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11436 | \n\ |
| 11437 | 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] | 11438 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | |
| 11440 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11441 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11442 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11443 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11444 | } |
| 11445 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11446 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11447 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11448 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11449 | if (PyUnicode_READY(self) == -1) |
| 11450 | return -1; |
| 11451 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | } |
| 11453 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11454 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11455 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11456 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11457 | 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] | 11458 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11459 | |
| 11460 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11461 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11462 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11463 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11464 | Py_UCS4 fillchar = ' '; |
| 11465 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11466 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11467 | return NULL; |
| 11468 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11469 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11470 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11471 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11472 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11473 | return unicode_result_unchanged(self); |
| 11474 | |
| 11475 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11476 | } |
| 11477 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11478 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11479 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11480 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11481 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11482 | |
| 11483 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11484 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11485 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11486 | if (PyUnicode_READY(self) == -1) |
| 11487 | return NULL; |
| 11488 | if (PyUnicode_IS_ASCII(self)) |
| 11489 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11490 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11491 | } |
| 11492 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11493 | #define LEFTSTRIP 0 |
| 11494 | #define RIGHTSTRIP 1 |
| 11495 | #define BOTHSTRIP 2 |
| 11496 | |
| 11497 | /* Arrays indexed by above */ |
| 11498 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11499 | |
| 11500 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11501 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11502 | /* externally visible for str.strip(unicode) */ |
| 11503 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11504 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11505 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11506 | void *data; |
| 11507 | int kind; |
| 11508 | Py_ssize_t i, j, len; |
| 11509 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11510 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11511 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11512 | return NULL; |
| 11513 | |
| 11514 | kind = PyUnicode_KIND(self); |
| 11515 | data = PyUnicode_DATA(self); |
| 11516 | len = PyUnicode_GET_LENGTH(self); |
| 11517 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11518 | PyUnicode_DATA(sepobj), |
| 11519 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11520 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11521 | i = 0; |
| 11522 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11523 | while (i < len && |
| 11524 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11525 | i++; |
| 11526 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11527 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11528 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11529 | j = len; |
| 11530 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11531 | do { |
| 11532 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11533 | } while (j >= i && |
| 11534 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11535 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11536 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11537 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11538 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11539 | } |
| 11540 | |
| 11541 | PyObject* |
| 11542 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11543 | { |
| 11544 | unsigned char *data; |
| 11545 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11546 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11547 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11548 | if (PyUnicode_READY(self) == -1) |
| 11549 | return NULL; |
| 11550 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11551 | length = PyUnicode_GET_LENGTH(self); |
| 11552 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11553 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11554 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11555 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11556 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11557 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11558 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11559 | return NULL; |
| 11560 | } |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11561 | if (start >= length || end < start) { |
Victor Stinner | 3a7f7977 | 2012-05-03 03:36:40 +0200 | [diff] [blame] | 11562 | Py_INCREF(unicode_empty); |
| 11563 | return unicode_empty; |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11564 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11565 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11566 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11567 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11568 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11569 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11570 | } |
| 11571 | else { |
| 11572 | kind = PyUnicode_KIND(self); |
| 11573 | data = PyUnicode_1BYTE_DATA(self); |
| 11574 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11575 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11576 | length); |
| 11577 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11578 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11579 | |
| 11580 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11581 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11582 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11583 | int kind; |
| 11584 | void *data; |
| 11585 | Py_ssize_t len, i, j; |
| 11586 | |
| 11587 | if (PyUnicode_READY(self) == -1) |
| 11588 | return NULL; |
| 11589 | |
| 11590 | kind = PyUnicode_KIND(self); |
| 11591 | data = PyUnicode_DATA(self); |
| 11592 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11593 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11594 | i = 0; |
| 11595 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11596 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11597 | i++; |
| 11598 | } |
| 11599 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11600 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11601 | j = len; |
| 11602 | if (striptype != LEFTSTRIP) { |
| 11603 | do { |
| 11604 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11605 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11606 | j++; |
| 11607 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11608 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11609 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11610 | } |
| 11611 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11612 | |
| 11613 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11614 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11615 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11616 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11617 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11618 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11619 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11620 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11621 | if (sep != NULL && sep != Py_None) { |
| 11622 | if (PyUnicode_Check(sep)) |
| 11623 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11624 | else { |
| 11625 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11626 | "%s arg must be None or str", |
| 11627 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11628 | return NULL; |
| 11629 | } |
| 11630 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11631 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11632 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11633 | } |
| 11634 | |
| 11635 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11636 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11637 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11638 | \n\ |
| 11639 | Return a copy of the string S with leading and trailing\n\ |
| 11640 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11641 | 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] | 11642 | |
| 11643 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11644 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11645 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11646 | if (PyTuple_GET_SIZE(args) == 0) |
| 11647 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11648 | else |
| 11649 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11650 | } |
| 11651 | |
| 11652 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11653 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11654 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11655 | \n\ |
| 11656 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11657 | 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] | 11658 | |
| 11659 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11660 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11661 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11662 | if (PyTuple_GET_SIZE(args) == 0) |
| 11663 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11664 | else |
| 11665 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11666 | } |
| 11667 | |
| 11668 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11669 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11670 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11671 | \n\ |
| 11672 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11673 | 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] | 11674 | |
| 11675 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11676 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11677 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11678 | if (PyTuple_GET_SIZE(args) == 0) |
| 11679 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11680 | else |
| 11681 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11682 | } |
| 11683 | |
| 11684 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11685 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11686 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11687 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11688 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11689 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11690 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11691 | if (len < 1) { |
| 11692 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11693 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11694 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11695 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11696 | /* no repeat, return original string */ |
| 11697 | if (len == 1) |
| 11698 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11699 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11700 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11701 | return NULL; |
| 11702 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11703 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11704 | PyErr_SetString(PyExc_OverflowError, |
| 11705 | "repeated string is too long"); |
| 11706 | return NULL; |
| 11707 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11708 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11709 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11710 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11711 | if (!u) |
| 11712 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11713 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11714 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11715 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11716 | const int kind = PyUnicode_KIND(str); |
| 11717 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11718 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11719 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11720 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11721 | } |
| 11722 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11723 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11724 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11725 | ucs2[n] = fill_char; |
| 11726 | } else { |
| 11727 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11728 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11729 | for (n = 0; n < len; ++n) |
| 11730 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11731 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11732 | } |
| 11733 | else { |
| 11734 | /* number of characters copied this far */ |
| 11735 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11736 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11737 | char *to = (char *) PyUnicode_DATA(u); |
| 11738 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11739 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11740 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11741 | n = (done <= nchars-done) ? done : nchars-done; |
| 11742 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11743 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11744 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11745 | } |
| 11746 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11747 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11748 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11749 | } |
| 11750 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11751 | PyObject * |
| 11752 | PyUnicode_Replace(PyObject *obj, |
| 11753 | PyObject *subobj, |
| 11754 | PyObject *replobj, |
| 11755 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11756 | { |
| 11757 | PyObject *self; |
| 11758 | PyObject *str1; |
| 11759 | PyObject *str2; |
| 11760 | PyObject *result; |
| 11761 | |
| 11762 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11763 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11764 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11765 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11766 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11767 | Py_DECREF(self); |
| 11768 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11769 | } |
| 11770 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11771 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11772 | Py_DECREF(self); |
| 11773 | Py_DECREF(str1); |
| 11774 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11775 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11776 | if (PyUnicode_READY(self) == -1 || |
| 11777 | PyUnicode_READY(str1) == -1 || |
| 11778 | PyUnicode_READY(str2) == -1) |
| 11779 | result = NULL; |
| 11780 | else |
| 11781 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11782 | Py_DECREF(self); |
| 11783 | Py_DECREF(str1); |
| 11784 | Py_DECREF(str2); |
| 11785 | return result; |
| 11786 | } |
| 11787 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11788 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11789 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11790 | \n\ |
| 11791 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11792 | old replaced by new. If the optional argument count is\n\ |
| 11793 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11794 | |
| 11795 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11796 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11797 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11798 | PyObject *str1; |
| 11799 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11800 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11801 | PyObject *result; |
| 11802 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11803 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11804 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11805 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11806 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11807 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11808 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11809 | return NULL; |
| 11810 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11811 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11812 | Py_DECREF(str1); |
| 11813 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11814 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11815 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 11816 | result = NULL; |
| 11817 | else |
| 11818 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11819 | |
| 11820 | Py_DECREF(str1); |
| 11821 | Py_DECREF(str2); |
| 11822 | return result; |
| 11823 | } |
| 11824 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11825 | static PyObject * |
| 11826 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11827 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11828 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11829 | Py_ssize_t isize; |
| 11830 | Py_ssize_t osize, squote, dquote, i, o; |
| 11831 | Py_UCS4 max, quote; |
| 11832 | int ikind, okind; |
| 11833 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11834 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11835 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11836 | return NULL; |
| 11837 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11838 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11839 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11840 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11841 | /* Compute length of output, quote characters, and |
| 11842 | maximum character */ |
| 11843 | osize = 2; /* quotes */ |
| 11844 | max = 127; |
| 11845 | squote = dquote = 0; |
| 11846 | ikind = PyUnicode_KIND(unicode); |
| 11847 | for (i = 0; i < isize; i++) { |
| 11848 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11849 | switch (ch) { |
| 11850 | case '\'': squote++; osize++; break; |
| 11851 | case '"': dquote++; osize++; break; |
| 11852 | case '\\': case '\t': case '\r': case '\n': |
| 11853 | osize += 2; break; |
| 11854 | default: |
| 11855 | /* Fast-path ASCII */ |
| 11856 | if (ch < ' ' || ch == 0x7f) |
| 11857 | osize += 4; /* \xHH */ |
| 11858 | else if (ch < 0x7f) |
| 11859 | osize++; |
| 11860 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11861 | osize++; |
| 11862 | max = ch > max ? ch : max; |
| 11863 | } |
| 11864 | else if (ch < 0x100) |
| 11865 | osize += 4; /* \xHH */ |
| 11866 | else if (ch < 0x10000) |
| 11867 | osize += 6; /* \uHHHH */ |
| 11868 | else |
| 11869 | osize += 10; /* \uHHHHHHHH */ |
| 11870 | } |
| 11871 | } |
| 11872 | |
| 11873 | quote = '\''; |
| 11874 | if (squote) { |
| 11875 | if (dquote) |
| 11876 | /* Both squote and dquote present. Use squote, |
| 11877 | and escape them */ |
| 11878 | osize += squote; |
| 11879 | else |
| 11880 | quote = '"'; |
| 11881 | } |
| 11882 | |
| 11883 | repr = PyUnicode_New(osize, max); |
| 11884 | if (repr == NULL) |
| 11885 | return NULL; |
| 11886 | okind = PyUnicode_KIND(repr); |
| 11887 | odata = PyUnicode_DATA(repr); |
| 11888 | |
| 11889 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11890 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11891 | |
| 11892 | for (i = 0, o = 1; i < isize; i++) { |
| 11893 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11894 | |
| 11895 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11896 | if ((ch == quote) || (ch == '\\')) { |
| 11897 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11898 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11899 | continue; |
| 11900 | } |
| 11901 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11902 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11903 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11904 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11905 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11906 | } |
| 11907 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11908 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11909 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11910 | } |
| 11911 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11912 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11913 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11914 | } |
| 11915 | |
| 11916 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11917 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11918 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11919 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11920 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 11921 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11922 | } |
| 11923 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11924 | /* Copy ASCII characters as-is */ |
| 11925 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11926 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11927 | } |
| 11928 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11929 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11930 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11931 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11932 | (categories Z* and C* except ASCII space) |
| 11933 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11934 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11935 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11936 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11937 | if (ch <= 0xff) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11938 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11939 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 11940 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11941 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11942 | /* Map 16-bit characters to '\uxxxx' */ |
| 11943 | else if (ch <= 0xffff) { |
| 11944 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11945 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 11946 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 11947 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 11948 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11949 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11950 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11951 | else { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11952 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11953 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 11954 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 11955 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 11956 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11957 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 11958 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 11959 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 11960 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11961 | } |
| 11962 | } |
| 11963 | /* Copy characters as-is */ |
| 11964 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11965 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11966 | } |
| 11967 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11968 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11969 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 11970 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11971 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11972 | } |
| 11973 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11974 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11975 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11976 | \n\ |
| 11977 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11978 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11979 | arguments start and end are interpreted as in slice notation.\n\ |
| 11980 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11981 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11982 | |
| 11983 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11984 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11985 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11986 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11987 | Py_ssize_t start; |
| 11988 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11989 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11990 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11991 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11992 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11993 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11994 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11995 | if (PyUnicode_READY(self) == -1) |
| 11996 | return NULL; |
| 11997 | if (PyUnicode_READY(substring) == -1) |
| 11998 | return NULL; |
| 11999 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12000 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12001 | |
| 12002 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12003 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12004 | if (result == -2) |
| 12005 | return NULL; |
| 12006 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12007 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12008 | } |
| 12009 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12010 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12011 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12012 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12013 | 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] | 12014 | |
| 12015 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12016 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12017 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12018 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 12019 | Py_ssize_t start; |
| 12020 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12021 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12022 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12023 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12024 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12025 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12027 | if (PyUnicode_READY(self) == -1) |
| 12028 | return NULL; |
| 12029 | if (PyUnicode_READY(substring) == -1) |
| 12030 | return NULL; |
| 12031 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12032 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12033 | |
| 12034 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12035 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12036 | if (result == -2) |
| 12037 | return NULL; |
| 12038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12039 | if (result < 0) { |
| 12040 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12041 | return NULL; |
| 12042 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12043 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12044 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12045 | } |
| 12046 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12047 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12048 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12049 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12050 | 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] | 12051 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12052 | |
| 12053 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12054 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12055 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12056 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12057 | Py_UCS4 fillchar = ' '; |
| 12058 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12059 | 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] | 12060 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12061 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12062 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12063 | return NULL; |
| 12064 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12065 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12066 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12067 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12068 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12069 | } |
| 12070 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12071 | PyObject * |
| 12072 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12073 | { |
| 12074 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12075 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12076 | s = PyUnicode_FromObject(s); |
| 12077 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12078 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12079 | if (sep != NULL) { |
| 12080 | sep = PyUnicode_FromObject(sep); |
| 12081 | if (sep == NULL) { |
| 12082 | Py_DECREF(s); |
| 12083 | return NULL; |
| 12084 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12085 | } |
| 12086 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12087 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12088 | |
| 12089 | Py_DECREF(s); |
| 12090 | Py_XDECREF(sep); |
| 12091 | return result; |
| 12092 | } |
| 12093 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12094 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12095 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12096 | \n\ |
| 12097 | Return a list of the words in S, using sep as the\n\ |
| 12098 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12099 | 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] | 12100 | whitespace string is a separator and empty strings are\n\ |
| 12101 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12102 | |
| 12103 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12104 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12105 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12106 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12107 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12108 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12109 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12110 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12111 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12112 | return NULL; |
| 12113 | |
| 12114 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12115 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12116 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12117 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12118 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12119 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12120 | } |
| 12121 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12122 | PyObject * |
| 12123 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12124 | { |
| 12125 | PyObject* str_obj; |
| 12126 | PyObject* sep_obj; |
| 12127 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12128 | int kind1, kind2, kind; |
| 12129 | void *buf1 = NULL, *buf2 = NULL; |
| 12130 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12131 | |
| 12132 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12133 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12134 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12135 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12136 | if (!sep_obj) { |
| 12137 | Py_DECREF(str_obj); |
| 12138 | return NULL; |
| 12139 | } |
| 12140 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12141 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12142 | Py_DECREF(str_obj); |
| 12143 | return NULL; |
| 12144 | } |
| 12145 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12146 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12147 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12148 | kind = Py_MAX(kind1, kind2); |
| 12149 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12150 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12151 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12152 | if (!buf1) |
| 12153 | goto onError; |
| 12154 | buf2 = PyUnicode_DATA(sep_obj); |
| 12155 | if (kind2 != kind) |
| 12156 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12157 | if (!buf2) |
| 12158 | goto onError; |
| 12159 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12160 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12161 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12162 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12163 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12164 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12165 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12166 | else |
| 12167 | 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] | 12168 | break; |
| 12169 | case PyUnicode_2BYTE_KIND: |
| 12170 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12171 | break; |
| 12172 | case PyUnicode_4BYTE_KIND: |
| 12173 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12174 | break; |
| 12175 | default: |
| 12176 | assert(0); |
| 12177 | out = 0; |
| 12178 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12179 | |
| 12180 | Py_DECREF(sep_obj); |
| 12181 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12182 | if (kind1 != kind) |
| 12183 | PyMem_Free(buf1); |
| 12184 | if (kind2 != kind) |
| 12185 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12186 | |
| 12187 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12188 | onError: |
| 12189 | Py_DECREF(sep_obj); |
| 12190 | Py_DECREF(str_obj); |
| 12191 | if (kind1 != kind && buf1) |
| 12192 | PyMem_Free(buf1); |
| 12193 | if (kind2 != kind && buf2) |
| 12194 | PyMem_Free(buf2); |
| 12195 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12196 | } |
| 12197 | |
| 12198 | |
| 12199 | PyObject * |
| 12200 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12201 | { |
| 12202 | PyObject* str_obj; |
| 12203 | PyObject* sep_obj; |
| 12204 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12205 | int kind1, kind2, kind; |
| 12206 | void *buf1 = NULL, *buf2 = NULL; |
| 12207 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12208 | |
| 12209 | str_obj = PyUnicode_FromObject(str_in); |
| 12210 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12211 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12212 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12213 | if (!sep_obj) { |
| 12214 | Py_DECREF(str_obj); |
| 12215 | return NULL; |
| 12216 | } |
| 12217 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12218 | kind1 = PyUnicode_KIND(str_in); |
| 12219 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12220 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12221 | buf1 = PyUnicode_DATA(str_in); |
| 12222 | if (kind1 != kind) |
| 12223 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12224 | if (!buf1) |
| 12225 | goto onError; |
| 12226 | buf2 = PyUnicode_DATA(sep_obj); |
| 12227 | if (kind2 != kind) |
| 12228 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12229 | if (!buf2) |
| 12230 | goto onError; |
| 12231 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12232 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12233 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12234 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12235 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12236 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12237 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12238 | else |
| 12239 | 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] | 12240 | break; |
| 12241 | case PyUnicode_2BYTE_KIND: |
| 12242 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12243 | break; |
| 12244 | case PyUnicode_4BYTE_KIND: |
| 12245 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12246 | break; |
| 12247 | default: |
| 12248 | assert(0); |
| 12249 | out = 0; |
| 12250 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12251 | |
| 12252 | Py_DECREF(sep_obj); |
| 12253 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12254 | if (kind1 != kind) |
| 12255 | PyMem_Free(buf1); |
| 12256 | if (kind2 != kind) |
| 12257 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12258 | |
| 12259 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12260 | onError: |
| 12261 | Py_DECREF(sep_obj); |
| 12262 | Py_DECREF(str_obj); |
| 12263 | if (kind1 != kind && buf1) |
| 12264 | PyMem_Free(buf1); |
| 12265 | if (kind2 != kind && buf2) |
| 12266 | PyMem_Free(buf2); |
| 12267 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12268 | } |
| 12269 | |
| 12270 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12271 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12272 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12273 | 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] | 12274 | 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] | 12275 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12276 | |
| 12277 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12278 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12279 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12280 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12281 | } |
| 12282 | |
| 12283 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12284 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12285 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12286 | 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] | 12287 | 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] | 12288 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12289 | |
| 12290 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12291 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12292 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12293 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12294 | } |
| 12295 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12296 | PyObject * |
| 12297 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12298 | { |
| 12299 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12300 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12301 | s = PyUnicode_FromObject(s); |
| 12302 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12303 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12304 | if (sep != NULL) { |
| 12305 | sep = PyUnicode_FromObject(sep); |
| 12306 | if (sep == NULL) { |
| 12307 | Py_DECREF(s); |
| 12308 | return NULL; |
| 12309 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12310 | } |
| 12311 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12312 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12313 | |
| 12314 | Py_DECREF(s); |
| 12315 | Py_XDECREF(sep); |
| 12316 | return result; |
| 12317 | } |
| 12318 | |
| 12319 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12320 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12321 | \n\ |
| 12322 | Return a list of the words in S, using sep as the\n\ |
| 12323 | delimiter string, starting at the end of the string and\n\ |
| 12324 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12325 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12326 | is a separator."); |
| 12327 | |
| 12328 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12329 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12330 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12331 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12332 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12333 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12334 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12335 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12336 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12337 | return NULL; |
| 12338 | |
| 12339 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12340 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12341 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12342 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12343 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12344 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12345 | } |
| 12346 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12347 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12348 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12349 | \n\ |
| 12350 | 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] | 12351 | 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] | 12352 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12353 | |
| 12354 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12355 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12356 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12357 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12358 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12359 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12360 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12361 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12362 | return NULL; |
| 12363 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12364 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12365 | } |
| 12366 | |
| 12367 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12368 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12369 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12370 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12371 | } |
| 12372 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12373 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12374 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12375 | \n\ |
| 12376 | 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] | 12377 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12378 | |
| 12379 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12380 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12381 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12382 | if (PyUnicode_READY(self) == -1) |
| 12383 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12384 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12385 | } |
| 12386 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12387 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12388 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12389 | \n\ |
| 12390 | Return a translation table usable for str.translate().\n\ |
| 12391 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12392 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12393 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12394 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12395 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12396 | character at the same position in y. If there is a third argument, it\n\ |
| 12397 | must be a string, whose characters will be mapped to None in the result."); |
| 12398 | |
| 12399 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12400 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12401 | { |
| 12402 | PyObject *x, *y = NULL, *z = NULL; |
| 12403 | PyObject *new = NULL, *key, *value; |
| 12404 | Py_ssize_t i = 0; |
| 12405 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12406 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12407 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12408 | return NULL; |
| 12409 | new = PyDict_New(); |
| 12410 | if (!new) |
| 12411 | return NULL; |
| 12412 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12413 | int x_kind, y_kind, z_kind; |
| 12414 | void *x_data, *y_data, *z_data; |
| 12415 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12416 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12417 | if (!PyUnicode_Check(x)) { |
| 12418 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12419 | "be a string if there is a second argument"); |
| 12420 | goto err; |
| 12421 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12422 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12423 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12424 | "arguments must have equal length"); |
| 12425 | goto err; |
| 12426 | } |
| 12427 | /* 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] | 12428 | x_kind = PyUnicode_KIND(x); |
| 12429 | y_kind = PyUnicode_KIND(y); |
| 12430 | x_data = PyUnicode_DATA(x); |
| 12431 | y_data = PyUnicode_DATA(y); |
| 12432 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12433 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12434 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12435 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12436 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12437 | if (!value) { |
| 12438 | Py_DECREF(key); |
| 12439 | goto err; |
| 12440 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12441 | res = PyDict_SetItem(new, key, value); |
| 12442 | Py_DECREF(key); |
| 12443 | Py_DECREF(value); |
| 12444 | if (res < 0) |
| 12445 | goto err; |
| 12446 | } |
| 12447 | /* create entries for deleting chars in z */ |
| 12448 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12449 | z_kind = PyUnicode_KIND(z); |
| 12450 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12451 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12452 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12453 | if (!key) |
| 12454 | goto err; |
| 12455 | res = PyDict_SetItem(new, key, Py_None); |
| 12456 | Py_DECREF(key); |
| 12457 | if (res < 0) |
| 12458 | goto err; |
| 12459 | } |
| 12460 | } |
| 12461 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12462 | int kind; |
| 12463 | void *data; |
| 12464 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12465 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12466 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12467 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12468 | "to maketrans it must be a dict"); |
| 12469 | goto err; |
| 12470 | } |
| 12471 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12472 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12473 | if (PyUnicode_Check(key)) { |
| 12474 | /* convert string keys to integer keys */ |
| 12475 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12476 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12477 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12478 | "table must be of length 1"); |
| 12479 | goto err; |
| 12480 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12481 | kind = PyUnicode_KIND(key); |
| 12482 | data = PyUnicode_DATA(key); |
| 12483 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12484 | if (!newkey) |
| 12485 | goto err; |
| 12486 | res = PyDict_SetItem(new, newkey, value); |
| 12487 | Py_DECREF(newkey); |
| 12488 | if (res < 0) |
| 12489 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12490 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12491 | /* just keep integer keys */ |
| 12492 | if (PyDict_SetItem(new, key, value) < 0) |
| 12493 | goto err; |
| 12494 | } else { |
| 12495 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12496 | "be strings or integers"); |
| 12497 | goto err; |
| 12498 | } |
| 12499 | } |
| 12500 | } |
| 12501 | return new; |
| 12502 | err: |
| 12503 | Py_DECREF(new); |
| 12504 | return NULL; |
| 12505 | } |
| 12506 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12507 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12508 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12509 | \n\ |
| 12510 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12511 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12512 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12513 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12514 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12515 | |
| 12516 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12517 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12518 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12519 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12520 | } |
| 12521 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12522 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12523 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12524 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12525 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12526 | |
| 12527 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12528 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12529 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12530 | if (PyUnicode_READY(self) == -1) |
| 12531 | return NULL; |
| 12532 | if (PyUnicode_IS_ASCII(self)) |
| 12533 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12534 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12535 | } |
| 12536 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12537 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12538 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12539 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12540 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12541 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12542 | |
| 12543 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12544 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12545 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12546 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12547 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12548 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12549 | int kind; |
| 12550 | void *data; |
| 12551 | Py_UCS4 chr; |
| 12552 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12553 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12554 | return NULL; |
| 12555 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12556 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12557 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12558 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12559 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12560 | return unicode_result_unchanged(self); |
| 12561 | |
| 12562 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12563 | |
| 12564 | u = pad(self, fill, 0, '0'); |
| 12565 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12566 | if (u == NULL) |
| 12567 | return NULL; |
| 12568 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12569 | kind = PyUnicode_KIND(u); |
| 12570 | data = PyUnicode_DATA(u); |
| 12571 | chr = PyUnicode_READ(kind, data, fill); |
| 12572 | |
| 12573 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12574 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12575 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12576 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12577 | } |
| 12578 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12579 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12580 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12581 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12582 | |
| 12583 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12584 | static PyObject * |
| 12585 | unicode__decimal2ascii(PyObject *self) |
| 12586 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12587 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12588 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12589 | #endif |
| 12590 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12591 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12592 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12593 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12594 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12595 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12596 | With optional end, stop comparing S at that position.\n\ |
| 12597 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12598 | |
| 12599 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12600 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12601 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12602 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12603 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12604 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12605 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12606 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12607 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12608 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12609 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12610 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12611 | if (PyTuple_Check(subobj)) { |
| 12612 | Py_ssize_t i; |
| 12613 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12614 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12615 | if (substring == NULL) |
| 12616 | return NULL; |
| 12617 | result = tailmatch(self, substring, start, end, -1); |
| 12618 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12619 | if (result == -1) |
| 12620 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12621 | if (result) { |
| 12622 | Py_RETURN_TRUE; |
| 12623 | } |
| 12624 | } |
| 12625 | /* nothing matched */ |
| 12626 | Py_RETURN_FALSE; |
| 12627 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12628 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12629 | if (substring == NULL) { |
| 12630 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12631 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12632 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12633 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12634 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12635 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12636 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12637 | if (result == -1) |
| 12638 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12639 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12640 | } |
| 12641 | |
| 12642 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12643 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12644 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12645 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12646 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12647 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12648 | With optional end, stop comparing S at that position.\n\ |
| 12649 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12650 | |
| 12651 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12652 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12653 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12654 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12655 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12656 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12657 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12658 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12659 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12660 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12661 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12662 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12663 | if (PyTuple_Check(subobj)) { |
| 12664 | Py_ssize_t i; |
| 12665 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12666 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12667 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12668 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12669 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12670 | result = tailmatch(self, substring, start, end, +1); |
| 12671 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12672 | if (result == -1) |
| 12673 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12674 | if (result) { |
| 12675 | Py_RETURN_TRUE; |
| 12676 | } |
| 12677 | } |
| 12678 | Py_RETURN_FALSE; |
| 12679 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12680 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12681 | if (substring == NULL) { |
| 12682 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12683 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12684 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12685 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12686 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12687 | result = tailmatch(self, substring, start, end, +1); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame] | 12688 | if (result == -1) |
| 12689 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12690 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12691 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12692 | } |
| 12693 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12694 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12695 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12696 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12697 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12698 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 12699 | writer->data = PyUnicode_DATA(writer->buffer); |
| 12700 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 12701 | } |
| 12702 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12703 | void |
| 12704 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12705 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12706 | memset(writer, 0, sizeof(*writer)); |
| 12707 | #ifdef Py_DEBUG |
| 12708 | writer->kind = 5; /* invalid kind */ |
| 12709 | #endif |
| 12710 | writer->min_length = Py_MAX(min_length, 100); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12711 | writer->overallocate = (min_length > 0); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12712 | } |
| 12713 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12714 | int |
| 12715 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 12716 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12717 | { |
| 12718 | Py_ssize_t newlen; |
| 12719 | PyObject *newbuffer; |
| 12720 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12721 | assert(length > 0); |
| 12722 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12723 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 12724 | PyErr_NoMemory(); |
| 12725 | return -1; |
| 12726 | } |
| 12727 | newlen = writer->pos + length; |
| 12728 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12729 | if (writer->buffer == NULL) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12730 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12731 | /* overallocate 25% to limit the number of resize */ |
| 12732 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12733 | newlen += newlen / 4; |
| 12734 | if (newlen < writer->min_length) |
| 12735 | newlen = writer->min_length; |
| 12736 | } |
| 12737 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 12738 | if (writer->buffer == NULL) |
| 12739 | return -1; |
| 12740 | _PyUnicodeWriter_Update(writer); |
| 12741 | return 0; |
| 12742 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12743 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12744 | if (newlen > writer->size) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12745 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12746 | /* overallocate 25% to limit the number of resize */ |
| 12747 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12748 | newlen += newlen / 4; |
| 12749 | if (newlen < writer->min_length) |
| 12750 | newlen = writer->min_length; |
| 12751 | } |
| 12752 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12753 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12754 | /* resize + widen */ |
| 12755 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 12756 | if (newbuffer == NULL) |
| 12757 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12758 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12759 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12760 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12761 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12762 | } |
| 12763 | else { |
| 12764 | newbuffer = resize_compact(writer->buffer, newlen); |
| 12765 | if (newbuffer == NULL) |
| 12766 | return -1; |
| 12767 | } |
| 12768 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12769 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12770 | } |
| 12771 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12772 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12773 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 12774 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12775 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12776 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12777 | writer->buffer, 0, writer->pos); |
| 12778 | Py_DECREF(writer->buffer); |
| 12779 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12780 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12781 | } |
| 12782 | return 0; |
| 12783 | } |
| 12784 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12785 | int |
| 12786 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 12787 | { |
| 12788 | Py_UCS4 maxchar; |
| 12789 | Py_ssize_t len; |
| 12790 | |
| 12791 | if (PyUnicode_READY(str) == -1) |
| 12792 | return -1; |
| 12793 | len = PyUnicode_GET_LENGTH(str); |
| 12794 | if (len == 0) |
| 12795 | return 0; |
| 12796 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 12797 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12798 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12799 | Py_INCREF(str); |
| 12800 | writer->buffer = str; |
| 12801 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12802 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12803 | writer->size = 0; |
| 12804 | writer->pos += len; |
| 12805 | return 0; |
| 12806 | } |
| 12807 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 12808 | return -1; |
| 12809 | } |
| 12810 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 12811 | str, 0, len); |
| 12812 | writer->pos += len; |
| 12813 | return 0; |
| 12814 | } |
| 12815 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 12816 | int |
| 12817 | _PyUnicodeWriter_WriteCstr(_PyUnicodeWriter *writer, const char *str, Py_ssize_t len) |
| 12818 | { |
| 12819 | Py_UCS4 maxchar; |
| 12820 | |
| 12821 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 12822 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 12823 | return -1; |
| 12824 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 12825 | writer->pos += len; |
| 12826 | return 0; |
| 12827 | } |
| 12828 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12829 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12830 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12831 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12832 | if (writer->pos == 0) { |
| 12833 | Py_XDECREF(writer->buffer); |
| 12834 | Py_INCREF(unicode_empty); |
| 12835 | return unicode_empty; |
| 12836 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12837 | if (writer->readonly) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12838 | assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); |
| 12839 | return writer->buffer; |
| 12840 | } |
| 12841 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 12842 | PyObject *newbuffer; |
| 12843 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 12844 | if (newbuffer == NULL) { |
| 12845 | Py_DECREF(writer->buffer); |
| 12846 | return NULL; |
| 12847 | } |
| 12848 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12849 | } |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 12850 | assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12851 | return writer->buffer; |
| 12852 | } |
| 12853 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12854 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12855 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12856 | { |
| 12857 | Py_CLEAR(writer->buffer); |
| 12858 | } |
| 12859 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12860 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12861 | |
| 12862 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12863 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12864 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12865 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12866 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12867 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12868 | PyDoc_STRVAR(format_map__doc__, |
| 12869 | "S.format_map(mapping) -> str\n\ |
| 12870 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12871 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12872 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12873 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12874 | static PyObject * |
| 12875 | unicode__format__(PyObject* self, PyObject* args) |
| 12876 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12877 | PyObject *format_spec; |
| 12878 | _PyUnicodeWriter writer; |
| 12879 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12880 | |
| 12881 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12882 | return NULL; |
| 12883 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12884 | if (PyUnicode_READY(self) == -1) |
| 12885 | return NULL; |
| 12886 | _PyUnicodeWriter_Init(&writer, 0); |
| 12887 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 12888 | self, format_spec, 0, |
| 12889 | PyUnicode_GET_LENGTH(format_spec)); |
| 12890 | if (ret == -1) { |
| 12891 | _PyUnicodeWriter_Dealloc(&writer); |
| 12892 | return NULL; |
| 12893 | } |
| 12894 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12895 | } |
| 12896 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12897 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12898 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12899 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12900 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12901 | |
| 12902 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12903 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12904 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12905 | Py_ssize_t size; |
| 12906 | |
| 12907 | /* If it's a compact object, account for base structure + |
| 12908 | character data. */ |
| 12909 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12910 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12911 | else if (PyUnicode_IS_COMPACT(v)) |
| 12912 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12913 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12914 | else { |
| 12915 | /* If it is a two-block object, account for base object, and |
| 12916 | for character block if present. */ |
| 12917 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12918 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12919 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12920 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12921 | } |
| 12922 | /* 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] | 12923 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12924 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12925 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12926 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12927 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12928 | |
| 12929 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12930 | } |
| 12931 | |
| 12932 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12933 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12934 | |
| 12935 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12936 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12937 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 12938 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12939 | if (!copy) |
| 12940 | return NULL; |
| 12941 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12942 | } |
| 12943 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12944 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12945 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12946 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12947 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 12948 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12949 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12950 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 12951 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12952 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12953 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12954 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12955 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12956 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12957 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12958 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12959 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12960 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12961 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12962 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12963 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12964 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12965 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12966 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12967 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12968 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12969 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12970 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12971 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12972 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12973 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12974 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12975 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12976 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12977 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12978 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12979 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12980 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12981 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12982 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12983 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12984 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12985 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12986 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12987 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12988 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12989 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12990 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12991 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12992 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12993 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12994 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12995 | #endif |
| 12996 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12997 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12998 | {NULL, NULL} |
| 12999 | }; |
| 13000 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13001 | static PyObject * |
| 13002 | unicode_mod(PyObject *v, PyObject *w) |
| 13003 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 13004 | if (!PyUnicode_Check(v)) |
| 13005 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13006 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13007 | } |
| 13008 | |
| 13009 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13010 | 0, /*nb_add*/ |
| 13011 | 0, /*nb_subtract*/ |
| 13012 | 0, /*nb_multiply*/ |
| 13013 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 13014 | }; |
| 13015 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13016 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13017 | (lenfunc) unicode_length, /* sq_length */ |
| 13018 | PyUnicode_Concat, /* sq_concat */ |
| 13019 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13020 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13021 | 0, /* sq_slice */ |
| 13022 | 0, /* sq_ass_item */ |
| 13023 | 0, /* sq_ass_slice */ |
| 13024 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13025 | }; |
| 13026 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13027 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13028 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13029 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13030 | if (PyUnicode_READY(self) == -1) |
| 13031 | return NULL; |
| 13032 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13033 | if (PyIndex_Check(item)) { |
| 13034 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13035 | if (i == -1 && PyErr_Occurred()) |
| 13036 | return NULL; |
| 13037 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13038 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13039 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13040 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13041 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13042 | PyObject *result; |
| 13043 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13044 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13045 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13046 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13047 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13048 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13049 | return NULL; |
| 13050 | } |
| 13051 | |
| 13052 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13053 | Py_INCREF(unicode_empty); |
| 13054 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13055 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13056 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13057 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13058 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13059 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13060 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13061 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13062 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13063 | src_kind = PyUnicode_KIND(self); |
| 13064 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13065 | if (!PyUnicode_IS_ASCII(self)) { |
| 13066 | kind_limit = kind_maxchar_limit(src_kind); |
| 13067 | max_char = 0; |
| 13068 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13069 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13070 | if (ch > max_char) { |
| 13071 | max_char = ch; |
| 13072 | if (max_char >= kind_limit) |
| 13073 | break; |
| 13074 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13075 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13076 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13077 | else |
| 13078 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13079 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13080 | if (result == NULL) |
| 13081 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13082 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13083 | dest_data = PyUnicode_DATA(result); |
| 13084 | |
| 13085 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13086 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13087 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13088 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13089 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13090 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13091 | } else { |
| 13092 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13093 | return NULL; |
| 13094 | } |
| 13095 | } |
| 13096 | |
| 13097 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13098 | (lenfunc)unicode_length, /* mp_length */ |
| 13099 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13100 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13101 | }; |
| 13102 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13103 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13104 | /* Helpers for PyUnicode_Format() */ |
| 13105 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13106 | struct unicode_formatter_t { |
| 13107 | PyObject *args; |
| 13108 | int args_owned; |
| 13109 | Py_ssize_t arglen, argidx; |
| 13110 | PyObject *dict; |
| 13111 | |
| 13112 | enum PyUnicode_Kind fmtkind; |
| 13113 | Py_ssize_t fmtcnt, fmtpos; |
| 13114 | void *fmtdata; |
| 13115 | PyObject *fmtstr; |
| 13116 | |
| 13117 | _PyUnicodeWriter writer; |
| 13118 | }; |
| 13119 | |
| 13120 | struct unicode_format_arg_t { |
| 13121 | Py_UCS4 ch; |
| 13122 | int flags; |
| 13123 | Py_ssize_t width; |
| 13124 | int prec; |
| 13125 | int sign; |
| 13126 | }; |
| 13127 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13128 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13129 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13130 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13131 | Py_ssize_t argidx = ctx->argidx; |
| 13132 | |
| 13133 | if (argidx < ctx->arglen) { |
| 13134 | ctx->argidx++; |
| 13135 | if (ctx->arglen < 0) |
| 13136 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13137 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13138 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13139 | } |
| 13140 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13141 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13142 | return NULL; |
| 13143 | } |
| 13144 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13145 | /* 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] | 13146 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13147 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13148 | otherwise. |
| 13149 | |
| 13150 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13151 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13152 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13153 | PyObject **p_output, |
| 13154 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13155 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13156 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13157 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13158 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13159 | int prec; |
| 13160 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13161 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13162 | x = PyFloat_AsDouble(v); |
| 13163 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13164 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13165 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13166 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13167 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13168 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13169 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13170 | if (arg->flags & F_ALT) |
| 13171 | dtoa_flags = Py_DTSF_ALT; |
| 13172 | else |
| 13173 | dtoa_flags = 0; |
| 13174 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13175 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13176 | return -1; |
| 13177 | len = strlen(p); |
| 13178 | if (writer) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13179 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) { |
| 13180 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13181 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13182 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13183 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13184 | writer->pos += len; |
| 13185 | } |
| 13186 | else |
| 13187 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13188 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13189 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13190 | } |
| 13191 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13192 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13193 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13194 | * Python's regular ints. |
| 13195 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13196 | * The output string is of the form |
| 13197 | * "-"? ("0x" | "0X")? digit+ |
| 13198 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13199 | * set in flags. The case of hex digits will be correct, |
| 13200 | * There will be at least prec digits, zero-filled on the left if |
| 13201 | * necessary to get that many. |
| 13202 | * val object to be converted |
| 13203 | * flags bitmask of format flags; only F_ALT is looked at |
| 13204 | * prec minimum number of digits; 0-fill on left if needed |
| 13205 | * type a character in [duoxX]; u acts the same as d |
| 13206 | * |
| 13207 | * CAUTION: o, x and X conversions on regular ints can never |
| 13208 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13209 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13210 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13211 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13212 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13213 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13214 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13215 | Py_ssize_t i; |
| 13216 | int sign; /* 1 if '-', else 0 */ |
| 13217 | int len; /* number of characters */ |
| 13218 | Py_ssize_t llen; |
| 13219 | int numdigits; /* len == numnondigits + numdigits */ |
| 13220 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13221 | int prec = arg->prec; |
| 13222 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13223 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13224 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13225 | if (prec > INT_MAX-3) { |
| 13226 | PyErr_SetString(PyExc_OverflowError, |
| 13227 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13228 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13229 | } |
| 13230 | |
| 13231 | assert(PyLong_Check(val)); |
| 13232 | |
| 13233 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13234 | default: |
| 13235 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13236 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13237 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13238 | case 'u': |
| 13239 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13240 | if (PyBool_Check(val)) |
| 13241 | result = PyNumber_ToBase(val, 10); |
| 13242 | else |
| 13243 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13244 | break; |
| 13245 | case 'o': |
| 13246 | numnondigits = 2; |
| 13247 | result = PyNumber_ToBase(val, 8); |
| 13248 | break; |
| 13249 | case 'x': |
| 13250 | case 'X': |
| 13251 | numnondigits = 2; |
| 13252 | result = PyNumber_ToBase(val, 16); |
| 13253 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13254 | } |
| 13255 | if (!result) |
| 13256 | return NULL; |
| 13257 | |
| 13258 | assert(unicode_modifiable(result)); |
| 13259 | assert(PyUnicode_IS_READY(result)); |
| 13260 | assert(PyUnicode_IS_ASCII(result)); |
| 13261 | |
| 13262 | /* To modify the string in-place, there can only be one reference. */ |
| 13263 | if (Py_REFCNT(result) != 1) { |
| 13264 | PyErr_BadInternalCall(); |
| 13265 | return NULL; |
| 13266 | } |
| 13267 | buf = PyUnicode_DATA(result); |
| 13268 | llen = PyUnicode_GET_LENGTH(result); |
| 13269 | if (llen > INT_MAX) { |
| 13270 | PyErr_SetString(PyExc_ValueError, |
| 13271 | "string too large in _PyBytes_FormatLong"); |
| 13272 | return NULL; |
| 13273 | } |
| 13274 | len = (int)llen; |
| 13275 | sign = buf[0] == '-'; |
| 13276 | numnondigits += sign; |
| 13277 | numdigits = len - numnondigits; |
| 13278 | assert(numdigits > 0); |
| 13279 | |
| 13280 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13281 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13282 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13283 | assert(buf[sign] == '0'); |
| 13284 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13285 | buf[sign+1] == 'o'); |
| 13286 | numnondigits -= 2; |
| 13287 | buf += 2; |
| 13288 | len -= 2; |
| 13289 | if (sign) |
| 13290 | buf[0] = '-'; |
| 13291 | assert(len == numnondigits + numdigits); |
| 13292 | assert(numdigits > 0); |
| 13293 | } |
| 13294 | |
| 13295 | /* Fill with leading zeroes to meet minimum width. */ |
| 13296 | if (prec > numdigits) { |
| 13297 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13298 | numnondigits + prec); |
| 13299 | char *b1; |
| 13300 | if (!r1) { |
| 13301 | Py_DECREF(result); |
| 13302 | return NULL; |
| 13303 | } |
| 13304 | b1 = PyBytes_AS_STRING(r1); |
| 13305 | for (i = 0; i < numnondigits; ++i) |
| 13306 | *b1++ = *buf++; |
| 13307 | for (i = 0; i < prec - numdigits; i++) |
| 13308 | *b1++ = '0'; |
| 13309 | for (i = 0; i < numdigits; i++) |
| 13310 | *b1++ = *buf++; |
| 13311 | *b1 = '\0'; |
| 13312 | Py_DECREF(result); |
| 13313 | result = r1; |
| 13314 | buf = PyBytes_AS_STRING(result); |
| 13315 | len = numnondigits + prec; |
| 13316 | } |
| 13317 | |
| 13318 | /* Fix up case for hex conversions. */ |
| 13319 | if (type == 'X') { |
| 13320 | /* Need to convert all lower case letters to upper case. |
| 13321 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13322 | for (i = 0; i < len; i++) |
| 13323 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13324 | buf[i] -= 'a'-'A'; |
| 13325 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13326 | if (!PyUnicode_Check(result) |
| 13327 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13328 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13329 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13330 | Py_DECREF(result); |
| 13331 | result = unicode; |
| 13332 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13333 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 13334 | if (PyUnicode_Resize(&result, len) < 0) |
| 13335 | Py_CLEAR(result); |
| 13336 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13337 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13338 | } |
| 13339 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13340 | /* Format an integer. |
| 13341 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13342 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13343 | * -1 and raise an exception on error */ |
| 13344 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13345 | mainformatlong(PyObject *v, |
| 13346 | struct unicode_format_arg_t *arg, |
| 13347 | PyObject **p_output, |
| 13348 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13349 | { |
| 13350 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13351 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13352 | |
| 13353 | if (!PyNumber_Check(v)) |
| 13354 | goto wrongtype; |
| 13355 | |
| 13356 | if (!PyLong_Check(v)) { |
| 13357 | iobj = PyNumber_Long(v); |
| 13358 | if (iobj == NULL) { |
| 13359 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13360 | goto wrongtype; |
| 13361 | return -1; |
| 13362 | } |
| 13363 | assert(PyLong_Check(iobj)); |
| 13364 | } |
| 13365 | else { |
| 13366 | iobj = v; |
| 13367 | Py_INCREF(iobj); |
| 13368 | } |
| 13369 | |
| 13370 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13371 | && arg->width == -1 && arg->prec == -1 |
| 13372 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 13373 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13374 | { |
| 13375 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13376 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13377 | int base; |
| 13378 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13379 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13380 | { |
| 13381 | default: |
| 13382 | assert(0 && "'type' not in [diuoxX]"); |
| 13383 | case 'd': |
| 13384 | case 'i': |
| 13385 | case 'u': |
| 13386 | base = 10; |
| 13387 | break; |
| 13388 | case 'o': |
| 13389 | base = 8; |
| 13390 | break; |
| 13391 | case 'x': |
| 13392 | case 'X': |
| 13393 | base = 16; |
| 13394 | break; |
| 13395 | } |
| 13396 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13397 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 13398 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13399 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13400 | } |
| 13401 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13402 | return 1; |
| 13403 | } |
| 13404 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13405 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13406 | Py_DECREF(iobj); |
| 13407 | if (res == NULL) |
| 13408 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13409 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13410 | return 0; |
| 13411 | |
| 13412 | wrongtype: |
| 13413 | PyErr_Format(PyExc_TypeError, |
| 13414 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13415 | "not %.200s", |
| 13416 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13417 | return -1; |
| 13418 | } |
| 13419 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13420 | static Py_UCS4 |
| 13421 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13422 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13423 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13424 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13425 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13426 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13427 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13428 | goto onError; |
| 13429 | } |
| 13430 | else { |
| 13431 | /* Integer input truncated to a character */ |
| 13432 | long x; |
| 13433 | x = PyLong_AsLong(v); |
| 13434 | if (x == -1 && PyErr_Occurred()) |
| 13435 | goto onError; |
| 13436 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13437 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13438 | PyErr_SetString(PyExc_OverflowError, |
| 13439 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13440 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13441 | } |
| 13442 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13443 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13444 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13445 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13446 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13447 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13448 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13449 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13450 | } |
| 13451 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13452 | /* Parse options of an argument: flags, width, precision. |
| 13453 | Handle also "%(name)" syntax. |
| 13454 | |
| 13455 | Return 0 if the argument has been formatted into arg->str. |
| 13456 | Return 1 if the argument has been written into ctx->writer, |
| 13457 | Raise an exception and return -1 on error. */ |
| 13458 | static int |
| 13459 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 13460 | struct unicode_format_arg_t *arg) |
| 13461 | { |
| 13462 | #define FORMAT_READ(ctx) \ |
| 13463 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 13464 | |
| 13465 | PyObject *v; |
| 13466 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13467 | if (arg->ch == '(') { |
| 13468 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 13469 | Py_ssize_t keystart; |
| 13470 | Py_ssize_t keylen; |
| 13471 | PyObject *key; |
| 13472 | int pcount = 1; |
| 13473 | |
| 13474 | if (ctx->dict == NULL) { |
| 13475 | PyErr_SetString(PyExc_TypeError, |
| 13476 | "format requires a mapping"); |
| 13477 | return -1; |
| 13478 | } |
| 13479 | ++ctx->fmtpos; |
| 13480 | --ctx->fmtcnt; |
| 13481 | keystart = ctx->fmtpos; |
| 13482 | /* Skip over balanced parentheses */ |
| 13483 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 13484 | arg->ch = FORMAT_READ(ctx); |
| 13485 | if (arg->ch == ')') |
| 13486 | --pcount; |
| 13487 | else if (arg->ch == '(') |
| 13488 | ++pcount; |
| 13489 | ctx->fmtpos++; |
| 13490 | } |
| 13491 | keylen = ctx->fmtpos - keystart - 1; |
| 13492 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 13493 | PyErr_SetString(PyExc_ValueError, |
| 13494 | "incomplete format key"); |
| 13495 | return -1; |
| 13496 | } |
| 13497 | key = PyUnicode_Substring(ctx->fmtstr, |
| 13498 | keystart, keystart + keylen); |
| 13499 | if (key == NULL) |
| 13500 | return -1; |
| 13501 | if (ctx->args_owned) { |
| 13502 | Py_DECREF(ctx->args); |
| 13503 | ctx->args_owned = 0; |
| 13504 | } |
| 13505 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 13506 | Py_DECREF(key); |
| 13507 | if (ctx->args == NULL) |
| 13508 | return -1; |
| 13509 | ctx->args_owned = 1; |
| 13510 | ctx->arglen = -1; |
| 13511 | ctx->argidx = -2; |
| 13512 | } |
| 13513 | |
| 13514 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13515 | while (--ctx->fmtcnt >= 0) { |
| 13516 | arg->ch = FORMAT_READ(ctx); |
| 13517 | ctx->fmtpos++; |
| 13518 | switch (arg->ch) { |
| 13519 | case '-': arg->flags |= F_LJUST; continue; |
| 13520 | case '+': arg->flags |= F_SIGN; continue; |
| 13521 | case ' ': arg->flags |= F_BLANK; continue; |
| 13522 | case '#': arg->flags |= F_ALT; continue; |
| 13523 | case '0': arg->flags |= F_ZERO; continue; |
| 13524 | } |
| 13525 | break; |
| 13526 | } |
| 13527 | |
| 13528 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13529 | if (arg->ch == '*') { |
| 13530 | v = unicode_format_getnextarg(ctx); |
| 13531 | if (v == NULL) |
| 13532 | return -1; |
| 13533 | if (!PyLong_Check(v)) { |
| 13534 | PyErr_SetString(PyExc_TypeError, |
| 13535 | "* wants int"); |
| 13536 | return -1; |
| 13537 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13538 | arg->width = PyLong_AsSsize_t(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13539 | if (arg->width == -1 && PyErr_Occurred()) |
| 13540 | return -1; |
| 13541 | if (arg->width < 0) { |
| 13542 | arg->flags |= F_LJUST; |
| 13543 | arg->width = -arg->width; |
| 13544 | } |
| 13545 | if (--ctx->fmtcnt >= 0) { |
| 13546 | arg->ch = FORMAT_READ(ctx); |
| 13547 | ctx->fmtpos++; |
| 13548 | } |
| 13549 | } |
| 13550 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13551 | arg->width = arg->ch - '0'; |
| 13552 | while (--ctx->fmtcnt >= 0) { |
| 13553 | arg->ch = FORMAT_READ(ctx); |
| 13554 | ctx->fmtpos++; |
| 13555 | if (arg->ch < '0' || arg->ch > '9') |
| 13556 | break; |
| 13557 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 13558 | mixing signed and unsigned comparison. Since arg->ch is between |
| 13559 | '0' and '9', casting to int is safe. */ |
| 13560 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 13561 | PyErr_SetString(PyExc_ValueError, |
| 13562 | "width too big"); |
| 13563 | return -1; |
| 13564 | } |
| 13565 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 13566 | } |
| 13567 | } |
| 13568 | |
| 13569 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13570 | if (arg->ch == '.') { |
| 13571 | arg->prec = 0; |
| 13572 | if (--ctx->fmtcnt >= 0) { |
| 13573 | arg->ch = FORMAT_READ(ctx); |
| 13574 | ctx->fmtpos++; |
| 13575 | } |
| 13576 | if (arg->ch == '*') { |
| 13577 | v = unicode_format_getnextarg(ctx); |
| 13578 | if (v == NULL) |
| 13579 | return -1; |
| 13580 | if (!PyLong_Check(v)) { |
| 13581 | PyErr_SetString(PyExc_TypeError, |
| 13582 | "* wants int"); |
| 13583 | return -1; |
| 13584 | } |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 13585 | arg->prec = _PyLong_AsInt(v); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13586 | if (arg->prec == -1 && PyErr_Occurred()) |
| 13587 | return -1; |
| 13588 | if (arg->prec < 0) |
| 13589 | arg->prec = 0; |
| 13590 | if (--ctx->fmtcnt >= 0) { |
| 13591 | arg->ch = FORMAT_READ(ctx); |
| 13592 | ctx->fmtpos++; |
| 13593 | } |
| 13594 | } |
| 13595 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13596 | arg->prec = arg->ch - '0'; |
| 13597 | while (--ctx->fmtcnt >= 0) { |
| 13598 | arg->ch = FORMAT_READ(ctx); |
| 13599 | ctx->fmtpos++; |
| 13600 | if (arg->ch < '0' || arg->ch > '9') |
| 13601 | break; |
| 13602 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 13603 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 13604 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13605 | return -1; |
| 13606 | } |
| 13607 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 13608 | } |
| 13609 | } |
| 13610 | } |
| 13611 | |
| 13612 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 13613 | if (ctx->fmtcnt >= 0) { |
| 13614 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 13615 | if (--ctx->fmtcnt >= 0) { |
| 13616 | arg->ch = FORMAT_READ(ctx); |
| 13617 | ctx->fmtpos++; |
| 13618 | } |
| 13619 | } |
| 13620 | } |
| 13621 | if (ctx->fmtcnt < 0) { |
| 13622 | PyErr_SetString(PyExc_ValueError, |
| 13623 | "incomplete format"); |
| 13624 | return -1; |
| 13625 | } |
| 13626 | return 0; |
| 13627 | |
| 13628 | #undef FORMAT_READ |
| 13629 | } |
| 13630 | |
| 13631 | /* Format one argument. Supported conversion specifiers: |
| 13632 | |
| 13633 | - "s", "r", "a": any type |
| 13634 | - "i", "d", "u", "o", "x", "X": int |
| 13635 | - "e", "E", "f", "F", "g", "G": float |
| 13636 | - "c": int or str (1 character) |
| 13637 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13638 | When possible, the output is written directly into the Unicode writer |
| 13639 | (ctx->writer). A string is created when padding is required. |
| 13640 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13641 | Return 0 if the argument has been formatted into *p_str, |
| 13642 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13643 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13644 | static int |
| 13645 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 13646 | struct unicode_format_arg_t *arg, |
| 13647 | PyObject **p_str) |
| 13648 | { |
| 13649 | PyObject *v; |
| 13650 | _PyUnicodeWriter *writer = &ctx->writer; |
| 13651 | |
| 13652 | if (ctx->fmtcnt == 0) |
| 13653 | ctx->writer.overallocate = 0; |
| 13654 | |
| 13655 | if (arg->ch == '%') { |
| 13656 | if (_PyUnicodeWriter_Prepare(writer, 1, '%') == -1) |
| 13657 | return -1; |
| 13658 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '%'); |
| 13659 | writer->pos += 1; |
| 13660 | return 1; |
| 13661 | } |
| 13662 | |
| 13663 | v = unicode_format_getnextarg(ctx); |
| 13664 | if (v == NULL) |
| 13665 | return -1; |
| 13666 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13667 | |
| 13668 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13669 | case 's': |
| 13670 | case 'r': |
| 13671 | case 'a': |
| 13672 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 13673 | /* Fast path */ |
| 13674 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 13675 | return -1; |
| 13676 | return 1; |
| 13677 | } |
| 13678 | |
| 13679 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 13680 | *p_str = v; |
| 13681 | Py_INCREF(*p_str); |
| 13682 | } |
| 13683 | else { |
| 13684 | if (arg->ch == 's') |
| 13685 | *p_str = PyObject_Str(v); |
| 13686 | else if (arg->ch == 'r') |
| 13687 | *p_str = PyObject_Repr(v); |
| 13688 | else |
| 13689 | *p_str = PyObject_ASCII(v); |
| 13690 | } |
| 13691 | break; |
| 13692 | |
| 13693 | case 'i': |
| 13694 | case 'd': |
| 13695 | case 'u': |
| 13696 | case 'o': |
| 13697 | case 'x': |
| 13698 | case 'X': |
| 13699 | { |
| 13700 | int ret = mainformatlong(v, arg, p_str, writer); |
| 13701 | if (ret != 0) |
| 13702 | return ret; |
| 13703 | arg->sign = 1; |
| 13704 | break; |
| 13705 | } |
| 13706 | |
| 13707 | case 'e': |
| 13708 | case 'E': |
| 13709 | case 'f': |
| 13710 | case 'F': |
| 13711 | case 'g': |
| 13712 | case 'G': |
| 13713 | if (arg->width == -1 && arg->prec == -1 |
| 13714 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 13715 | { |
| 13716 | /* Fast path */ |
| 13717 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 13718 | return -1; |
| 13719 | return 1; |
| 13720 | } |
| 13721 | |
| 13722 | arg->sign = 1; |
| 13723 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 13724 | return -1; |
| 13725 | break; |
| 13726 | |
| 13727 | case 'c': |
| 13728 | { |
| 13729 | Py_UCS4 ch = formatchar(v); |
| 13730 | if (ch == (Py_UCS4) -1) |
| 13731 | return -1; |
| 13732 | if (arg->width == -1 && arg->prec == -1) { |
| 13733 | /* Fast path */ |
| 13734 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) == -1) |
| 13735 | return -1; |
| 13736 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13737 | writer->pos += 1; |
| 13738 | return 1; |
| 13739 | } |
| 13740 | *p_str = PyUnicode_FromOrdinal(ch); |
| 13741 | break; |
| 13742 | } |
| 13743 | |
| 13744 | default: |
| 13745 | PyErr_Format(PyExc_ValueError, |
| 13746 | "unsupported format character '%c' (0x%x) " |
| 13747 | "at index %zd", |
| 13748 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 13749 | (int)arg->ch, |
| 13750 | ctx->fmtpos - 1); |
| 13751 | return -1; |
| 13752 | } |
| 13753 | if (*p_str == NULL) |
| 13754 | return -1; |
| 13755 | assert (PyUnicode_Check(*p_str)); |
| 13756 | return 0; |
| 13757 | } |
| 13758 | |
| 13759 | static int |
| 13760 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 13761 | struct unicode_format_arg_t *arg, |
| 13762 | PyObject *str) |
| 13763 | { |
| 13764 | Py_ssize_t len; |
| 13765 | enum PyUnicode_Kind kind; |
| 13766 | void *pbuf; |
| 13767 | Py_ssize_t pindex; |
| 13768 | Py_UCS4 signchar; |
| 13769 | Py_ssize_t buflen; |
| 13770 | Py_UCS4 maxchar, bufmaxchar; |
| 13771 | Py_ssize_t sublen; |
| 13772 | _PyUnicodeWriter *writer = &ctx->writer; |
| 13773 | Py_UCS4 fill; |
| 13774 | |
| 13775 | fill = ' '; |
| 13776 | if (arg->sign && arg->flags & F_ZERO) |
| 13777 | fill = '0'; |
| 13778 | |
| 13779 | if (PyUnicode_READY(str) == -1) |
| 13780 | return -1; |
| 13781 | |
| 13782 | len = PyUnicode_GET_LENGTH(str); |
| 13783 | if ((arg->width == -1 || arg->width <= len) |
| 13784 | && (arg->prec == -1 || arg->prec >= len) |
| 13785 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 13786 | { |
| 13787 | /* Fast path */ |
| 13788 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 13789 | return -1; |
| 13790 | return 0; |
| 13791 | } |
| 13792 | |
| 13793 | /* Truncate the string for "s", "r" and "a" formats |
| 13794 | if the precision is set */ |
| 13795 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 13796 | if (arg->prec >= 0 && len > arg->prec) |
| 13797 | len = arg->prec; |
| 13798 | } |
| 13799 | |
| 13800 | /* Adjust sign and width */ |
| 13801 | kind = PyUnicode_KIND(str); |
| 13802 | pbuf = PyUnicode_DATA(str); |
| 13803 | pindex = 0; |
| 13804 | signchar = '\0'; |
| 13805 | if (arg->sign) { |
| 13806 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 13807 | if (ch == '-' || ch == '+') { |
| 13808 | signchar = ch; |
| 13809 | len--; |
| 13810 | pindex++; |
| 13811 | } |
| 13812 | else if (arg->flags & F_SIGN) |
| 13813 | signchar = '+'; |
| 13814 | else if (arg->flags & F_BLANK) |
| 13815 | signchar = ' '; |
| 13816 | else |
| 13817 | arg->sign = 0; |
| 13818 | } |
| 13819 | if (arg->width < len) |
| 13820 | arg->width = len; |
| 13821 | |
| 13822 | /* Prepare the writer */ |
| 13823 | bufmaxchar = 127; |
| 13824 | if (!(arg->flags & F_LJUST)) { |
| 13825 | if (arg->sign) { |
| 13826 | if ((arg->width-1) > len) |
| 13827 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); |
| 13828 | } |
| 13829 | else { |
| 13830 | if (arg->width > len) |
| 13831 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); |
| 13832 | } |
| 13833 | } |
| 13834 | maxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
| 13835 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, maxchar); |
| 13836 | buflen = arg->width; |
| 13837 | if (arg->sign && len == arg->width) |
| 13838 | buflen++; |
| 13839 | if (_PyUnicodeWriter_Prepare(writer, buflen, bufmaxchar) == -1) |
| 13840 | return -1; |
| 13841 | |
| 13842 | /* Write the sign if needed */ |
| 13843 | if (arg->sign) { |
| 13844 | if (fill != ' ') { |
| 13845 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 13846 | writer->pos += 1; |
| 13847 | } |
| 13848 | if (arg->width > len) |
| 13849 | arg->width--; |
| 13850 | } |
| 13851 | |
| 13852 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 13853 | if the alternate form is used. |
| 13854 | For example, write "0x" for the "%#x" format. */ |
| 13855 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 13856 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13857 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 13858 | if (fill != ' ') { |
| 13859 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 13860 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 13861 | writer->pos += 2; |
| 13862 | pindex += 2; |
| 13863 | } |
| 13864 | arg->width -= 2; |
| 13865 | if (arg->width < 0) |
| 13866 | arg->width = 0; |
| 13867 | len -= 2; |
| 13868 | } |
| 13869 | |
| 13870 | /* Pad left with the fill character if needed */ |
| 13871 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 13872 | sublen = arg->width - len; |
| 13873 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 13874 | writer->pos += sublen; |
| 13875 | arg->width = len; |
| 13876 | } |
| 13877 | |
| 13878 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 13879 | the alternate form is used */ |
| 13880 | if (fill == ' ') { |
| 13881 | if (arg->sign) { |
| 13882 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 13883 | writer->pos += 1; |
| 13884 | } |
| 13885 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 13886 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13887 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 13888 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 13889 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 13890 | writer->pos += 2; |
| 13891 | pindex += 2; |
| 13892 | } |
| 13893 | } |
| 13894 | |
| 13895 | /* Write characters */ |
| 13896 | if (len) { |
| 13897 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13898 | str, pindex, len); |
| 13899 | writer->pos += len; |
| 13900 | } |
| 13901 | |
| 13902 | /* Pad right with the fill character if needed */ |
| 13903 | if (arg->width > len) { |
| 13904 | sublen = arg->width - len; |
| 13905 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 13906 | writer->pos += sublen; |
| 13907 | } |
| 13908 | return 0; |
| 13909 | } |
| 13910 | |
| 13911 | /* Helper of PyUnicode_Format(): format one arg. |
| 13912 | Return 0 on success, raise an exception and return -1 on error. */ |
| 13913 | static int |
| 13914 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 13915 | { |
| 13916 | struct unicode_format_arg_t arg; |
| 13917 | PyObject *str; |
| 13918 | int ret; |
| 13919 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13920 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 13921 | arg.flags = 0; |
| 13922 | arg.width = -1; |
| 13923 | arg.prec = -1; |
| 13924 | arg.sign = 0; |
| 13925 | str = NULL; |
| 13926 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13927 | ret = unicode_format_arg_parse(ctx, &arg); |
| 13928 | if (ret == -1) |
| 13929 | return -1; |
| 13930 | |
| 13931 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 13932 | if (ret == -1) |
| 13933 | return -1; |
| 13934 | |
| 13935 | if (ret != 1) { |
| 13936 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 13937 | Py_DECREF(str); |
| 13938 | if (ret == -1) |
| 13939 | return -1; |
| 13940 | } |
| 13941 | |
| 13942 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 13943 | PyErr_SetString(PyExc_TypeError, |
| 13944 | "not all arguments converted during string formatting"); |
| 13945 | return -1; |
| 13946 | } |
| 13947 | return 0; |
| 13948 | } |
| 13949 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13950 | PyObject * |
| 13951 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13952 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13953 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13954 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13955 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13956 | PyErr_BadInternalCall(); |
| 13957 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13958 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13959 | |
| 13960 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 13961 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13962 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13963 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 13964 | Py_DECREF(ctx.fmtstr); |
| 13965 | return NULL; |
| 13966 | } |
| 13967 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 13968 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 13969 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 13970 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13971 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13972 | _PyUnicodeWriter_Init(&ctx.writer, ctx.fmtcnt + 100); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13973 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13974 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13975 | ctx.arglen = PyTuple_Size(args); |
| 13976 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13977 | } |
| 13978 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13979 | ctx.arglen = -1; |
| 13980 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13981 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13982 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 13983 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13984 | ctx.dict = args; |
| 13985 | else |
| 13986 | ctx.dict = NULL; |
| 13987 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13988 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13989 | while (--ctx.fmtcnt >= 0) { |
| 13990 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 13991 | Py_ssize_t nonfmtpos, sublen; |
| 13992 | Py_UCS4 maxchar; |
| 13993 | |
| 13994 | nonfmtpos = ctx.fmtpos++; |
| 13995 | while (ctx.fmtcnt >= 0 && |
| 13996 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 13997 | ctx.fmtpos++; |
| 13998 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13999 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14000 | if (ctx.fmtcnt < 0) { |
| 14001 | ctx.fmtpos--; |
| 14002 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 14003 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14004 | sublen = ctx.fmtpos - nonfmtpos; |
| 14005 | maxchar = _PyUnicode_FindMaxChar(ctx.fmtstr, |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14006 | nonfmtpos, nonfmtpos + sublen); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14007 | if (_PyUnicodeWriter_Prepare(&ctx.writer, sublen, maxchar) == -1) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 14008 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 14009 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14010 | _PyUnicode_FastCopyCharacters(ctx.writer.buffer, ctx.writer.pos, |
| 14011 | ctx.fmtstr, nonfmtpos, sublen); |
| 14012 | ctx.writer.pos += sublen; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14013 | } |
| 14014 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14015 | ctx.fmtpos++; |
| 14016 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14017 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14018 | } |
| 14019 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14020 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14021 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14022 | PyErr_SetString(PyExc_TypeError, |
| 14023 | "not all arguments converted during string formatting"); |
| 14024 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14025 | } |
| 14026 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14027 | if (ctx.args_owned) { |
| 14028 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14029 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14030 | Py_DECREF(ctx.fmtstr); |
| 14031 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14032 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14033 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14034 | Py_DECREF(ctx.fmtstr); |
| 14035 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14036 | if (ctx.args_owned) { |
| 14037 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14038 | } |
| 14039 | return NULL; |
| 14040 | } |
| 14041 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14042 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14043 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14044 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14045 | static PyObject * |
| 14046 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14047 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14048 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14049 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14050 | char *encoding = NULL; |
| 14051 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14052 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14053 | if (type != &PyUnicode_Type) |
| 14054 | return unicode_subtype_new(type, args, kwds); |
| 14055 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14056 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14057 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 14058 | if (x == NULL) { |
| 14059 | Py_INCREF(unicode_empty); |
| 14060 | return unicode_empty; |
| 14061 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14062 | if (encoding == NULL && errors == NULL) |
| 14063 | return PyObject_Str(x); |
| 14064 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14065 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14066 | } |
| 14067 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14068 | static PyObject * |
| 14069 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14070 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14071 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14072 | Py_ssize_t length, char_size; |
| 14073 | int share_wstr, share_utf8; |
| 14074 | unsigned int kind; |
| 14075 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14076 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14077 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14078 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14079 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14080 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14081 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14082 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14083 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14084 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14085 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14086 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14087 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14088 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14089 | if (self == NULL) { |
| 14090 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14091 | return NULL; |
| 14092 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14093 | kind = PyUnicode_KIND(unicode); |
| 14094 | length = PyUnicode_GET_LENGTH(unicode); |
| 14095 | |
| 14096 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14097 | #ifdef Py_DEBUG |
| 14098 | _PyUnicode_HASH(self) = -1; |
| 14099 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14100 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14101 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14102 | _PyUnicode_STATE(self).interned = 0; |
| 14103 | _PyUnicode_STATE(self).kind = kind; |
| 14104 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14105 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14106 | _PyUnicode_STATE(self).ready = 1; |
| 14107 | _PyUnicode_WSTR(self) = NULL; |
| 14108 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14109 | _PyUnicode_UTF8(self) = NULL; |
| 14110 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14111 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14112 | |
| 14113 | share_utf8 = 0; |
| 14114 | share_wstr = 0; |
| 14115 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14116 | char_size = 1; |
| 14117 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14118 | share_utf8 = 1; |
| 14119 | } |
| 14120 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14121 | char_size = 2; |
| 14122 | if (sizeof(wchar_t) == 2) |
| 14123 | share_wstr = 1; |
| 14124 | } |
| 14125 | else { |
| 14126 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14127 | char_size = 4; |
| 14128 | if (sizeof(wchar_t) == 4) |
| 14129 | share_wstr = 1; |
| 14130 | } |
| 14131 | |
| 14132 | /* Ensure we won't overflow the length. */ |
| 14133 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14134 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14135 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14136 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14137 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14138 | if (data == NULL) { |
| 14139 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14140 | goto onError; |
| 14141 | } |
| 14142 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14143 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14144 | if (share_utf8) { |
| 14145 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14146 | _PyUnicode_UTF8(self) = data; |
| 14147 | } |
| 14148 | if (share_wstr) { |
| 14149 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14150 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14151 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14152 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14153 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14154 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14155 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14156 | #ifdef Py_DEBUG |
| 14157 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14158 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14159 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14160 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14161 | |
| 14162 | onError: |
| 14163 | Py_DECREF(unicode); |
| 14164 | Py_DECREF(self); |
| 14165 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14166 | } |
| 14167 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14168 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14169 | "str(object='') -> str\n\ |
| 14170 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14171 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14172 | Create a new string object from the given object. If encoding or\n\ |
| 14173 | errors is specified, then the object must expose a data buffer\n\ |
| 14174 | that will be decoded using the given encoding and error handler.\n\ |
| 14175 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14176 | or repr(object).\n\ |
| 14177 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14178 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14179 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14180 | static PyObject *unicode_iter(PyObject *seq); |
| 14181 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14182 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14183 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14184 | "str", /* tp_name */ |
| 14185 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14186 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14187 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14188 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14189 | 0, /* tp_print */ |
| 14190 | 0, /* tp_getattr */ |
| 14191 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14192 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14193 | unicode_repr, /* tp_repr */ |
| 14194 | &unicode_as_number, /* tp_as_number */ |
| 14195 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14196 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14197 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14198 | 0, /* tp_call*/ |
| 14199 | (reprfunc) unicode_str, /* tp_str */ |
| 14200 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14201 | 0, /* tp_setattro */ |
| 14202 | 0, /* tp_as_buffer */ |
| 14203 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14204 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14205 | unicode_doc, /* tp_doc */ |
| 14206 | 0, /* tp_traverse */ |
| 14207 | 0, /* tp_clear */ |
| 14208 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14209 | 0, /* tp_weaklistoffset */ |
| 14210 | unicode_iter, /* tp_iter */ |
| 14211 | 0, /* tp_iternext */ |
| 14212 | unicode_methods, /* tp_methods */ |
| 14213 | 0, /* tp_members */ |
| 14214 | 0, /* tp_getset */ |
| 14215 | &PyBaseObject_Type, /* tp_base */ |
| 14216 | 0, /* tp_dict */ |
| 14217 | 0, /* tp_descr_get */ |
| 14218 | 0, /* tp_descr_set */ |
| 14219 | 0, /* tp_dictoffset */ |
| 14220 | 0, /* tp_init */ |
| 14221 | 0, /* tp_alloc */ |
| 14222 | unicode_new, /* tp_new */ |
| 14223 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14224 | }; |
| 14225 | |
| 14226 | /* Initialize the Unicode implementation */ |
| 14227 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14228 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14229 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14230 | int i; |
| 14231 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14232 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14233 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14234 | 0x000A, /* LINE FEED */ |
| 14235 | 0x000D, /* CARRIAGE RETURN */ |
| 14236 | 0x001C, /* FILE SEPARATOR */ |
| 14237 | 0x001D, /* GROUP SEPARATOR */ |
| 14238 | 0x001E, /* RECORD SEPARATOR */ |
| 14239 | 0x0085, /* NEXT LINE */ |
| 14240 | 0x2028, /* LINE SEPARATOR */ |
| 14241 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14242 | }; |
| 14243 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14244 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14245 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14246 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14247 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14248 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14249 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14250 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14251 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14252 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14253 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14254 | |
| 14255 | /* initialize the linebreak bloom filter */ |
| 14256 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14257 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14258 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14259 | |
| 14260 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14261 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14262 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14263 | Py_FatalError("Can't initialize field name iterator type"); |
| 14264 | |
| 14265 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14266 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14267 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14268 | #ifdef HAVE_MBCS |
| 14269 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14270 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14271 | PyErr_SetFromWindowsErr(0); |
| 14272 | return -1; |
| 14273 | } |
| 14274 | #endif |
| 14275 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14276 | } |
| 14277 | |
| 14278 | /* Finalize the Unicode implementation */ |
| 14279 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14280 | int |
| 14281 | PyUnicode_ClearFreeList(void) |
| 14282 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14283 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14284 | } |
| 14285 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14286 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14287 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14288 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14289 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14290 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14291 | Py_XDECREF(unicode_empty); |
| 14292 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14293 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14294 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14295 | if (unicode_latin1[i]) { |
| 14296 | Py_DECREF(unicode_latin1[i]); |
| 14297 | unicode_latin1[i] = NULL; |
| 14298 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14299 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14300 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14301 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14302 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14303 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14304 | void |
| 14305 | PyUnicode_InternInPlace(PyObject **p) |
| 14306 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14307 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14308 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14309 | #ifdef Py_DEBUG |
| 14310 | assert(s != NULL); |
| 14311 | assert(_PyUnicode_CHECK(s)); |
| 14312 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14313 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14314 | return; |
| 14315 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14316 | /* If it's a subclass, we don't really know what putting |
| 14317 | it in the interned dict might do. */ |
| 14318 | if (!PyUnicode_CheckExact(s)) |
| 14319 | return; |
| 14320 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14321 | return; |
| 14322 | if (interned == NULL) { |
| 14323 | interned = PyDict_New(); |
| 14324 | if (interned == NULL) { |
| 14325 | PyErr_Clear(); /* Don't leave an exception */ |
| 14326 | return; |
| 14327 | } |
| 14328 | } |
| 14329 | /* It might be that the GetItem call fails even |
| 14330 | though the key is present in the dictionary, |
| 14331 | namely when this happens during a stack overflow. */ |
| 14332 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14333 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14334 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14335 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14336 | if (t) { |
| 14337 | Py_INCREF(t); |
| 14338 | Py_DECREF(*p); |
| 14339 | *p = t; |
| 14340 | return; |
| 14341 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14342 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14343 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14344 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14345 | PyErr_Clear(); |
| 14346 | PyThreadState_GET()->recursion_critical = 0; |
| 14347 | return; |
| 14348 | } |
| 14349 | PyThreadState_GET()->recursion_critical = 0; |
| 14350 | /* The two references in interned are not counted by refcnt. |
| 14351 | The deallocator will take care of this */ |
| 14352 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14353 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14354 | } |
| 14355 | |
| 14356 | void |
| 14357 | PyUnicode_InternImmortal(PyObject **p) |
| 14358 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14359 | PyUnicode_InternInPlace(p); |
| 14360 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14361 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14362 | Py_INCREF(*p); |
| 14363 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14364 | } |
| 14365 | |
| 14366 | PyObject * |
| 14367 | PyUnicode_InternFromString(const char *cp) |
| 14368 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14369 | PyObject *s = PyUnicode_FromString(cp); |
| 14370 | if (s == NULL) |
| 14371 | return NULL; |
| 14372 | PyUnicode_InternInPlace(&s); |
| 14373 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14374 | } |
| 14375 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14376 | void |
| 14377 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14378 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14379 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14380 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14381 | Py_ssize_t i, n; |
| 14382 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14383 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14384 | if (interned == NULL || !PyDict_Check(interned)) |
| 14385 | return; |
| 14386 | keys = PyDict_Keys(interned); |
| 14387 | if (keys == NULL || !PyList_Check(keys)) { |
| 14388 | PyErr_Clear(); |
| 14389 | return; |
| 14390 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14391 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14392 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14393 | detector, interned unicode strings are not forcibly deallocated; |
| 14394 | rather, we give them their stolen references back, and then clear |
| 14395 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14396 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14397 | n = PyList_GET_SIZE(keys); |
| 14398 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14399 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14400 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14401 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14402 | if (PyUnicode_READY(s) == -1) { |
| 14403 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14404 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14405 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14406 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14407 | case SSTATE_NOT_INTERNED: |
| 14408 | /* XXX Shouldn't happen */ |
| 14409 | break; |
| 14410 | case SSTATE_INTERNED_IMMORTAL: |
| 14411 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14412 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14413 | break; |
| 14414 | case SSTATE_INTERNED_MORTAL: |
| 14415 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14416 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14417 | break; |
| 14418 | default: |
| 14419 | Py_FatalError("Inconsistent interned string state."); |
| 14420 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14421 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14422 | } |
| 14423 | fprintf(stderr, "total size of all interned strings: " |
| 14424 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14425 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14426 | Py_DECREF(keys); |
| 14427 | PyDict_Clear(interned); |
| 14428 | Py_DECREF(interned); |
| 14429 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14430 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14431 | |
| 14432 | |
| 14433 | /********************* Unicode Iterator **************************/ |
| 14434 | |
| 14435 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14436 | PyObject_HEAD |
| 14437 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14438 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14439 | } unicodeiterobject; |
| 14440 | |
| 14441 | static void |
| 14442 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14443 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14444 | _PyObject_GC_UNTRACK(it); |
| 14445 | Py_XDECREF(it->it_seq); |
| 14446 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14447 | } |
| 14448 | |
| 14449 | static int |
| 14450 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14451 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14452 | Py_VISIT(it->it_seq); |
| 14453 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14454 | } |
| 14455 | |
| 14456 | static PyObject * |
| 14457 | unicodeiter_next(unicodeiterobject *it) |
| 14458 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14459 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14460 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14461 | assert(it != NULL); |
| 14462 | seq = it->it_seq; |
| 14463 | if (seq == NULL) |
| 14464 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14465 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14466 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14467 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14468 | int kind = PyUnicode_KIND(seq); |
| 14469 | void *data = PyUnicode_DATA(seq); |
| 14470 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14471 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14472 | if (item != NULL) |
| 14473 | ++it->it_index; |
| 14474 | return item; |
| 14475 | } |
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 | Py_DECREF(seq); |
| 14478 | it->it_seq = NULL; |
| 14479 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14480 | } |
| 14481 | |
| 14482 | static PyObject * |
| 14483 | unicodeiter_len(unicodeiterobject *it) |
| 14484 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14485 | Py_ssize_t len = 0; |
| 14486 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14487 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14488 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14489 | } |
| 14490 | |
| 14491 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14492 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14493 | static PyObject * |
| 14494 | unicodeiter_reduce(unicodeiterobject *it) |
| 14495 | { |
| 14496 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14497 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14498 | it->it_seq, it->it_index); |
| 14499 | } else { |
| 14500 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14501 | if (u == NULL) |
| 14502 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14503 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14504 | } |
| 14505 | } |
| 14506 | |
| 14507 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14508 | |
| 14509 | static PyObject * |
| 14510 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14511 | { |
| 14512 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14513 | if (index == -1 && PyErr_Occurred()) |
| 14514 | return NULL; |
| 14515 | if (index < 0) |
| 14516 | index = 0; |
| 14517 | it->it_index = index; |
| 14518 | Py_RETURN_NONE; |
| 14519 | } |
| 14520 | |
| 14521 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14522 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14523 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14524 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14525 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14526 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14527 | reduce_doc}, |
| 14528 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14529 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14530 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14531 | }; |
| 14532 | |
| 14533 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14534 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14535 | "str_iterator", /* tp_name */ |
| 14536 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14537 | 0, /* tp_itemsize */ |
| 14538 | /* methods */ |
| 14539 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14540 | 0, /* tp_print */ |
| 14541 | 0, /* tp_getattr */ |
| 14542 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14543 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14544 | 0, /* tp_repr */ |
| 14545 | 0, /* tp_as_number */ |
| 14546 | 0, /* tp_as_sequence */ |
| 14547 | 0, /* tp_as_mapping */ |
| 14548 | 0, /* tp_hash */ |
| 14549 | 0, /* tp_call */ |
| 14550 | 0, /* tp_str */ |
| 14551 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14552 | 0, /* tp_setattro */ |
| 14553 | 0, /* tp_as_buffer */ |
| 14554 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14555 | 0, /* tp_doc */ |
| 14556 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14557 | 0, /* tp_clear */ |
| 14558 | 0, /* tp_richcompare */ |
| 14559 | 0, /* tp_weaklistoffset */ |
| 14560 | PyObject_SelfIter, /* tp_iter */ |
| 14561 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14562 | unicodeiter_methods, /* tp_methods */ |
| 14563 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14564 | }; |
| 14565 | |
| 14566 | static PyObject * |
| 14567 | unicode_iter(PyObject *seq) |
| 14568 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14569 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14570 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14571 | if (!PyUnicode_Check(seq)) { |
| 14572 | PyErr_BadInternalCall(); |
| 14573 | return NULL; |
| 14574 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14575 | if (PyUnicode_READY(seq) == -1) |
| 14576 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14577 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14578 | if (it == NULL) |
| 14579 | return NULL; |
| 14580 | it->it_index = 0; |
| 14581 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14582 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14583 | _PyObject_GC_TRACK(it); |
| 14584 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14585 | } |
| 14586 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14587 | |
| 14588 | size_t |
| 14589 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14590 | { |
| 14591 | int res = 0; |
| 14592 | while(*u++) |
| 14593 | res++; |
| 14594 | return res; |
| 14595 | } |
| 14596 | |
| 14597 | Py_UNICODE* |
| 14598 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14599 | { |
| 14600 | Py_UNICODE *u = s1; |
| 14601 | while ((*u++ = *s2++)); |
| 14602 | return s1; |
| 14603 | } |
| 14604 | |
| 14605 | Py_UNICODE* |
| 14606 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14607 | { |
| 14608 | Py_UNICODE *u = s1; |
| 14609 | while ((*u++ = *s2++)) |
| 14610 | if (n-- == 0) |
| 14611 | break; |
| 14612 | return s1; |
| 14613 | } |
| 14614 | |
| 14615 | Py_UNICODE* |
| 14616 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14617 | { |
| 14618 | Py_UNICODE *u1 = s1; |
| 14619 | u1 += Py_UNICODE_strlen(u1); |
| 14620 | Py_UNICODE_strcpy(u1, s2); |
| 14621 | return s1; |
| 14622 | } |
| 14623 | |
| 14624 | int |
| 14625 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14626 | { |
| 14627 | while (*s1 && *s2 && *s1 == *s2) |
| 14628 | s1++, s2++; |
| 14629 | if (*s1 && *s2) |
| 14630 | return (*s1 < *s2) ? -1 : +1; |
| 14631 | if (*s1) |
| 14632 | return 1; |
| 14633 | if (*s2) |
| 14634 | return -1; |
| 14635 | return 0; |
| 14636 | } |
| 14637 | |
| 14638 | int |
| 14639 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14640 | { |
| 14641 | register Py_UNICODE u1, u2; |
| 14642 | for (; n != 0; n--) { |
| 14643 | u1 = *s1; |
| 14644 | u2 = *s2; |
| 14645 | if (u1 != u2) |
| 14646 | return (u1 < u2) ? -1 : +1; |
| 14647 | if (u1 == '\0') |
| 14648 | return 0; |
| 14649 | s1++; |
| 14650 | s2++; |
| 14651 | } |
| 14652 | return 0; |
| 14653 | } |
| 14654 | |
| 14655 | Py_UNICODE* |
| 14656 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14657 | { |
| 14658 | const Py_UNICODE *p; |
| 14659 | for (p = s; *p; p++) |
| 14660 | if (*p == c) |
| 14661 | return (Py_UNICODE*)p; |
| 14662 | return NULL; |
| 14663 | } |
| 14664 | |
| 14665 | Py_UNICODE* |
| 14666 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14667 | { |
| 14668 | const Py_UNICODE *p; |
| 14669 | p = s + Py_UNICODE_strlen(s); |
| 14670 | while (p != s) { |
| 14671 | p--; |
| 14672 | if (*p == c) |
| 14673 | return (Py_UNICODE*)p; |
| 14674 | } |
| 14675 | return NULL; |
| 14676 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14677 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14678 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14679 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14680 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14681 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14682 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14683 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14684 | if (!PyUnicode_Check(unicode)) { |
| 14685 | PyErr_BadArgument(); |
| 14686 | return NULL; |
| 14687 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14688 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14689 | if (u == NULL) |
| 14690 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14691 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14692 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14693 | PyErr_NoMemory(); |
| 14694 | return NULL; |
| 14695 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14696 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14697 | size *= sizeof(Py_UNICODE); |
| 14698 | copy = PyMem_Malloc(size); |
| 14699 | if (copy == NULL) { |
| 14700 | PyErr_NoMemory(); |
| 14701 | return NULL; |
| 14702 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14703 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14704 | return copy; |
| 14705 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14706 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14707 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14708 | to the string.Formatter class implemented in Python. */ |
| 14709 | |
| 14710 | static PyMethodDef _string_methods[] = { |
| 14711 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14712 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14713 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14714 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14715 | {NULL, NULL} |
| 14716 | }; |
| 14717 | |
| 14718 | static struct PyModuleDef _string_module = { |
| 14719 | PyModuleDef_HEAD_INIT, |
| 14720 | "_string", |
| 14721 | PyDoc_STR("string helper module"), |
| 14722 | 0, |
| 14723 | _string_methods, |
| 14724 | NULL, |
| 14725 | NULL, |
| 14726 | NULL, |
| 14727 | NULL |
| 14728 | }; |
| 14729 | |
| 14730 | PyMODINIT_FUNC |
| 14731 | PyInit__string(void) |
| 14732 | { |
| 14733 | return PyModule_Create(&_string_module); |
| 14734 | } |
| 14735 | |
| 14736 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14737 | #ifdef __cplusplus |
| 14738 | } |
| 14739 | #endif |