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 | { |
| 1825 | _Py_Identifier *i; |
| 1826 | for (i = static_strings; i; i = i->next) { |
| 1827 | Py_DECREF(i->object); |
| 1828 | i->object = NULL; |
| 1829 | i->next = NULL; |
| 1830 | } |
| 1831 | } |
| 1832 | |
Benjamin Peterson | 0df5429 | 2012-03-26 14:50:32 -0400 | [diff] [blame] | 1833 | /* Internal function, doesn't check maximum character */ |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1834 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1835 | PyObject* |
| 1836 | _PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1837 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1838 | const unsigned char *s = (const unsigned char *)buffer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1839 | PyObject *unicode; |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1840 | if (size == 1) { |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1841 | #ifdef Py_DEBUG |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1842 | assert(s[0] < 128); |
Victor Stinner | 0617b6e | 2011-10-05 23:26:01 +0200 | [diff] [blame] | 1843 | #endif |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1844 | return get_latin1_char(s[0]); |
Victor Stinner | e6b2d44 | 2011-12-11 21:54:30 +0100 | [diff] [blame] | 1845 | } |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1846 | unicode = PyUnicode_New(size, 127); |
| 1847 | if (!unicode) |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1848 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 1849 | memcpy(PyUnicode_1BYTE_DATA(unicode), s, size); |
| 1850 | assert(_PyUnicode_CheckConsistency(unicode, 1)); |
| 1851 | return unicode; |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1852 | } |
| 1853 | |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1854 | static Py_UCS4 |
| 1855 | kind_maxchar_limit(unsigned int kind) |
| 1856 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1857 | switch (kind) { |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1858 | case PyUnicode_1BYTE_KIND: |
| 1859 | return 0x80; |
| 1860 | case PyUnicode_2BYTE_KIND: |
| 1861 | return 0x100; |
| 1862 | case PyUnicode_4BYTE_KIND: |
| 1863 | return 0x10000; |
| 1864 | default: |
| 1865 | assert(0 && "invalid kind"); |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 1866 | return MAX_UNICODE; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 1870 | Py_LOCAL_INLINE(Py_UCS4) |
| 1871 | align_maxchar(Py_UCS4 maxchar) |
| 1872 | { |
| 1873 | if (maxchar <= 127) |
| 1874 | return 127; |
| 1875 | else if (maxchar <= 255) |
| 1876 | return 255; |
| 1877 | else if (maxchar <= 65535) |
| 1878 | return 65535; |
| 1879 | else |
| 1880 | return MAX_UNICODE; |
| 1881 | } |
| 1882 | |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 1883 | static PyObject* |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1884 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1885 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1886 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1887 | unsigned char max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1888 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1889 | if (size == 0) { |
| 1890 | Py_INCREF(unicode_empty); |
| 1891 | return unicode_empty; |
| 1892 | } |
| 1893 | assert(size > 0); |
Antoine Pitrou | 7c46da7 | 2011-10-06 22:07:51 +0200 | [diff] [blame] | 1894 | if (size == 1) |
| 1895 | return get_latin1_char(u[0]); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1896 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1897 | max_char = ucs1lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1898 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1899 | if (!res) |
| 1900 | return NULL; |
| 1901 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1902 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1903 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1906 | static PyObject* |
| 1907 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1908 | { |
| 1909 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1910 | Py_UCS2 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1911 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1912 | if (size == 0) { |
| 1913 | Py_INCREF(unicode_empty); |
| 1914 | return unicode_empty; |
| 1915 | } |
| 1916 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1917 | if (size == 1) { |
| 1918 | Py_UCS4 ch = u[0]; |
| 1919 | if (ch < 256) |
| 1920 | return get_latin1_char((unsigned char)ch); |
| 1921 | |
| 1922 | res = PyUnicode_New(1, ch); |
| 1923 | if (res == NULL) |
| 1924 | return NULL; |
| 1925 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1926 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1927 | return res; |
| 1928 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1929 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1930 | max_char = ucs2lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1931 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1932 | if (!res) |
| 1933 | return NULL; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1934 | if (max_char >= 256) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1935 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1936 | else { |
| 1937 | _PyUnicode_CONVERT_BYTES( |
| 1938 | Py_UCS2, Py_UCS1, u, u + size, PyUnicode_1BYTE_DATA(res)); |
| 1939 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1940 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1941 | return res; |
| 1942 | } |
| 1943 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1944 | static PyObject* |
| 1945 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1946 | { |
| 1947 | PyObject *res; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1948 | Py_UCS4 max_char; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1949 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1950 | if (size == 0) { |
| 1951 | Py_INCREF(unicode_empty); |
| 1952 | return unicode_empty; |
| 1953 | } |
| 1954 | assert(size > 0); |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 1955 | if (size == 1) { |
| 1956 | Py_UCS4 ch = u[0]; |
| 1957 | if (ch < 256) |
| 1958 | return get_latin1_char((unsigned char)ch); |
| 1959 | |
| 1960 | res = PyUnicode_New(1, ch); |
| 1961 | if (res == NULL) |
| 1962 | return NULL; |
| 1963 | PyUnicode_WRITE(PyUnicode_KIND(res), PyUnicode_DATA(res), 0, ch); |
| 1964 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 1965 | return res; |
| 1966 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 1967 | |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 1968 | max_char = ucs4lib_find_max_char(u, u + size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1969 | res = PyUnicode_New(size, max_char); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1970 | if (!res) |
| 1971 | return NULL; |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 1972 | if (max_char < 256) |
| 1973 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, u, u + size, |
| 1974 | PyUnicode_1BYTE_DATA(res)); |
| 1975 | else if (max_char < 0x10000) |
| 1976 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, u, u + size, |
| 1977 | PyUnicode_2BYTE_DATA(res)); |
| 1978 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1979 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 1980 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1981 | return res; |
| 1982 | } |
| 1983 | |
| 1984 | PyObject* |
| 1985 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1986 | { |
Victor Stinner | cfed46e | 2011-11-22 01:29:14 +0100 | [diff] [blame] | 1987 | if (size < 0) { |
| 1988 | PyErr_SetString(PyExc_ValueError, "size must be positive"); |
| 1989 | return NULL; |
| 1990 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 1991 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1992 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1993 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1994 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1995 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1996 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1997 | return _PyUnicode_FromUCS4(buffer, size); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1998 | default: |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 1999 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
| 2000 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2001 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2002 | } |
| 2003 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2004 | Py_UCS4 |
| 2005 | _PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) |
| 2006 | { |
| 2007 | enum PyUnicode_Kind kind; |
| 2008 | void *startptr, *endptr; |
| 2009 | |
| 2010 | assert(PyUnicode_IS_READY(unicode)); |
| 2011 | assert(0 <= start); |
| 2012 | assert(end <= PyUnicode_GET_LENGTH(unicode)); |
| 2013 | assert(start <= end); |
| 2014 | |
| 2015 | if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) |
| 2016 | return PyUnicode_MAX_CHAR_VALUE(unicode); |
| 2017 | |
| 2018 | if (start == end) |
| 2019 | return 127; |
| 2020 | |
Victor Stinner | 94d558b | 2012-04-27 22:26:58 +0200 | [diff] [blame] | 2021 | if (PyUnicode_IS_ASCII(unicode)) |
| 2022 | return 127; |
| 2023 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2024 | kind = PyUnicode_KIND(unicode); |
Benjamin Peterson | f3b7d86 | 2012-04-23 18:07:01 -0400 | [diff] [blame] | 2025 | startptr = PyUnicode_DATA(unicode); |
Benjamin Peterson | b9f4c9d | 2012-04-23 21:45:40 -0400 | [diff] [blame] | 2026 | endptr = (char *)startptr + end * kind; |
| 2027 | startptr = (char *)startptr + start * kind; |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2028 | switch(kind) { |
| 2029 | case PyUnicode_1BYTE_KIND: |
| 2030 | return ucs1lib_find_max_char(startptr, endptr); |
| 2031 | case PyUnicode_2BYTE_KIND: |
| 2032 | return ucs2lib_find_max_char(startptr, endptr); |
| 2033 | case PyUnicode_4BYTE_KIND: |
| 2034 | return ucs4lib_find_max_char(startptr, endptr); |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2035 | default: |
Benjamin Peterson | 2844a7a | 2012-04-23 18:00:25 -0400 | [diff] [blame] | 2036 | assert(0); |
| 2037 | return 0; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2041 | /* Ensure that a string uses the most efficient storage, if it is not the |
| 2042 | case: create a new string with of the right kind. Write NULL into *p_unicode |
| 2043 | on error. */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 2044 | static void |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2045 | unicode_adjust_maxchar(PyObject **p_unicode) |
| 2046 | { |
| 2047 | PyObject *unicode, *copy; |
| 2048 | Py_UCS4 max_char; |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2049 | Py_ssize_t len; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2050 | unsigned int kind; |
| 2051 | |
| 2052 | assert(p_unicode != NULL); |
| 2053 | unicode = *p_unicode; |
| 2054 | assert(PyUnicode_IS_READY(unicode)); |
| 2055 | if (PyUnicode_IS_ASCII(unicode)) |
| 2056 | return; |
| 2057 | |
| 2058 | len = PyUnicode_GET_LENGTH(unicode); |
| 2059 | kind = PyUnicode_KIND(unicode); |
| 2060 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2061 | const Py_UCS1 *u = PyUnicode_1BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2062 | max_char = ucs1lib_find_max_char(u, u + len); |
| 2063 | if (max_char >= 128) |
| 2064 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2065 | } |
| 2066 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2067 | const Py_UCS2 *u = PyUnicode_2BYTE_DATA(unicode); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2068 | max_char = ucs2lib_find_max_char(u, u + len); |
| 2069 | if (max_char >= 256) |
| 2070 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2071 | } |
| 2072 | else { |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2073 | const Py_UCS4 *u = PyUnicode_4BYTE_DATA(unicode); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2074 | assert(kind == PyUnicode_4BYTE_KIND); |
Antoine Pitrou | dd4e2f0 | 2011-10-13 00:02:27 +0200 | [diff] [blame] | 2075 | max_char = ucs4lib_find_max_char(u, u + len); |
| 2076 | if (max_char >= 0x10000) |
| 2077 | return; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2078 | } |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2079 | copy = PyUnicode_New(len, max_char); |
Victor Stinner | ca439ee | 2012-06-16 03:17:34 +0200 | [diff] [blame] | 2080 | if (copy != NULL) |
| 2081 | _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 2082 | Py_DECREF(unicode); |
| 2083 | *p_unicode = copy; |
| 2084 | } |
| 2085 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2086 | PyObject* |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2087 | _PyUnicode_Copy(PyObject *unicode) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2088 | { |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2089 | Py_ssize_t length; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2090 | PyObject *copy; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2091 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2092 | if (!PyUnicode_Check(unicode)) { |
| 2093 | PyErr_BadInternalCall(); |
| 2094 | return NULL; |
| 2095 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2096 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2097 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2098 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2099 | length = PyUnicode_GET_LENGTH(unicode); |
| 2100 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2101 | if (!copy) |
| 2102 | return NULL; |
| 2103 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 2104 | |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 2105 | Py_MEMCPY(PyUnicode_DATA(copy), PyUnicode_DATA(unicode), |
| 2106 | length * PyUnicode_KIND(unicode)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2107 | assert(_PyUnicode_CheckConsistency(copy, 1)); |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 2108 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 2109 | } |
| 2110 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2111 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2112 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 2113 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2114 | |
| 2115 | void* |
| 2116 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 2117 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2118 | Py_ssize_t len; |
| 2119 | void *result; |
| 2120 | unsigned int skind; |
| 2121 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2122 | if (PyUnicode_READY(s) == -1) |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2123 | return NULL; |
| 2124 | |
| 2125 | len = PyUnicode_GET_LENGTH(s); |
| 2126 | skind = PyUnicode_KIND(s); |
| 2127 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2128 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2129 | return NULL; |
| 2130 | } |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 2131 | switch (kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2132 | case PyUnicode_2BYTE_KIND: |
| 2133 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 2134 | if (!result) |
| 2135 | return PyErr_NoMemory(); |
| 2136 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2137 | _PyUnicode_CONVERT_BYTES( |
| 2138 | Py_UCS1, Py_UCS2, |
| 2139 | PyUnicode_1BYTE_DATA(s), |
| 2140 | PyUnicode_1BYTE_DATA(s) + len, |
| 2141 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2142 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2143 | case PyUnicode_4BYTE_KIND: |
| 2144 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 2145 | if (!result) |
| 2146 | return PyErr_NoMemory(); |
| 2147 | if (skind == PyUnicode_2BYTE_KIND) { |
| 2148 | _PyUnicode_CONVERT_BYTES( |
| 2149 | Py_UCS2, Py_UCS4, |
| 2150 | PyUnicode_2BYTE_DATA(s), |
| 2151 | PyUnicode_2BYTE_DATA(s) + len, |
| 2152 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2153 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2154 | else { |
| 2155 | assert(skind == PyUnicode_1BYTE_KIND); |
| 2156 | _PyUnicode_CONVERT_BYTES( |
| 2157 | Py_UCS1, Py_UCS4, |
| 2158 | PyUnicode_1BYTE_DATA(s), |
| 2159 | PyUnicode_1BYTE_DATA(s) + len, |
| 2160 | result); |
| 2161 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2162 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 2163 | default: |
| 2164 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2165 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 2166 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2167 | return NULL; |
| 2168 | } |
| 2169 | |
| 2170 | static Py_UCS4* |
| 2171 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2172 | int copy_null) |
| 2173 | { |
| 2174 | int kind; |
| 2175 | void *data; |
| 2176 | Py_ssize_t len, targetlen; |
| 2177 | if (PyUnicode_READY(string) == -1) |
| 2178 | return NULL; |
| 2179 | kind = PyUnicode_KIND(string); |
| 2180 | data = PyUnicode_DATA(string); |
| 2181 | len = PyUnicode_GET_LENGTH(string); |
| 2182 | targetlen = len; |
| 2183 | if (copy_null) |
| 2184 | targetlen++; |
| 2185 | if (!target) { |
| 2186 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 2187 | PyErr_NoMemory(); |
| 2188 | return NULL; |
| 2189 | } |
| 2190 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 2191 | if (!target) { |
| 2192 | PyErr_NoMemory(); |
| 2193 | return NULL; |
| 2194 | } |
| 2195 | } |
| 2196 | else { |
| 2197 | if (targetsize < targetlen) { |
| 2198 | PyErr_Format(PyExc_SystemError, |
| 2199 | "string is longer than the buffer"); |
| 2200 | if (copy_null && 0 < targetsize) |
| 2201 | target[0] = 0; |
| 2202 | return NULL; |
| 2203 | } |
| 2204 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2205 | if (kind == PyUnicode_1BYTE_KIND) { |
| 2206 | Py_UCS1 *start = (Py_UCS1 *) data; |
| 2207 | _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] | 2208 | } |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2209 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 2210 | Py_UCS2 *start = (Py_UCS2 *) data; |
| 2211 | _PyUnicode_CONVERT_BYTES(Py_UCS2, Py_UCS4, start, start + len, target); |
| 2212 | } |
| 2213 | else { |
| 2214 | assert(kind == PyUnicode_4BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2215 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
Antoine Pitrou | 950468e | 2011-10-11 22:45:48 +0200 | [diff] [blame] | 2216 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2217 | if (copy_null) |
| 2218 | target[len] = 0; |
| 2219 | return target; |
| 2220 | } |
| 2221 | |
| 2222 | Py_UCS4* |
| 2223 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 2224 | int copy_null) |
| 2225 | { |
Antoine Pitrou | de20b0b | 2011-11-10 21:47:38 +0100 | [diff] [blame] | 2226 | if (target == NULL || targetsize < 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2227 | PyErr_BadInternalCall(); |
| 2228 | return NULL; |
| 2229 | } |
| 2230 | return as_ucs4(string, target, targetsize, copy_null); |
| 2231 | } |
| 2232 | |
| 2233 | Py_UCS4* |
| 2234 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 2235 | { |
| 2236 | return as_ucs4(string, NULL, 0, 1); |
| 2237 | } |
| 2238 | |
| 2239 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2240 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2241 | PyObject * |
| 2242 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2243 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2244 | if (w == NULL) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 2245 | if (size == 0) { |
| 2246 | Py_INCREF(unicode_empty); |
| 2247 | return unicode_empty; |
| 2248 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2249 | PyErr_BadInternalCall(); |
| 2250 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2253 | if (size == -1) { |
| 2254 | size = wcslen(w); |
| 2255 | } |
| 2256 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2257 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2260 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 2261 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2262 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2263 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2264 | char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2265 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2266 | *fmt++ = '%'; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2267 | if (longflag) |
| 2268 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2269 | else if (longlongflag) { |
| 2270 | /* longlongflag should only ever be nonzero on machines with |
| 2271 | HAVE_LONG_LONG defined */ |
| 2272 | #ifdef HAVE_LONG_LONG |
| 2273 | char *f = PY_FORMAT_LONG_LONG; |
| 2274 | while (*f) |
| 2275 | *fmt++ = *f++; |
| 2276 | #else |
| 2277 | /* we shouldn't ever get here */ |
| 2278 | assert(0); |
| 2279 | *fmt++ = 'l'; |
| 2280 | #endif |
| 2281 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2282 | else if (size_tflag) { |
| 2283 | char *f = PY_FORMAT_SIZE_T; |
| 2284 | while (*f) |
| 2285 | *fmt++ = *f++; |
| 2286 | } |
| 2287 | *fmt++ = c; |
| 2288 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2291 | /* maximum number of characters required for output of %lld or %p. |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2292 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 2293 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 2294 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2295 | |
| 2296 | static const char* |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2297 | unicode_fromformat_arg(_PyUnicodeWriter *writer, |
| 2298 | const char *f, va_list *vargs) |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2299 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2300 | const char *p; |
| 2301 | Py_ssize_t len; |
| 2302 | int zeropad; |
| 2303 | int width; |
| 2304 | int precision; |
| 2305 | int longflag; |
| 2306 | int longlongflag; |
| 2307 | int size_tflag; |
| 2308 | int fill; |
| 2309 | |
| 2310 | p = f; |
| 2311 | f++; |
Victor Stinner | 4c63a97 | 2012-10-06 23:55:33 +0200 | [diff] [blame] | 2312 | zeropad = 0; |
| 2313 | if (*f == '0') { |
| 2314 | zeropad = 1; |
| 2315 | f++; |
| 2316 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2317 | |
| 2318 | /* 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] | 2319 | width = 0; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2320 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2321 | if (width > (INT_MAX - ((int)*f - '0')) / 10) { |
| 2322 | PyErr_SetString(PyExc_ValueError, |
| 2323 | "width too big"); |
| 2324 | return NULL; |
| 2325 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2326 | width = (width*10) + (*f - '0'); |
| 2327 | f++; |
| 2328 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2329 | precision = 0; |
| 2330 | if (*f == '.') { |
| 2331 | f++; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2332 | while (Py_ISDIGIT((unsigned)*f)) { |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 2333 | if (precision > (INT_MAX - ((int)*f - '0')) / 10) { |
| 2334 | PyErr_SetString(PyExc_ValueError, |
| 2335 | "precision too big"); |
| 2336 | return NULL; |
| 2337 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2338 | precision = (precision*10) + (*f - '0'); |
| 2339 | f++; |
| 2340 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2341 | if (*f == '%') { |
| 2342 | /* "%.3%s" => f points to "3" */ |
| 2343 | f--; |
| 2344 | } |
| 2345 | } |
| 2346 | if (*f == '\0') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2347 | /* bogus format "%.123" => go backward, f points to "3" */ |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2348 | f--; |
| 2349 | } |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2350 | |
| 2351 | /* Handle %ld, %lu, %lld and %llu. */ |
| 2352 | longflag = 0; |
| 2353 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 2354 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2355 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2356 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2357 | longflag = 1; |
| 2358 | ++f; |
| 2359 | } |
| 2360 | #ifdef HAVE_LONG_LONG |
| 2361 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2362 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2363 | longlongflag = 1; |
| 2364 | f += 2; |
| 2365 | } |
| 2366 | #endif |
| 2367 | } |
| 2368 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2369 | 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] | 2370 | size_tflag = 1; |
| 2371 | ++f; |
| 2372 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2373 | |
| 2374 | if (f[1] == '\0') |
| 2375 | writer->overallocate = 0; |
| 2376 | |
| 2377 | switch (*f) { |
| 2378 | case 'c': |
| 2379 | { |
| 2380 | int ordinal = va_arg(*vargs, int); |
Victor Stinner | ff5a848 | 2012-10-06 23:05:45 +0200 | [diff] [blame] | 2381 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
| 2382 | PyErr_SetString(PyExc_ValueError, |
| 2383 | "character argument not in range(0x110000)"); |
| 2384 | return NULL; |
| 2385 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2386 | if (_PyUnicodeWriter_Prepare(writer, 1, ordinal) == -1) |
| 2387 | return NULL; |
| 2388 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ordinal); |
| 2389 | writer->pos++; |
| 2390 | break; |
| 2391 | } |
| 2392 | |
| 2393 | case 'i': |
| 2394 | case 'd': |
| 2395 | case 'u': |
| 2396 | case 'x': |
| 2397 | { |
| 2398 | /* used by sprintf */ |
| 2399 | char fmt[10]; /* should be enough for "%0lld\0" */ |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2400 | char buffer[MAX_LONG_LONG_CHARS]; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2401 | |
| 2402 | if (*f == 'u') { |
| 2403 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2404 | |
| 2405 | if (longflag) |
| 2406 | len = sprintf(buffer, fmt, |
| 2407 | va_arg(*vargs, unsigned long)); |
| 2408 | #ifdef HAVE_LONG_LONG |
| 2409 | else if (longlongflag) |
| 2410 | len = sprintf(buffer, fmt, |
| 2411 | va_arg(*vargs, unsigned PY_LONG_LONG)); |
| 2412 | #endif |
| 2413 | else if (size_tflag) |
| 2414 | len = sprintf(buffer, fmt, |
| 2415 | va_arg(*vargs, size_t)); |
| 2416 | else |
| 2417 | len = sprintf(buffer, fmt, |
| 2418 | va_arg(*vargs, unsigned int)); |
| 2419 | } |
| 2420 | else if (*f == 'x') { |
| 2421 | makefmt(fmt, 0, 0, 0, 'x'); |
| 2422 | len = sprintf(buffer, fmt, va_arg(*vargs, int)); |
| 2423 | } |
| 2424 | else { |
| 2425 | makefmt(fmt, longflag, longlongflag, size_tflag, *f); |
| 2426 | |
| 2427 | if (longflag) |
| 2428 | len = sprintf(buffer, fmt, |
| 2429 | va_arg(*vargs, long)); |
| 2430 | #ifdef HAVE_LONG_LONG |
| 2431 | else if (longlongflag) |
| 2432 | len = sprintf(buffer, fmt, |
| 2433 | va_arg(*vargs, PY_LONG_LONG)); |
| 2434 | #endif |
| 2435 | else if (size_tflag) |
| 2436 | len = sprintf(buffer, fmt, |
| 2437 | va_arg(*vargs, Py_ssize_t)); |
| 2438 | else |
| 2439 | len = sprintf(buffer, fmt, |
| 2440 | va_arg(*vargs, int)); |
| 2441 | } |
| 2442 | assert(len >= 0); |
| 2443 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2444 | if (precision < len) |
| 2445 | precision = len; |
| 2446 | if (width > precision) { |
| 2447 | Py_UCS4 fillchar; |
| 2448 | fill = width - precision; |
| 2449 | fillchar = zeropad?'0':' '; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2450 | if (_PyUnicodeWriter_Prepare(writer, fill, fillchar) == -1) |
| 2451 | return NULL; |
| 2452 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, fillchar) == -1) |
| 2453 | return NULL; |
| 2454 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2455 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2456 | if (precision > len) { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2457 | fill = precision - len; |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2458 | if (_PyUnicodeWriter_Prepare(writer, fill, '0') == -1) |
| 2459 | return NULL; |
| 2460 | if (PyUnicode_Fill(writer->buffer, writer->pos, fill, '0') == -1) |
| 2461 | return NULL; |
| 2462 | writer->pos += fill; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2463 | } |
Victor Stinner | 15a1136 | 2012-10-06 23:48:20 +0200 | [diff] [blame] | 2464 | if (_PyUnicodeWriter_WriteCstr(writer, buffer, len) == -1) |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2465 | return NULL; |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2466 | break; |
| 2467 | } |
| 2468 | |
| 2469 | case 'p': |
| 2470 | { |
| 2471 | char number[MAX_LONG_LONG_CHARS]; |
| 2472 | |
| 2473 | len = sprintf(number, "%p", va_arg(*vargs, void*)); |
| 2474 | assert(len >= 0); |
| 2475 | |
| 2476 | /* %p is ill-defined: ensure leading 0x. */ |
| 2477 | if (number[1] == 'X') |
| 2478 | number[1] = 'x'; |
| 2479 | else if (number[1] != 'x') { |
| 2480 | memmove(number + 2, number, |
| 2481 | strlen(number) + 1); |
| 2482 | number[0] = '0'; |
| 2483 | number[1] = 'x'; |
| 2484 | len += 2; |
| 2485 | } |
| 2486 | |
| 2487 | if (_PyUnicodeWriter_WriteCstr(writer, number, len) == -1) |
| 2488 | return NULL; |
| 2489 | break; |
| 2490 | } |
| 2491 | |
| 2492 | case 's': |
| 2493 | { |
| 2494 | /* UTF-8 */ |
| 2495 | const char *s = va_arg(*vargs, const char*); |
| 2496 | PyObject *str = PyUnicode_DecodeUTF8Stateful(s, strlen(s), "replace", NULL); |
| 2497 | if (!str) |
| 2498 | return NULL; |
| 2499 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) { |
| 2500 | Py_DECREF(str); |
| 2501 | return NULL; |
| 2502 | } |
| 2503 | Py_DECREF(str); |
| 2504 | break; |
| 2505 | } |
| 2506 | |
| 2507 | case 'U': |
| 2508 | { |
| 2509 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2510 | assert(obj && _PyUnicode_CHECK(obj)); |
| 2511 | |
| 2512 | if (_PyUnicodeWriter_WriteStr(writer, obj) == -1) |
| 2513 | return NULL; |
| 2514 | break; |
| 2515 | } |
| 2516 | |
| 2517 | case 'V': |
| 2518 | { |
| 2519 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2520 | const char *str = va_arg(*vargs, const char *); |
| 2521 | PyObject *str_obj; |
| 2522 | assert(obj || str); |
| 2523 | if (obj) { |
| 2524 | assert(_PyUnicode_CHECK(obj)); |
| 2525 | if (_PyUnicodeWriter_WriteStr(writer, obj) == -1) |
| 2526 | return NULL; |
| 2527 | } |
| 2528 | else { |
| 2529 | str_obj = PyUnicode_DecodeUTF8Stateful(str, strlen(str), "replace", NULL); |
| 2530 | if (!str_obj) |
| 2531 | return NULL; |
| 2532 | if (_PyUnicodeWriter_WriteStr(writer, str_obj) == -1) { |
| 2533 | Py_DECREF(str_obj); |
| 2534 | return NULL; |
| 2535 | } |
| 2536 | Py_DECREF(str_obj); |
| 2537 | } |
| 2538 | break; |
| 2539 | } |
| 2540 | |
| 2541 | case 'S': |
| 2542 | { |
| 2543 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2544 | PyObject *str; |
| 2545 | assert(obj); |
| 2546 | str = PyObject_Str(obj); |
| 2547 | if (!str) |
| 2548 | return NULL; |
| 2549 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) { |
| 2550 | Py_DECREF(str); |
| 2551 | return NULL; |
| 2552 | } |
| 2553 | Py_DECREF(str); |
| 2554 | break; |
| 2555 | } |
| 2556 | |
| 2557 | case 'R': |
| 2558 | { |
| 2559 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2560 | PyObject *repr; |
| 2561 | assert(obj); |
| 2562 | repr = PyObject_Repr(obj); |
| 2563 | if (!repr) |
| 2564 | return NULL; |
| 2565 | if (_PyUnicodeWriter_WriteStr(writer, repr) == -1) { |
| 2566 | Py_DECREF(repr); |
| 2567 | return NULL; |
| 2568 | } |
| 2569 | Py_DECREF(repr); |
| 2570 | break; |
| 2571 | } |
| 2572 | |
| 2573 | case 'A': |
| 2574 | { |
| 2575 | PyObject *obj = va_arg(*vargs, PyObject *); |
| 2576 | PyObject *ascii; |
| 2577 | assert(obj); |
| 2578 | ascii = PyObject_ASCII(obj); |
| 2579 | if (!ascii) |
| 2580 | return NULL; |
| 2581 | if (_PyUnicodeWriter_WriteStr(writer, ascii) == -1) { |
| 2582 | Py_DECREF(ascii); |
| 2583 | return NULL; |
| 2584 | } |
| 2585 | Py_DECREF(ascii); |
| 2586 | break; |
| 2587 | } |
| 2588 | |
| 2589 | case '%': |
| 2590 | if (_PyUnicodeWriter_Prepare(writer, 1, '%') == 1) |
| 2591 | return NULL; |
| 2592 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '%'); |
| 2593 | writer->pos++; |
| 2594 | break; |
| 2595 | |
| 2596 | default: |
| 2597 | /* if we stumble upon an unknown formatting code, copy the rest |
| 2598 | of the format string to the output string. (we cannot just |
| 2599 | skip the code, since there's no way to know what's in the |
| 2600 | argument list) */ |
| 2601 | len = strlen(p); |
| 2602 | if (_PyUnicodeWriter_WriteCstr(writer, p, len) == -1) |
| 2603 | return NULL; |
| 2604 | f = p+len; |
| 2605 | return f; |
| 2606 | } |
| 2607 | |
| 2608 | f++; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2609 | return f; |
| 2610 | } |
| 2611 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2612 | PyObject * |
| 2613 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 2614 | { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2615 | va_list vargs2; |
| 2616 | const char *f; |
| 2617 | _PyUnicodeWriter writer; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2618 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2619 | _PyUnicodeWriter_Init(&writer, strlen(format) + 100); |
| 2620 | |
| 2621 | /* va_list may be an array (of 1 item) on some platforms (ex: AMD64). |
| 2622 | Copy it to be able to pass a reference to a subfunction. */ |
| 2623 | Py_VA_COPY(vargs2, vargs); |
| 2624 | |
| 2625 | for (f = format; *f; ) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2626 | if (*f == '%') { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2627 | f = unicode_fromformat_arg(&writer, f, &vargs2); |
| 2628 | if (f == NULL) |
| 2629 | goto fail; |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2630 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2631 | else { |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2632 | const char *p; |
| 2633 | Py_ssize_t len; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2634 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2635 | p = f; |
| 2636 | do |
| 2637 | { |
| 2638 | if ((unsigned char)*p > 127) { |
| 2639 | PyErr_Format(PyExc_ValueError, |
| 2640 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 2641 | "string, got a non-ASCII byte: 0x%02x", |
| 2642 | (unsigned char)*p); |
| 2643 | return NULL; |
| 2644 | } |
| 2645 | p++; |
| 2646 | } |
| 2647 | while (*p != '\0' && *p != '%'); |
| 2648 | len = p - f; |
| 2649 | |
| 2650 | if (*p == '\0') |
| 2651 | writer.overallocate = 0; |
| 2652 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
| 2653 | goto fail; |
| 2654 | unicode_write_cstr(writer.buffer, writer.pos, f, len); |
| 2655 | writer.pos += len; |
| 2656 | |
| 2657 | f = p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2658 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2659 | } |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 2660 | return _PyUnicodeWriter_Finish(&writer); |
| 2661 | |
| 2662 | fail: |
| 2663 | _PyUnicodeWriter_Dealloc(&writer); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2664 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2667 | PyObject * |
| 2668 | PyUnicode_FromFormat(const char *format, ...) |
| 2669 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2670 | PyObject* ret; |
| 2671 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2672 | |
| 2673 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2674 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2675 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2676 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2677 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2678 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2679 | va_end(vargs); |
| 2680 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2683 | #ifdef HAVE_WCHAR_H |
| 2684 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2685 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2686 | convert a Unicode object to a wide character string. |
| 2687 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2688 | - 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] | 2689 | character) required to convert the unicode object. Ignore size argument. |
| 2690 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2691 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2692 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2693 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2694 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2695 | unicode_aswidechar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2696 | wchar_t *w, |
| 2697 | Py_ssize_t size) |
| 2698 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2699 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2700 | const wchar_t *wstr; |
| 2701 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2702 | wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2703 | if (wstr == NULL) |
| 2704 | return -1; |
| 2705 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2706 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2707 | if (size > res) |
| 2708 | size = res + 1; |
| 2709 | else |
| 2710 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2711 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2712 | return res; |
| 2713 | } |
| 2714 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2715 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2719 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2720 | wchar_t *w, |
| 2721 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2722 | { |
| 2723 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2724 | PyErr_BadInternalCall(); |
| 2725 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2726 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2727 | return unicode_aswidechar(unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2730 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2731 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2732 | Py_ssize_t *size) |
| 2733 | { |
| 2734 | wchar_t* buffer; |
| 2735 | Py_ssize_t buflen; |
| 2736 | |
| 2737 | if (unicode == NULL) { |
| 2738 | PyErr_BadInternalCall(); |
| 2739 | return NULL; |
| 2740 | } |
| 2741 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2742 | buflen = unicode_aswidechar(unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2743 | if (buflen == -1) |
| 2744 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2745 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2746 | PyErr_NoMemory(); |
| 2747 | return NULL; |
| 2748 | } |
| 2749 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2750 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2751 | if (buffer == NULL) { |
| 2752 | PyErr_NoMemory(); |
| 2753 | return NULL; |
| 2754 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 2755 | buflen = unicode_aswidechar(unicode, buffer, buflen); |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2756 | if (buflen == -1) { |
| 2757 | PyMem_FREE(buffer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2758 | return NULL; |
Stefan Krah | 8528c31 | 2012-08-19 21:52:43 +0200 | [diff] [blame] | 2759 | } |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2760 | if (size != NULL) |
| 2761 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2762 | return buffer; |
| 2763 | } |
| 2764 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2765 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2766 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2767 | PyObject * |
| 2768 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2769 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2770 | PyObject *v; |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 2771 | if (ordinal < 0 || ordinal > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2772 | PyErr_SetString(PyExc_ValueError, |
| 2773 | "chr() arg not in range(0x110000)"); |
| 2774 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2775 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2776 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2777 | if (ordinal < 256) |
| 2778 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2779 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2780 | v = PyUnicode_New(1, ordinal); |
| 2781 | if (v == NULL) |
| 2782 | return NULL; |
| 2783 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 2784 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2785 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2788 | PyObject * |
| 2789 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2790 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2791 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2792 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2793 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 2794 | if (PyUnicode_READY(obj) == -1) |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2795 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2796 | Py_INCREF(obj); |
| 2797 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2798 | } |
| 2799 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2800 | /* For a Unicode subtype that's not a Unicode object, |
| 2801 | return a true Unicode object with the same data. */ |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 2802 | return _PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2803 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2804 | PyErr_Format(PyExc_TypeError, |
| 2805 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2806 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2807 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2810 | PyObject * |
| 2811 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2812 | const char *encoding, |
| 2813 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2814 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2815 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2816 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2817 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2818 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2819 | PyErr_BadInternalCall(); |
| 2820 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2821 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2822 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2823 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2824 | if (PyBytes_Check(obj)) { |
| 2825 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2826 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2827 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2828 | } |
| 2829 | else { |
| 2830 | v = PyUnicode_Decode( |
| 2831 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2832 | encoding, errors); |
| 2833 | } |
| 2834 | return v; |
| 2835 | } |
| 2836 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2837 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2838 | PyErr_SetString(PyExc_TypeError, |
| 2839 | "decoding str is not supported"); |
| 2840 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2841 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2842 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2843 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2844 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2845 | PyErr_Format(PyExc_TypeError, |
| 2846 | "coercing to str: need bytes, bytearray " |
| 2847 | "or buffer-like object, %.80s found", |
| 2848 | Py_TYPE(obj)->tp_name); |
| 2849 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2850 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2851 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2852 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2853 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2854 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2855 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2856 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2857 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2858 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2859 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2860 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2863 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2864 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2865 | 1 on success. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2866 | int |
| 2867 | _Py_normalize_encoding(const char *encoding, |
| 2868 | char *lower, |
| 2869 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2870 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2871 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2872 | char *l; |
| 2873 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2874 | |
Benjamin Peterson | 7a6debe | 2011-10-15 09:25:28 -0400 | [diff] [blame] | 2875 | if (encoding == NULL) { |
| 2876 | strcpy(lower, "utf-8"); |
| 2877 | return 1; |
| 2878 | } |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2879 | e = encoding; |
| 2880 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2881 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2882 | while (*e) { |
| 2883 | if (l == l_end) |
| 2884 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2885 | if (Py_ISUPPER(*e)) { |
| 2886 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2887 | } |
| 2888 | else if (*e == '_') { |
| 2889 | *l++ = '-'; |
| 2890 | e++; |
| 2891 | } |
| 2892 | else { |
| 2893 | *l++ = *e++; |
| 2894 | } |
| 2895 | } |
| 2896 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2897 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2900 | PyObject * |
| 2901 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2902 | Py_ssize_t size, |
| 2903 | const char *encoding, |
| 2904 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2905 | { |
| 2906 | PyObject *buffer = NULL, *unicode; |
| 2907 | Py_buffer info; |
| 2908 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2909 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2910 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 2911 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2912 | if ((strcmp(lower, "utf-8") == 0) || |
| 2913 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 2914 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2915 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2916 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2917 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2918 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2919 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2920 | else if (strcmp(lower, "mbcs") == 0) |
| 2921 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2922 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2923 | else if (strcmp(lower, "ascii") == 0) |
| 2924 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2925 | else if (strcmp(lower, "utf-16") == 0) |
| 2926 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2927 | else if (strcmp(lower, "utf-32") == 0) |
| 2928 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2929 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2930 | |
| 2931 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2932 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2933 | 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] | 2934 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2935 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2936 | if (buffer == NULL) |
| 2937 | goto onError; |
| 2938 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2939 | if (unicode == NULL) |
| 2940 | goto onError; |
| 2941 | if (!PyUnicode_Check(unicode)) { |
| 2942 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2943 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2944 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2945 | Py_DECREF(unicode); |
| 2946 | goto onError; |
| 2947 | } |
| 2948 | Py_DECREF(buffer); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2949 | return unicode_result(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2950 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2951 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2952 | Py_XDECREF(buffer); |
| 2953 | return NULL; |
| 2954 | } |
| 2955 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2956 | PyObject * |
| 2957 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2958 | const char *encoding, |
| 2959 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2960 | { |
| 2961 | PyObject *v; |
| 2962 | |
| 2963 | if (!PyUnicode_Check(unicode)) { |
| 2964 | PyErr_BadArgument(); |
| 2965 | goto onError; |
| 2966 | } |
| 2967 | |
| 2968 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2969 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2970 | |
| 2971 | /* Decode via the codec registry */ |
| 2972 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2973 | if (v == NULL) |
| 2974 | goto onError; |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 2975 | return unicode_result(v); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2976 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2977 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2978 | return NULL; |
| 2979 | } |
| 2980 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2981 | PyObject * |
| 2982 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2983 | const char *encoding, |
| 2984 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2985 | { |
| 2986 | PyObject *v; |
| 2987 | |
| 2988 | if (!PyUnicode_Check(unicode)) { |
| 2989 | PyErr_BadArgument(); |
| 2990 | goto onError; |
| 2991 | } |
| 2992 | |
| 2993 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2994 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2995 | |
| 2996 | /* Decode via the codec registry */ |
| 2997 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2998 | if (v == NULL) |
| 2999 | goto onError; |
| 3000 | if (!PyUnicode_Check(v)) { |
| 3001 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3002 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3003 | Py_TYPE(v)->tp_name); |
| 3004 | Py_DECREF(v); |
| 3005 | goto onError; |
| 3006 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 3007 | return unicode_result(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3008 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3009 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3010 | return NULL; |
| 3011 | } |
| 3012 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3013 | PyObject * |
| 3014 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3015 | Py_ssize_t size, |
| 3016 | const char *encoding, |
| 3017 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3018 | { |
| 3019 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3020 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3021 | unicode = PyUnicode_FromUnicode(s, size); |
| 3022 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3023 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3024 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 3025 | Py_DECREF(unicode); |
| 3026 | return v; |
| 3027 | } |
| 3028 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3029 | PyObject * |
| 3030 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3031 | const char *encoding, |
| 3032 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3033 | { |
| 3034 | PyObject *v; |
| 3035 | |
| 3036 | if (!PyUnicode_Check(unicode)) { |
| 3037 | PyErr_BadArgument(); |
| 3038 | goto onError; |
| 3039 | } |
| 3040 | |
| 3041 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3042 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3043 | |
| 3044 | /* Encode via the codec registry */ |
| 3045 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3046 | if (v == NULL) |
| 3047 | goto onError; |
| 3048 | return v; |
| 3049 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3050 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3051 | return NULL; |
| 3052 | } |
| 3053 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3054 | static size_t |
| 3055 | wcstombs_errorpos(const wchar_t *wstr) |
| 3056 | { |
| 3057 | size_t len; |
| 3058 | #if SIZEOF_WCHAR_T == 2 |
| 3059 | wchar_t buf[3]; |
| 3060 | #else |
| 3061 | wchar_t buf[2]; |
| 3062 | #endif |
| 3063 | char outbuf[MB_LEN_MAX]; |
| 3064 | const wchar_t *start, *previous; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3065 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3066 | #if SIZEOF_WCHAR_T == 2 |
| 3067 | buf[2] = 0; |
| 3068 | #else |
| 3069 | buf[1] = 0; |
| 3070 | #endif |
| 3071 | start = wstr; |
| 3072 | while (*wstr != L'\0') |
| 3073 | { |
| 3074 | previous = wstr; |
| 3075 | #if SIZEOF_WCHAR_T == 2 |
| 3076 | if (Py_UNICODE_IS_HIGH_SURROGATE(wstr[0]) |
| 3077 | && Py_UNICODE_IS_LOW_SURROGATE(wstr[1])) |
| 3078 | { |
| 3079 | buf[0] = wstr[0]; |
| 3080 | buf[1] = wstr[1]; |
| 3081 | wstr += 2; |
| 3082 | } |
| 3083 | else { |
| 3084 | buf[0] = *wstr; |
| 3085 | buf[1] = 0; |
| 3086 | wstr++; |
| 3087 | } |
| 3088 | #else |
| 3089 | buf[0] = *wstr; |
| 3090 | wstr++; |
| 3091 | #endif |
| 3092 | len = wcstombs(outbuf, buf, sizeof(outbuf)); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3093 | if (len == (size_t)-1) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3094 | return previous - start; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | /* failed to find the unencodable character */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3098 | return 0; |
| 3099 | } |
| 3100 | |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3101 | static int |
| 3102 | locale_error_handler(const char *errors, int *surrogateescape) |
| 3103 | { |
| 3104 | if (errors == NULL) { |
| 3105 | *surrogateescape = 0; |
| 3106 | return 0; |
| 3107 | } |
| 3108 | |
| 3109 | if (strcmp(errors, "strict") == 0) { |
| 3110 | *surrogateescape = 0; |
| 3111 | return 0; |
| 3112 | } |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 3113 | if (strcmp(errors, "surrogateescape") == 0) { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3114 | *surrogateescape = 1; |
| 3115 | return 0; |
| 3116 | } |
| 3117 | PyErr_Format(PyExc_ValueError, |
| 3118 | "only 'strict' and 'surrogateescape' error handlers " |
| 3119 | "are supported, not '%s'", |
| 3120 | errors); |
| 3121 | return -1; |
| 3122 | } |
| 3123 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3124 | PyObject * |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3125 | PyUnicode_EncodeLocale(PyObject *unicode, const char *errors) |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3126 | { |
| 3127 | Py_ssize_t wlen, wlen2; |
| 3128 | wchar_t *wstr; |
| 3129 | PyObject *bytes = NULL; |
| 3130 | char *errmsg; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3131 | PyObject *reason; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3132 | PyObject *exc; |
| 3133 | size_t error_pos; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3134 | int surrogateescape; |
| 3135 | |
| 3136 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3137 | return NULL; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3138 | |
| 3139 | wstr = PyUnicode_AsWideCharString(unicode, &wlen); |
| 3140 | if (wstr == NULL) |
| 3141 | return NULL; |
| 3142 | |
| 3143 | wlen2 = wcslen(wstr); |
| 3144 | if (wlen2 != wlen) { |
| 3145 | PyMem_Free(wstr); |
| 3146 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3147 | return NULL; |
| 3148 | } |
| 3149 | |
| 3150 | if (surrogateescape) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3151 | /* "surrogateescape" error handler */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3152 | char *str; |
| 3153 | |
| 3154 | str = _Py_wchar2char(wstr, &error_pos); |
| 3155 | if (str == NULL) { |
| 3156 | if (error_pos == (size_t)-1) { |
| 3157 | PyErr_NoMemory(); |
| 3158 | PyMem_Free(wstr); |
| 3159 | return NULL; |
| 3160 | } |
| 3161 | else { |
| 3162 | goto encode_error; |
| 3163 | } |
| 3164 | } |
| 3165 | PyMem_Free(wstr); |
| 3166 | |
| 3167 | bytes = PyBytes_FromString(str); |
| 3168 | PyMem_Free(str); |
| 3169 | } |
| 3170 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3171 | /* strict mode */ |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3172 | size_t len, len2; |
| 3173 | |
| 3174 | len = wcstombs(NULL, wstr, 0); |
| 3175 | if (len == (size_t)-1) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3176 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3177 | goto encode_error; |
| 3178 | } |
| 3179 | |
| 3180 | bytes = PyBytes_FromStringAndSize(NULL, len); |
| 3181 | if (bytes == NULL) { |
| 3182 | PyMem_Free(wstr); |
| 3183 | return NULL; |
| 3184 | } |
| 3185 | |
| 3186 | len2 = wcstombs(PyBytes_AS_STRING(bytes), wstr, len+1); |
| 3187 | if (len2 == (size_t)-1 || len2 > len) { |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3188 | error_pos = (size_t)-1; |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3189 | goto encode_error; |
| 3190 | } |
| 3191 | PyMem_Free(wstr); |
| 3192 | } |
| 3193 | return bytes; |
| 3194 | |
| 3195 | encode_error: |
| 3196 | errmsg = strerror(errno); |
| 3197 | assert(errmsg != NULL); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3198 | |
| 3199 | if (error_pos == (size_t)-1) |
| 3200 | error_pos = wcstombs_errorpos(wstr); |
| 3201 | |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3202 | PyMem_Free(wstr); |
| 3203 | Py_XDECREF(bytes); |
| 3204 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3205 | if (errmsg != NULL) { |
| 3206 | size_t errlen; |
| 3207 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3208 | if (wstr != NULL) { |
| 3209 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3210 | PyMem_Free(wstr); |
| 3211 | } else |
| 3212 | errmsg = NULL; |
| 3213 | } |
| 3214 | if (errmsg == NULL) |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 3215 | reason = PyUnicode_FromString( |
| 3216 | "wcstombs() encountered an unencodable " |
| 3217 | "wide character"); |
| 3218 | if (reason == NULL) |
| 3219 | return NULL; |
| 3220 | |
| 3221 | exc = PyObject_CallFunction(PyExc_UnicodeEncodeError, "sOnnO", |
| 3222 | "locale", unicode, |
| 3223 | (Py_ssize_t)error_pos, |
| 3224 | (Py_ssize_t)(error_pos+1), |
| 3225 | reason); |
| 3226 | Py_DECREF(reason); |
| 3227 | if (exc != NULL) { |
| 3228 | PyCodec_StrictErrors(exc); |
| 3229 | Py_XDECREF(exc); |
| 3230 | } |
Victor Stinner | f2ea71f | 2011-12-17 04:13:41 +0100 | [diff] [blame] | 3231 | return NULL; |
| 3232 | } |
| 3233 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3234 | PyObject * |
| 3235 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3236 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3237 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3238 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3239 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3240 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3241 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3242 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3243 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3244 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3245 | the Python codec requires to encode at least its own filename. Use the C |
| 3246 | version of the locale codec until the codec registry is initialized and |
| 3247 | the Python codec is loaded. |
| 3248 | |
| 3249 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3250 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3251 | subinterpreters. */ |
| 3252 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3253 | return PyUnicode_AsEncodedString(unicode, |
| 3254 | Py_FileSystemDefaultEncoding, |
| 3255 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3256 | } |
| 3257 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3258 | return PyUnicode_EncodeLocale(unicode, "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 3259 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3260 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3263 | PyObject * |
| 3264 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3265 | const char *encoding, |
| 3266 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3267 | { |
| 3268 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 3269 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3270 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3271 | if (!PyUnicode_Check(unicode)) { |
| 3272 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3273 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3274 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3275 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3276 | /* Shortcuts for common default encodings */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3277 | if (_Py_normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3278 | if ((strcmp(lower, "utf-8") == 0) || |
| 3279 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3280 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3281 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3282 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 3283 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3284 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 3285 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3286 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 3287 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3288 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3289 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3290 | #ifdef HAVE_MBCS |
Victor Stinner | ac931b1 | 2011-11-20 18:27:03 +0100 | [diff] [blame] | 3291 | else if (strcmp(lower, "mbcs") == 0) |
| 3292 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3293 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3294 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3295 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 3296 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3297 | |
| 3298 | /* Encode via the codec registry */ |
| 3299 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3300 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3301 | return NULL; |
| 3302 | |
| 3303 | /* The normal path */ |
| 3304 | if (PyBytes_Check(v)) |
| 3305 | return v; |
| 3306 | |
| 3307 | /* 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] | 3308 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3309 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3310 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3311 | |
| 3312 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 3313 | "encoder %s returned bytearray instead of bytes", |
| 3314 | encoding); |
| 3315 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3316 | Py_DECREF(v); |
| 3317 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3318 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3319 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 3320 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 3321 | Py_DECREF(v); |
| 3322 | return b; |
| 3323 | } |
| 3324 | |
| 3325 | PyErr_Format(PyExc_TypeError, |
| 3326 | "encoder did not return a bytes object (type=%.400s)", |
| 3327 | Py_TYPE(v)->tp_name); |
| 3328 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3329 | return NULL; |
| 3330 | } |
| 3331 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3332 | PyObject * |
| 3333 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3334 | const char *encoding, |
| 3335 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3336 | { |
| 3337 | PyObject *v; |
| 3338 | |
| 3339 | if (!PyUnicode_Check(unicode)) { |
| 3340 | PyErr_BadArgument(); |
| 3341 | goto onError; |
| 3342 | } |
| 3343 | |
| 3344 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3345 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3346 | |
| 3347 | /* Encode via the codec registry */ |
| 3348 | v = PyCodec_Encode(unicode, encoding, errors); |
| 3349 | if (v == NULL) |
| 3350 | goto onError; |
| 3351 | if (!PyUnicode_Check(v)) { |
| 3352 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3353 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 3354 | Py_TYPE(v)->tp_name); |
| 3355 | Py_DECREF(v); |
| 3356 | goto onError; |
| 3357 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3358 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3359 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3360 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3361 | return NULL; |
| 3362 | } |
| 3363 | |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3364 | static size_t |
| 3365 | mbstowcs_errorpos(const char *str, size_t len) |
| 3366 | { |
| 3367 | #ifdef HAVE_MBRTOWC |
| 3368 | const char *start = str; |
| 3369 | mbstate_t mbs; |
| 3370 | size_t converted; |
| 3371 | wchar_t ch; |
| 3372 | |
| 3373 | memset(&mbs, 0, sizeof mbs); |
| 3374 | while (len) |
| 3375 | { |
| 3376 | converted = mbrtowc(&ch, (char*)str, len, &mbs); |
| 3377 | if (converted == 0) |
| 3378 | /* Reached end of string */ |
| 3379 | break; |
| 3380 | if (converted == (size_t)-1 || converted == (size_t)-2) { |
| 3381 | /* Conversion error or incomplete character */ |
| 3382 | return str - start; |
| 3383 | } |
| 3384 | else { |
| 3385 | str += converted; |
| 3386 | len -= converted; |
| 3387 | } |
| 3388 | } |
| 3389 | /* failed to find the undecodable byte sequence */ |
| 3390 | return 0; |
| 3391 | #endif |
| 3392 | return 0; |
| 3393 | } |
| 3394 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3395 | PyObject* |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3396 | PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3397 | const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3398 | { |
| 3399 | wchar_t smallbuf[256]; |
| 3400 | size_t smallbuf_len = Py_ARRAY_LENGTH(smallbuf); |
| 3401 | wchar_t *wstr; |
| 3402 | size_t wlen, wlen2; |
| 3403 | PyObject *unicode; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3404 | int surrogateescape; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3405 | size_t error_pos; |
| 3406 | char *errmsg; |
| 3407 | PyObject *reason, *exc; |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3408 | |
| 3409 | if (locale_error_handler(errors, &surrogateescape) < 0) |
| 3410 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3411 | |
| 3412 | if (str[len] != '\0' || len != strlen(str)) { |
| 3413 | PyErr_SetString(PyExc_TypeError, "embedded null character"); |
| 3414 | return NULL; |
| 3415 | } |
| 3416 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3417 | if (surrogateescape) { |
| 3418 | /* "surrogateescape" error handler */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3419 | wstr = _Py_char2wchar(str, &wlen); |
| 3420 | if (wstr == NULL) { |
| 3421 | if (wlen == (size_t)-1) |
| 3422 | PyErr_NoMemory(); |
| 3423 | else |
| 3424 | PyErr_SetFromErrno(PyExc_OSError); |
| 3425 | return NULL; |
| 3426 | } |
| 3427 | |
| 3428 | unicode = PyUnicode_FromWideChar(wstr, wlen); |
| 3429 | PyMem_Free(wstr); |
| 3430 | } |
| 3431 | else { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 3432 | /* strict mode */ |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3433 | #ifndef HAVE_BROKEN_MBSTOWCS |
| 3434 | wlen = mbstowcs(NULL, str, 0); |
| 3435 | #else |
| 3436 | wlen = len; |
| 3437 | #endif |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3438 | if (wlen == (size_t)-1) |
| 3439 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3440 | if (wlen+1 <= smallbuf_len) { |
| 3441 | wstr = smallbuf; |
| 3442 | } |
| 3443 | else { |
| 3444 | if (wlen > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) |
| 3445 | return PyErr_NoMemory(); |
| 3446 | |
| 3447 | wstr = PyMem_Malloc((wlen+1) * sizeof(wchar_t)); |
| 3448 | if (!wstr) |
| 3449 | return PyErr_NoMemory(); |
| 3450 | } |
| 3451 | |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3452 | wlen2 = mbstowcs(wstr, str, wlen+1); |
| 3453 | if (wlen2 == (size_t)-1) { |
| 3454 | if (wstr != smallbuf) |
| 3455 | PyMem_Free(wstr); |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3456 | goto decode_error; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3457 | } |
| 3458 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 3459 | assert(wlen2 == wlen); |
| 3460 | #endif |
| 3461 | unicode = PyUnicode_FromWideChar(wstr, wlen2); |
| 3462 | if (wstr != smallbuf) |
| 3463 | PyMem_Free(wstr); |
| 3464 | } |
| 3465 | return unicode; |
Victor Stinner | 2f19707 | 2011-12-17 07:08:30 +0100 | [diff] [blame] | 3466 | |
| 3467 | decode_error: |
| 3468 | errmsg = strerror(errno); |
| 3469 | assert(errmsg != NULL); |
| 3470 | |
| 3471 | error_pos = mbstowcs_errorpos(str, len); |
| 3472 | if (errmsg != NULL) { |
| 3473 | size_t errlen; |
| 3474 | wstr = _Py_char2wchar(errmsg, &errlen); |
| 3475 | if (wstr != NULL) { |
| 3476 | reason = PyUnicode_FromWideChar(wstr, errlen); |
| 3477 | PyMem_Free(wstr); |
| 3478 | } else |
| 3479 | errmsg = NULL; |
| 3480 | } |
| 3481 | if (errmsg == NULL) |
| 3482 | reason = PyUnicode_FromString( |
| 3483 | "mbstowcs() encountered an invalid multibyte sequence"); |
| 3484 | if (reason == NULL) |
| 3485 | return NULL; |
| 3486 | |
| 3487 | exc = PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nnO", |
| 3488 | "locale", str, len, |
| 3489 | (Py_ssize_t)error_pos, |
| 3490 | (Py_ssize_t)(error_pos+1), |
| 3491 | reason); |
| 3492 | Py_DECREF(reason); |
| 3493 | if (exc != NULL) { |
| 3494 | PyCodec_StrictErrors(exc); |
| 3495 | Py_XDECREF(exc); |
| 3496 | } |
| 3497 | return NULL; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | PyObject* |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3501 | PyUnicode_DecodeLocale(const char *str, const char *errors) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3502 | { |
| 3503 | Py_ssize_t size = (Py_ssize_t)strlen(str); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3504 | return PyUnicode_DecodeLocaleAndSize(str, size, errors); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | |
| 3508 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3509 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3510 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3511 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 3512 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3513 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 3514 | PyObject* |
| 3515 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 3516 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 3517 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3518 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 3519 | #elif defined(__APPLE__) |
Victor Stinner | a1d12bb | 2011-12-11 21:53:09 +0100 | [diff] [blame] | 3520 | return PyUnicode_DecodeUTF8Stateful(s, size, "surrogateescape", NULL); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3521 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 3522 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 3523 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 3524 | cannot use it to encode and decode filenames before it is loaded. Load |
| 3525 | the Python codec requires to encode at least its own filename. Use the C |
| 3526 | version of the locale codec until the codec registry is initialized and |
| 3527 | the Python codec is loaded. |
| 3528 | |
| 3529 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 3530 | cannot only rely on it: check also interp->fscodec_initialized for |
| 3531 | subinterpreters. */ |
| 3532 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3533 | return PyUnicode_Decode(s, size, |
| 3534 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 3535 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3536 | } |
| 3537 | else { |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 3538 | return PyUnicode_DecodeLocaleAndSize(s, size, "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3539 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 3540 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3543 | |
| 3544 | int |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3545 | _PyUnicode_HasNULChars(PyObject* str) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3546 | { |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3547 | Py_ssize_t pos; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3548 | |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3549 | if (PyUnicode_READY(str) == -1) |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3550 | return -1; |
Victor Stinner | fe75fb4 | 2012-10-23 02:52:18 +0200 | [diff] [blame] | 3551 | pos = findchar(PyUnicode_DATA(str), PyUnicode_KIND(str), |
| 3552 | PyUnicode_GET_LENGTH(str), '\0', 1); |
| 3553 | if (pos == -1) |
| 3554 | return 0; |
| 3555 | else |
| 3556 | return 1; |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3557 | } |
| 3558 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 3559 | int |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3560 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 3561 | { |
| 3562 | PyObject *output = NULL; |
| 3563 | Py_ssize_t size; |
| 3564 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3565 | if (arg == NULL) { |
| 3566 | Py_DECREF(*(PyObject**)addr); |
| 3567 | return 1; |
| 3568 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 3569 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3570 | output = arg; |
| 3571 | Py_INCREF(output); |
| 3572 | } |
| 3573 | else { |
| 3574 | arg = PyUnicode_FromObject(arg); |
| 3575 | if (!arg) |
| 3576 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3577 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3578 | Py_DECREF(arg); |
| 3579 | if (!output) |
| 3580 | return 0; |
| 3581 | if (!PyBytes_Check(output)) { |
| 3582 | Py_DECREF(output); |
| 3583 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 3584 | return 0; |
| 3585 | } |
| 3586 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 3587 | size = PyBytes_GET_SIZE(output); |
| 3588 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3589 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 3590 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3591 | Py_DECREF(output); |
| 3592 | return 0; |
| 3593 | } |
| 3594 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3595 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3599 | int |
| 3600 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 3601 | { |
| 3602 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3603 | if (arg == NULL) { |
| 3604 | Py_DECREF(*(PyObject**)addr); |
| 3605 | return 1; |
| 3606 | } |
| 3607 | if (PyUnicode_Check(arg)) { |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3608 | if (PyUnicode_READY(arg) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3609 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3610 | output = arg; |
| 3611 | Py_INCREF(output); |
| 3612 | } |
| 3613 | else { |
| 3614 | arg = PyBytes_FromObject(arg); |
| 3615 | if (!arg) |
| 3616 | return 0; |
| 3617 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3618 | PyBytes_GET_SIZE(arg)); |
| 3619 | Py_DECREF(arg); |
| 3620 | if (!output) |
| 3621 | return 0; |
| 3622 | if (!PyUnicode_Check(output)) { |
| 3623 | Py_DECREF(output); |
| 3624 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3625 | return 0; |
| 3626 | } |
| 3627 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 3628 | if (PyUnicode_READY(output) == -1) { |
Victor Stinner | 065836e | 2011-10-27 01:56:33 +0200 | [diff] [blame] | 3629 | Py_DECREF(output); |
| 3630 | return 0; |
| 3631 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3632 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 3633 | PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3634 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3635 | Py_DECREF(output); |
| 3636 | return 0; |
| 3637 | } |
| 3638 | *(PyObject**)addr = output; |
| 3639 | return Py_CLEANUP_SUPPORTED; |
| 3640 | } |
| 3641 | |
| 3642 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3643 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3644 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3645 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3646 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3647 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3648 | if (!PyUnicode_Check(unicode)) { |
| 3649 | PyErr_BadArgument(); |
| 3650 | return NULL; |
| 3651 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3652 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3653 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3654 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3655 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3656 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3657 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3658 | if (bytes == NULL) |
| 3659 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3660 | _PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3661 | if (_PyUnicode_UTF8(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3662 | Py_DECREF(bytes); |
| 3663 | return NULL; |
| 3664 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3665 | _PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); |
| 3666 | Py_MEMCPY(_PyUnicode_UTF8(unicode), |
| 3667 | PyBytes_AS_STRING(bytes), |
| 3668 | _PyUnicode_UTF8_LENGTH(unicode) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3669 | Py_DECREF(bytes); |
| 3670 | } |
| 3671 | |
| 3672 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3673 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3674 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3678 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3679 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3680 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3681 | } |
| 3682 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3683 | Py_UNICODE * |
| 3684 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3685 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3686 | const unsigned char *one_byte; |
| 3687 | #if SIZEOF_WCHAR_T == 4 |
| 3688 | const Py_UCS2 *two_bytes; |
| 3689 | #else |
| 3690 | const Py_UCS4 *four_bytes; |
| 3691 | const Py_UCS4 *ucs4_end; |
| 3692 | Py_ssize_t num_surrogates; |
| 3693 | #endif |
| 3694 | wchar_t *w; |
| 3695 | wchar_t *wchar_end; |
| 3696 | |
| 3697 | if (!PyUnicode_Check(unicode)) { |
| 3698 | PyErr_BadArgument(); |
| 3699 | return NULL; |
| 3700 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3701 | if (_PyUnicode_WSTR(unicode) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3702 | /* Non-ASCII compact unicode object */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3703 | assert(_PyUnicode_KIND(unicode) != 0); |
| 3704 | assert(PyUnicode_IS_READY(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3705 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3706 | if (PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3707 | #if SIZEOF_WCHAR_T == 2 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3708 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
| 3709 | ucs4_end = four_bytes + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3710 | num_surrogates = 0; |
| 3711 | |
| 3712 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3713 | if (*four_bytes > 0xFFFF) |
| 3714 | ++num_surrogates; |
| 3715 | } |
| 3716 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3717 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC( |
| 3718 | sizeof(wchar_t) * (_PyUnicode_LENGTH(unicode) + 1 + num_surrogates)); |
| 3719 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3720 | PyErr_NoMemory(); |
| 3721 | return NULL; |
| 3722 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3723 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode) + num_surrogates; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3724 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3725 | w = _PyUnicode_WSTR(unicode); |
| 3726 | wchar_end = w + _PyUnicode_WSTR_LENGTH(unicode); |
| 3727 | four_bytes = PyUnicode_4BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3728 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3729 | if (*four_bytes > 0xFFFF) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 3730 | assert(*four_bytes <= MAX_UNICODE); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3731 | /* encode surrogate pair in this case */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 3732 | *w++ = Py_UNICODE_HIGH_SURROGATE(*four_bytes); |
| 3733 | *w = Py_UNICODE_LOW_SURROGATE(*four_bytes); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3734 | } |
| 3735 | else |
| 3736 | *w = *four_bytes; |
| 3737 | |
| 3738 | if (w > wchar_end) { |
| 3739 | assert(0 && "Miscalculated string end"); |
| 3740 | } |
| 3741 | } |
| 3742 | *w = 0; |
| 3743 | #else |
| 3744 | /* sizeof(wchar_t) == 4 */ |
| 3745 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3746 | "should share memory already."); |
| 3747 | return NULL; |
| 3748 | #endif |
| 3749 | } |
| 3750 | else { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3751 | _PyUnicode_WSTR(unicode) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3752 | (_PyUnicode_LENGTH(unicode) + 1)); |
| 3753 | if (!_PyUnicode_WSTR(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3754 | PyErr_NoMemory(); |
| 3755 | return NULL; |
| 3756 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3757 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 3758 | _PyUnicode_WSTR_LENGTH(unicode) = _PyUnicode_LENGTH(unicode); |
| 3759 | w = _PyUnicode_WSTR(unicode); |
| 3760 | wchar_end = w + _PyUnicode_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3761 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3762 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) { |
| 3763 | one_byte = PyUnicode_1BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3764 | for (; w < wchar_end; ++one_byte, ++w) |
| 3765 | *w = *one_byte; |
| 3766 | /* null-terminate the wstr */ |
| 3767 | *w = 0; |
| 3768 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3769 | else if (PyUnicode_KIND(unicode) == PyUnicode_2BYTE_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3770 | #if SIZEOF_WCHAR_T == 4 |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3771 | two_bytes = PyUnicode_2BYTE_DATA(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3772 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3773 | *w = *two_bytes; |
| 3774 | /* null-terminate the wstr */ |
| 3775 | *w = 0; |
| 3776 | #else |
| 3777 | /* sizeof(wchar_t) == 2 */ |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3778 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 3779 | _PyUnicode_WSTR(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3780 | Py_FatalError("Impossible unicode object state, wstr " |
| 3781 | "and str should share memory already."); |
| 3782 | return NULL; |
| 3783 | #endif |
| 3784 | } |
| 3785 | else { |
| 3786 | assert(0 && "This should never happen."); |
| 3787 | } |
| 3788 | } |
| 3789 | } |
| 3790 | if (size != NULL) |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 3791 | *size = PyUnicode_WSTR_LENGTH(unicode); |
| 3792 | return _PyUnicode_WSTR(unicode); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3795 | Py_UNICODE * |
| 3796 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3797 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3798 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3801 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3802 | Py_ssize_t |
| 3803 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3804 | { |
| 3805 | if (!PyUnicode_Check(unicode)) { |
| 3806 | PyErr_BadArgument(); |
| 3807 | goto onError; |
| 3808 | } |
| 3809 | return PyUnicode_GET_SIZE(unicode); |
| 3810 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3811 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3812 | return -1; |
| 3813 | } |
| 3814 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3815 | Py_ssize_t |
| 3816 | PyUnicode_GetLength(PyObject *unicode) |
| 3817 | { |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3818 | if (!PyUnicode_Check(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3819 | PyErr_BadArgument(); |
| 3820 | return -1; |
| 3821 | } |
Victor Stinner | 0762133 | 2012-06-16 04:53:46 +0200 | [diff] [blame] | 3822 | if (PyUnicode_READY(unicode) == -1) |
| 3823 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3824 | return PyUnicode_GET_LENGTH(unicode); |
| 3825 | } |
| 3826 | |
| 3827 | Py_UCS4 |
| 3828 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3829 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3830 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3831 | PyErr_BadArgument(); |
| 3832 | return (Py_UCS4)-1; |
| 3833 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3834 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3835 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3836 | return (Py_UCS4)-1; |
| 3837 | } |
| 3838 | return PyUnicode_READ_CHAR(unicode, index); |
| 3839 | } |
| 3840 | |
| 3841 | int |
| 3842 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3843 | { |
| 3844 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3845 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3846 | return -1; |
| 3847 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3848 | assert(PyUnicode_IS_READY(unicode)); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 3849 | if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3850 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3851 | return -1; |
| 3852 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 3853 | if (unicode_check_modifiable(unicode)) |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3854 | return -1; |
Victor Stinner | c9590ad | 2012-03-04 01:34:37 +0100 | [diff] [blame] | 3855 | if (ch > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 3856 | PyErr_SetString(PyExc_ValueError, "character out of range"); |
| 3857 | return -1; |
| 3858 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3859 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3860 | index, ch); |
| 3861 | return 0; |
| 3862 | } |
| 3863 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3864 | const char * |
| 3865 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3866 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3867 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3870 | /* create or adjust a UnicodeDecodeError */ |
| 3871 | static void |
| 3872 | make_decode_exception(PyObject **exceptionObject, |
| 3873 | const char *encoding, |
| 3874 | const char *input, Py_ssize_t length, |
| 3875 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3876 | const char *reason) |
| 3877 | { |
| 3878 | if (*exceptionObject == NULL) { |
| 3879 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3880 | encoding, input, length, startpos, endpos, reason); |
| 3881 | } |
| 3882 | else { |
| 3883 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3884 | goto onError; |
| 3885 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3886 | goto onError; |
| 3887 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3888 | goto onError; |
| 3889 | } |
| 3890 | return; |
| 3891 | |
| 3892 | onError: |
| 3893 | Py_DECREF(*exceptionObject); |
| 3894 | *exceptionObject = NULL; |
| 3895 | } |
| 3896 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3897 | #ifdef HAVE_MBCS |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3898 | /* error handling callback helper: |
| 3899 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3900 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3901 | and adjust various state variables. |
| 3902 | return 0 on success, -1 on error |
| 3903 | */ |
| 3904 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3905 | static int |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3906 | unicode_decode_call_errorhandler_wchar( |
| 3907 | const char *errors, PyObject **errorHandler, |
| 3908 | const char *encoding, const char *reason, |
| 3909 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3910 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3911 | PyObject **output, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3912 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3913 | 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] | 3914 | |
| 3915 | PyObject *restuple = NULL; |
| 3916 | PyObject *repunicode = NULL; |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3917 | Py_ssize_t outsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3918 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3919 | Py_ssize_t requiredsize; |
| 3920 | Py_ssize_t newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3921 | PyObject *inputobj = NULL; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3922 | wchar_t *repwstr; |
| 3923 | Py_ssize_t repwlen; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3924 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3925 | assert (_PyUnicode_KIND(*output) == PyUnicode_WCHAR_KIND); |
| 3926 | outsize = _PyUnicode_WSTR_LENGTH(*output); |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 3927 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3928 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3929 | *errorHandler = PyCodec_LookupError(errors); |
| 3930 | if (*errorHandler == NULL) |
| 3931 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3934 | make_decode_exception(exceptionObject, |
| 3935 | encoding, |
| 3936 | *input, *inend - *input, |
| 3937 | *startinpos, *endinpos, |
| 3938 | reason); |
| 3939 | if (*exceptionObject == NULL) |
| 3940 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3941 | |
| 3942 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3943 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3944 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3945 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3946 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
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 | } |
| 3949 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3950 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 3951 | |
| 3952 | /* Copy back the bytes variables, which might have been modified by the |
| 3953 | callback */ |
| 3954 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3955 | if (!inputobj) |
| 3956 | goto onError; |
| 3957 | if (!PyBytes_Check(inputobj)) { |
| 3958 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 3959 | } |
| 3960 | *input = PyBytes_AS_STRING(inputobj); |
| 3961 | insize = PyBytes_GET_SIZE(inputobj); |
| 3962 | *inend = *input + insize; |
| 3963 | /* we can DECREF safely, as the exception has another reference, |
| 3964 | so the object won't go away. */ |
| 3965 | Py_DECREF(inputobj); |
| 3966 | |
| 3967 | if (newpos<0) |
| 3968 | newpos = insize+newpos; |
| 3969 | if (newpos<0 || newpos>insize) { |
| 3970 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3971 | goto onError; |
| 3972 | } |
| 3973 | |
| 3974 | repwstr = PyUnicode_AsUnicodeAndSize(repunicode, &repwlen); |
| 3975 | if (repwstr == NULL) |
| 3976 | goto onError; |
| 3977 | /* need more space? (at least enough for what we |
| 3978 | have+the replacement+the rest of the string (starting |
| 3979 | at the new input position), so we won't have to check space |
| 3980 | when there are no errors in the rest of the string) */ |
| 3981 | requiredsize = *outpos + repwlen + insize-newpos; |
| 3982 | if (requiredsize > outsize) { |
| 3983 | if (requiredsize < 2*outsize) |
| 3984 | requiredsize = 2*outsize; |
| 3985 | if (unicode_resize(output, requiredsize) < 0) |
| 3986 | goto onError; |
| 3987 | } |
| 3988 | wcsncpy(_PyUnicode_WSTR(*output) + *outpos, repwstr, repwlen); |
| 3989 | *outpos += repwlen; |
| 3990 | |
| 3991 | *endinpos = newpos; |
| 3992 | *inptr = *input + newpos; |
| 3993 | |
| 3994 | /* we made it! */ |
| 3995 | Py_XDECREF(restuple); |
| 3996 | return 0; |
| 3997 | |
| 3998 | onError: |
| 3999 | Py_XDECREF(restuple); |
| 4000 | return -1; |
| 4001 | } |
| 4002 | #endif /* HAVE_MBCS */ |
| 4003 | |
| 4004 | static int |
| 4005 | unicode_decode_call_errorhandler_writer( |
| 4006 | const char *errors, PyObject **errorHandler, |
| 4007 | const char *encoding, const char *reason, |
| 4008 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 4009 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 4010 | _PyUnicodeWriter *writer /* PyObject **output, Py_ssize_t *outpos */) |
| 4011 | { |
| 4012 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
| 4013 | |
| 4014 | PyObject *restuple = NULL; |
| 4015 | PyObject *repunicode = NULL; |
| 4016 | Py_ssize_t insize; |
| 4017 | Py_ssize_t newpos; |
| 4018 | PyObject *inputobj = NULL; |
| 4019 | |
| 4020 | if (*errorHandler == NULL) { |
| 4021 | *errorHandler = PyCodec_LookupError(errors); |
| 4022 | if (*errorHandler == NULL) |
| 4023 | goto onError; |
| 4024 | } |
| 4025 | |
| 4026 | make_decode_exception(exceptionObject, |
| 4027 | encoding, |
| 4028 | *input, *inend - *input, |
| 4029 | *startinpos, *endinpos, |
| 4030 | reason); |
| 4031 | if (*exceptionObject == NULL) |
| 4032 | goto onError; |
| 4033 | |
| 4034 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 4035 | if (restuple == NULL) |
| 4036 | goto onError; |
| 4037 | if (!PyTuple_Check(restuple)) { |
| 4038 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
| 4039 | goto onError; |
| 4040 | } |
| 4041 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4042 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4043 | |
| 4044 | /* Copy back the bytes variables, which might have been modified by the |
| 4045 | callback */ |
| 4046 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 4047 | if (!inputobj) |
| 4048 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4049 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4050 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4051 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4052 | *input = PyBytes_AS_STRING(inputobj); |
| 4053 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4054 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 4055 | /* we can DECREF safely, as the exception has another reference, |
| 4056 | so the object won't go away. */ |
| 4057 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4058 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4059 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4060 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4061 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4062 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 4063 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4064 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4065 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4066 | writer->overallocate = 1; |
| 4067 | if (_PyUnicodeWriter_WriteStr(writer, repunicode) == -1) |
| 4068 | return |
| 4069 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4070 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4071 | *inptr = *input + newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4072 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4073 | /* we made it! */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4074 | Py_XDECREF(restuple); |
| 4075 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4076 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4077 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4078 | Py_XDECREF(restuple); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4079 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4082 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 4083 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4084 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 4085 | |
| 4086 | /* Three simple macros defining base-64. */ |
| 4087 | |
| 4088 | /* Is c a base-64 character? */ |
| 4089 | |
| 4090 | #define IS_BASE64(c) \ |
| 4091 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 4092 | ((c) >= 'a' && (c) <= 'z') || \ |
| 4093 | ((c) >= '0' && (c) <= '9') || \ |
| 4094 | (c) == '+' || (c) == '/') |
| 4095 | |
| 4096 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 4097 | |
| 4098 | #define FROM_BASE64(c) \ |
| 4099 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 4100 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 4101 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 4102 | (c) == '+' ? 62 : 63) |
| 4103 | |
| 4104 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 4105 | |
| 4106 | #define TO_BASE64(n) \ |
| 4107 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 4108 | |
| 4109 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 4110 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 4111 | * byte not decoding to itself is the + which begins a base64 |
| 4112 | * string. */ |
| 4113 | |
| 4114 | #define DECODE_DIRECT(c) \ |
| 4115 | ((c) <= 127 && (c) != '+') |
| 4116 | |
| 4117 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 4118 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 4119 | * the above). See RFC2152. This array identifies these different |
| 4120 | * sets: |
| 4121 | * 0 : "Set D" |
| 4122 | * alphanumeric and '(),-./:? |
| 4123 | * 1 : "Set O" |
| 4124 | * !"#$%&*;<=>@[]^_`{|} |
| 4125 | * 2 : "whitespace" |
| 4126 | * ht nl cr sp |
| 4127 | * 3 : special (must be base64 encoded) |
| 4128 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 4129 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4130 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4131 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4132 | char utf7_category[128] = { |
| 4133 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 4134 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 4135 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 4136 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 4137 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 4138 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 4139 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 4140 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 4141 | /* @ A B C D E F G H I J K L M N O */ |
| 4142 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4143 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 4144 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 4145 | /* ` a b c d e f g h i j k l m n o */ |
| 4146 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4147 | /* p q r s t u v w x y z { | } ~ del */ |
| 4148 | 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] | 4149 | }; |
| 4150 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4151 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 4152 | * answer depends on whether we are encoding set O as itself, and also |
| 4153 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 4154 | * clear that the answers to these questions vary between |
| 4155 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 4156 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4157 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 4158 | ((c) < 128 && (c) > 0 && \ |
| 4159 | ((utf7_category[(c)] == 0) || \ |
| 4160 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 4161 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4162 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4163 | PyObject * |
| 4164 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4165 | Py_ssize_t size, |
| 4166 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4167 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4168 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 4169 | } |
| 4170 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4171 | /* The decoder. The only state we preserve is our read position, |
| 4172 | * i.e. how many characters we have consumed. So if we end in the |
| 4173 | * middle of a shift sequence we have to back off the read position |
| 4174 | * and the output to the beginning of the sequence, otherwise we lose |
| 4175 | * all the shift state (seen bits, number of bits seen, high |
| 4176 | * surrogate). */ |
| 4177 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4178 | PyObject * |
| 4179 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4180 | Py_ssize_t size, |
| 4181 | const char *errors, |
| 4182 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4183 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4184 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4185 | Py_ssize_t startinpos; |
| 4186 | Py_ssize_t endinpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4187 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4188 | _PyUnicodeWriter writer; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4189 | const char *errmsg = ""; |
| 4190 | int inShift = 0; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4191 | Py_ssize_t shiftOutStart; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4192 | unsigned int base64bits = 0; |
| 4193 | unsigned long base64buffer = 0; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4194 | Py_UCS4 surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4195 | PyObject *errorHandler = NULL; |
| 4196 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4197 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4198 | if (size == 0) { |
| 4199 | if (consumed) |
| 4200 | *consumed = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4201 | Py_INCREF(unicode_empty); |
| 4202 | return unicode_empty; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4203 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4204 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4205 | /* Start off assuming it's all ASCII. Widen later as necessary. */ |
| 4206 | _PyUnicodeWriter_Init(&writer, 0); |
| 4207 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
| 4208 | goto onError; |
| 4209 | |
| 4210 | shiftOutStart = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4211 | e = s + size; |
| 4212 | |
| 4213 | while (s < e) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4214 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4215 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 4216 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4217 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4218 | if (inShift) { /* in a base-64 section */ |
| 4219 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 4220 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 4221 | base64bits += 6; |
| 4222 | s++; |
| 4223 | if (base64bits >= 16) { |
| 4224 | /* we have enough bits for a UTF-16 value */ |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 4225 | Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16)); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4226 | base64bits -= 16; |
| 4227 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 4228 | if (surrogate) { |
| 4229 | /* expecting a second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4230 | if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) { |
| 4231 | Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4232 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch2) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4233 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4234 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch2); |
| 4235 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4236 | surrogate = 0; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4237 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4238 | } |
| 4239 | else { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4240 | if (_PyUnicodeWriter_Prepare(&writer, 1, surrogate) == -1) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4241 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4242 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, surrogate); |
| 4243 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4244 | surrogate = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4245 | } |
| 4246 | } |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4247 | if (Py_UNICODE_IS_HIGH_SURROGATE(outCh)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4248 | /* first surrogate */ |
| 4249 | surrogate = outCh; |
| 4250 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4251 | else { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4252 | if (_PyUnicodeWriter_Prepare(&writer, 1, outCh) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4253 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4254 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, outCh); |
| 4255 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4256 | } |
| 4257 | } |
| 4258 | } |
| 4259 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4260 | inShift = 0; |
| 4261 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4262 | if (surrogate) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4263 | if (_PyUnicodeWriter_Prepare(&writer, 1, surrogate) == -1) |
Antoine Pitrou | 78edf75 | 2011-11-15 01:44:16 +0100 | [diff] [blame] | 4264 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4265 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, surrogate); |
| 4266 | writer.pos++; |
Antoine Pitrou | 5418ee0 | 2011-11-15 01:42:21 +0100 | [diff] [blame] | 4267 | surrogate = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4268 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4269 | if (base64bits > 0) { /* left-over bits */ |
| 4270 | if (base64bits >= 6) { |
| 4271 | /* We've seen at least one base-64 character */ |
| 4272 | errmsg = "partial character in shift sequence"; |
| 4273 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4274 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4275 | else { |
| 4276 | /* Some bits remain; they should be zero */ |
| 4277 | if (base64buffer != 0) { |
| 4278 | errmsg = "non-zero padding bits in shift sequence"; |
| 4279 | goto utf7Error; |
| 4280 | } |
| 4281 | } |
| 4282 | } |
| 4283 | if (ch != '-') { |
| 4284 | /* '-' is absorbed; other terminating |
| 4285 | characters are preserved */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4286 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4287 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4288 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4289 | writer.pos++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4290 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4291 | } |
| 4292 | } |
| 4293 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4294 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4295 | s++; /* consume '+' */ |
| 4296 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4297 | s++; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4298 | if (_PyUnicodeWriter_Prepare(&writer, 1, '+') == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 4299 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4300 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '+'); |
| 4301 | writer.pos++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4302 | } |
| 4303 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4304 | inShift = 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4305 | shiftOutStart = writer.pos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4306 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4307 | } |
| 4308 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4309 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4310 | s++; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4311 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
| 4312 | goto onError; |
| 4313 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4314 | writer.pos++; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4315 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4316 | else { |
| 4317 | startinpos = s-starts; |
| 4318 | s++; |
| 4319 | errmsg = "unexpected special character"; |
| 4320 | goto utf7Error; |
| 4321 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4322 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4323 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4324 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4325 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4326 | errors, &errorHandler, |
| 4327 | "utf7", errmsg, |
| 4328 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4329 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4330 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4333 | /* end of string */ |
| 4334 | |
| 4335 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 4336 | /* if we're in an inconsistent state, that's an error */ |
| 4337 | if (surrogate || |
| 4338 | (base64bits >= 6) || |
| 4339 | (base64bits > 0 && base64buffer != 0)) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4340 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4341 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4342 | errors, &errorHandler, |
| 4343 | "utf7", "unterminated shift sequence", |
| 4344 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4345 | &writer)) |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4346 | goto onError; |
| 4347 | if (s < e) |
| 4348 | goto restart; |
| 4349 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4350 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4351 | |
| 4352 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4353 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4354 | if (inShift) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4355 | writer.pos = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4356 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4357 | } |
| 4358 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4359 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4360 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 4361 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4362 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4363 | Py_XDECREF(errorHandler); |
| 4364 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4365 | return _PyUnicodeWriter_Finish(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4366 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4367 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4368 | Py_XDECREF(errorHandler); |
| 4369 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4370 | _PyUnicodeWriter_Dealloc(&writer); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4371 | return NULL; |
| 4372 | } |
| 4373 | |
| 4374 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4375 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4376 | _PyUnicode_EncodeUTF7(PyObject *str, |
| 4377 | int base64SetO, |
| 4378 | int base64WhiteSpace, |
| 4379 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4380 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4381 | int kind; |
| 4382 | void *data; |
| 4383 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4384 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4385 | int inShift = 0; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4386 | Py_ssize_t i; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4387 | unsigned int base64bits = 0; |
| 4388 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4389 | char * out; |
| 4390 | char * start; |
| 4391 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4392 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4393 | return NULL; |
| 4394 | kind = PyUnicode_KIND(str); |
| 4395 | data = PyUnicode_DATA(str); |
| 4396 | len = PyUnicode_GET_LENGTH(str); |
| 4397 | |
| 4398 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4399 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4400 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4401 | /* It might be possible to tighten this worst case */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4402 | if (len > PY_SSIZE_T_MAX / 8) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4403 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4404 | v = PyBytes_FromStringAndSize(NULL, len * 8); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4405 | if (v == NULL) |
| 4406 | return NULL; |
| 4407 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4408 | start = out = PyBytes_AS_STRING(v); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4409 | for (i = 0; i < len; ++i) { |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4410 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4411 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4412 | if (inShift) { |
| 4413 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4414 | /* shifting out */ |
| 4415 | if (base64bits) { /* output remaining bits */ |
| 4416 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 4417 | base64buffer = 0; |
| 4418 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4419 | } |
| 4420 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4421 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 4422 | so no '-' is required, except if the character is itself a '-' */ |
| 4423 | if (IS_BASE64(ch) || ch == '-') { |
| 4424 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4425 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4426 | *out++ = (char) ch; |
| 4427 | } |
| 4428 | else { |
| 4429 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4430 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4431 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4432 | else { /* not in a shift sequence */ |
| 4433 | if (ch == '+') { |
| 4434 | *out++ = '+'; |
| 4435 | *out++ = '-'; |
| 4436 | } |
| 4437 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 4438 | *out++ = (char) ch; |
| 4439 | } |
| 4440 | else { |
| 4441 | *out++ = '+'; |
| 4442 | inShift = 1; |
| 4443 | goto encode_char; |
| 4444 | } |
| 4445 | } |
| 4446 | continue; |
| 4447 | encode_char: |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4448 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 4449 | assert(ch <= MAX_UNICODE); |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 4450 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4451 | /* code first surrogate */ |
| 4452 | base64bits += 16; |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 4453 | base64buffer = (base64buffer << 16) | Py_UNICODE_HIGH_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4454 | while (base64bits >= 6) { |
| 4455 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4456 | base64bits -= 6; |
| 4457 | } |
| 4458 | /* prepare second surrogate */ |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 4459 | ch = Py_UNICODE_LOW_SURROGATE(ch); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4460 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4461 | base64bits += 16; |
| 4462 | base64buffer = (base64buffer << 16) | ch; |
| 4463 | while (base64bits >= 6) { |
| 4464 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 4465 | base64bits -= 6; |
| 4466 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 4467 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4468 | if (base64bits) |
| 4469 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 4470 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4471 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4472 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 4473 | return NULL; |
| 4474 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4475 | } |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4476 | PyObject * |
| 4477 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
| 4478 | Py_ssize_t size, |
| 4479 | int base64SetO, |
| 4480 | int base64WhiteSpace, |
| 4481 | const char *errors) |
| 4482 | { |
| 4483 | PyObject *result; |
| 4484 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 4485 | if (tmp == NULL) |
| 4486 | return NULL; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 4487 | result = _PyUnicode_EncodeUTF7(tmp, base64SetO, |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4488 | base64WhiteSpace, errors); |
| 4489 | Py_DECREF(tmp); |
| 4490 | return result; |
| 4491 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4492 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 4493 | #undef IS_BASE64 |
| 4494 | #undef FROM_BASE64 |
| 4495 | #undef TO_BASE64 |
| 4496 | #undef DECODE_DIRECT |
| 4497 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 4498 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4499 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 4500 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4501 | PyObject * |
| 4502 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 4503 | Py_ssize_t size, |
| 4504 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4505 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4506 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 4507 | } |
| 4508 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4509 | #include "stringlib/asciilib.h" |
| 4510 | #include "stringlib/codecs.h" |
| 4511 | #include "stringlib/undef.h" |
| 4512 | |
Antoine Pitrou | 0a3229d | 2011-11-21 20:39:13 +0100 | [diff] [blame] | 4513 | #include "stringlib/ucs1lib.h" |
| 4514 | #include "stringlib/codecs.h" |
| 4515 | #include "stringlib/undef.h" |
| 4516 | |
| 4517 | #include "stringlib/ucs2lib.h" |
| 4518 | #include "stringlib/codecs.h" |
| 4519 | #include "stringlib/undef.h" |
| 4520 | |
| 4521 | #include "stringlib/ucs4lib.h" |
| 4522 | #include "stringlib/codecs.h" |
| 4523 | #include "stringlib/undef.h" |
| 4524 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4525 | /* Mask to quickly check whether a C 'long' contains a |
| 4526 | non-ASCII, UTF8-encoded char. */ |
| 4527 | #if (SIZEOF_LONG == 8) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4528 | # define ASCII_CHAR_MASK 0x8080808080808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4529 | #elif (SIZEOF_LONG == 4) |
Mark Dickinson | 01ac8b6 | 2012-07-07 14:08:48 +0200 | [diff] [blame] | 4530 | # define ASCII_CHAR_MASK 0x80808080UL |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4531 | #else |
| 4532 | # error C 'long' size should be either 4 or 8! |
| 4533 | #endif |
| 4534 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4535 | static Py_ssize_t |
| 4536 | 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] | 4537 | { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4538 | const char *p = start; |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4539 | 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] | 4540 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4541 | #if SIZEOF_LONG <= SIZEOF_VOID_P |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4542 | assert(_Py_IS_ALIGNED(dest, SIZEOF_LONG)); |
| 4543 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4544 | /* Fast path, see in STRINGLIB(utf8_decode) for |
| 4545 | an explanation. */ |
| 4546 | /* Help register allocation */ |
| 4547 | register const char *_p = p; |
| 4548 | register Py_UCS1 * q = dest; |
| 4549 | while (_p < aligned_end) { |
| 4550 | unsigned long value = *(const unsigned long *) _p; |
| 4551 | if (value & ASCII_CHAR_MASK) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4552 | break; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4553 | *((unsigned long *)q) = value; |
| 4554 | _p += SIZEOF_LONG; |
| 4555 | q += SIZEOF_LONG; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4556 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4557 | p = _p; |
| 4558 | while (p < end) { |
| 4559 | if ((unsigned char)*p & 0x80) |
| 4560 | break; |
| 4561 | *q++ = *p++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4562 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4563 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4564 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4565 | #endif |
| 4566 | while (p < end) { |
| 4567 | /* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h |
| 4568 | for an explanation. */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 4569 | if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4570 | /* Help register allocation */ |
| 4571 | register const char *_p = p; |
| 4572 | while (_p < aligned_end) { |
| 4573 | unsigned long value = *(unsigned long *) _p; |
| 4574 | if (value & ASCII_CHAR_MASK) |
| 4575 | break; |
| 4576 | _p += SIZEOF_LONG; |
| 4577 | } |
| 4578 | p = _p; |
| 4579 | if (_p == end) |
| 4580 | break; |
| 4581 | } |
| 4582 | if ((unsigned char)*p & 0x80) |
| 4583 | break; |
| 4584 | ++p; |
| 4585 | } |
| 4586 | memcpy(dest, start, p - start); |
| 4587 | return p - start; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4588 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4589 | |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4590 | PyObject * |
| 4591 | PyUnicode_DecodeUTF8Stateful(const char *s, |
| 4592 | Py_ssize_t size, |
| 4593 | const char *errors, |
| 4594 | Py_ssize_t *consumed) |
| 4595 | { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4596 | _PyUnicodeWriter writer; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4597 | const char *starts = s; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4598 | const char *end = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4599 | |
| 4600 | Py_ssize_t startinpos; |
| 4601 | Py_ssize_t endinpos; |
| 4602 | const char *errmsg = ""; |
| 4603 | PyObject *errorHandler = NULL; |
| 4604 | PyObject *exc = NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4605 | |
| 4606 | if (size == 0) { |
| 4607 | if (consumed) |
| 4608 | *consumed = 0; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 4609 | Py_INCREF(unicode_empty); |
| 4610 | return unicode_empty; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4611 | } |
| 4612 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4613 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
| 4614 | if (size == 1 && (unsigned char)s[0] < 128) { |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4615 | if (consumed) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4616 | *consumed = 1; |
| 4617 | return get_latin1_char((unsigned char)s[0]); |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4618 | } |
| 4619 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4620 | _PyUnicodeWriter_Init(&writer, 0); |
| 4621 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
| 4622 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4623 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4624 | writer.pos = ascii_decode(s, end, writer.data); |
| 4625 | s += writer.pos; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4626 | while (s < end) { |
| 4627 | Py_UCS4 ch; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4628 | int kind = writer.kind; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4629 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4630 | if (PyUnicode_IS_ASCII(writer.buffer)) |
| 4631 | ch = asciilib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4632 | else |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4633 | ch = ucs1lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4634 | } else if (kind == PyUnicode_2BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4635 | ch = ucs2lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4636 | } else { |
| 4637 | assert(kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4638 | ch = ucs4lib_utf8_decode(&s, end, writer.data, &writer.pos); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | switch (ch) { |
| 4642 | case 0: |
| 4643 | if (s == end || consumed) |
| 4644 | goto End; |
| 4645 | errmsg = "unexpected end of data"; |
| 4646 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4647 | endinpos = end - starts; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4648 | break; |
| 4649 | case 1: |
| 4650 | errmsg = "invalid start byte"; |
| 4651 | startinpos = s - starts; |
| 4652 | endinpos = startinpos + 1; |
| 4653 | break; |
| 4654 | case 2: |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4655 | case 3: |
| 4656 | case 4: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4657 | errmsg = "invalid continuation byte"; |
| 4658 | startinpos = s - starts; |
Ezio Melotti | f7ed5d1 | 2012-11-04 23:21:38 +0200 | [diff] [blame] | 4659 | endinpos = startinpos + ch - 1; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4660 | break; |
| 4661 | default: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4662 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4663 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4664 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4665 | writer.pos++; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4666 | continue; |
| 4667 | } |
| 4668 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4669 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4670 | errors, &errorHandler, |
| 4671 | "utf-8", errmsg, |
| 4672 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4673 | &writer)) |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4674 | goto onError; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4675 | } |
| 4676 | |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4677 | End: |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4678 | if (consumed) |
| 4679 | *consumed = s - starts; |
| 4680 | |
| 4681 | Py_XDECREF(errorHandler); |
| 4682 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4683 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4684 | |
| 4685 | onError: |
| 4686 | Py_XDECREF(errorHandler); |
| 4687 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4688 | _PyUnicodeWriter_Dealloc(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4689 | return NULL; |
Victor Stinner | 785938e | 2011-12-11 20:09:03 +0100 | [diff] [blame] | 4690 | } |
| 4691 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4692 | #ifdef __APPLE__ |
| 4693 | |
| 4694 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4695 | used to decode the command line arguments on Mac OS X. |
| 4696 | |
| 4697 | Return a pointer to a newly allocated wide character string (use |
| 4698 | PyMem_Free() to free the memory), or NULL on memory allocation error. */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4699 | |
| 4700 | wchar_t* |
| 4701 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4702 | { |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4703 | const char *e; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4704 | wchar_t *unicode; |
| 4705 | Py_ssize_t outpos; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4706 | |
| 4707 | /* Note: size will always be longer than the resulting Unicode |
| 4708 | character count */ |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 4709 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4710 | return NULL; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4711 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4712 | if (!unicode) |
| 4713 | return NULL; |
| 4714 | |
| 4715 | /* Unpack UTF-8 encoded data */ |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4716 | e = s + size; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4717 | outpos = 0; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4718 | while (s < e) { |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4719 | Py_UCS4 ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4720 | #if SIZEOF_WCHAR_T == 4 |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4721 | ch = ucs4lib_utf8_decode(&s, e, (Py_UCS4 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4722 | #else |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4723 | ch = ucs2lib_utf8_decode(&s, e, (Py_UCS2 *)unicode, &outpos); |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4724 | #endif |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4725 | if (ch > 0xFF) { |
| 4726 | #if SIZEOF_WCHAR_T == 4 |
| 4727 | assert(0); |
| 4728 | #else |
| 4729 | assert(Py_UNICODE_IS_SURROGATE(ch)); |
| 4730 | /* compute and append the two surrogates: */ |
| 4731 | unicode[outpos++] = (wchar_t)Py_UNICODE_HIGH_SURROGATE(ch); |
| 4732 | unicode[outpos++] = (wchar_t)Py_UNICODE_LOW_SURROGATE(ch); |
| 4733 | #endif |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4734 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4735 | else { |
| 4736 | if (!ch && s == e) |
| 4737 | break; |
| 4738 | /* surrogateescape */ |
| 4739 | unicode[outpos++] = 0xDC00 + (unsigned char)*s++; |
| 4740 | } |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4741 | } |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 4742 | unicode[outpos] = L'\0'; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4743 | return unicode; |
| 4744 | } |
| 4745 | |
| 4746 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4747 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4748 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4749 | |
| 4750 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4751 | and allocate exactly as much space needed at the end. Else allocate the |
| 4752 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4753 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4754 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4755 | PyObject * |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 4756 | _PyUnicode_AsUTF8String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4757 | { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4758 | enum PyUnicode_Kind kind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4759 | void *data; |
| 4760 | Py_ssize_t size; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4761 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4762 | if (!PyUnicode_Check(unicode)) { |
| 4763 | PyErr_BadArgument(); |
| 4764 | return NULL; |
| 4765 | } |
| 4766 | |
| 4767 | if (PyUnicode_READY(unicode) == -1) |
| 4768 | return NULL; |
| 4769 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4770 | if (PyUnicode_UTF8(unicode)) |
| 4771 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4772 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4773 | |
| 4774 | kind = PyUnicode_KIND(unicode); |
| 4775 | data = PyUnicode_DATA(unicode); |
| 4776 | size = PyUnicode_GET_LENGTH(unicode); |
| 4777 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 4778 | switch (kind) { |
Victor Stinner | 6099a03 | 2011-12-18 14:22:26 +0100 | [diff] [blame] | 4779 | default: |
| 4780 | assert(0); |
| 4781 | case PyUnicode_1BYTE_KIND: |
| 4782 | /* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ |
| 4783 | assert(!PyUnicode_IS_ASCII(unicode)); |
| 4784 | return ucs1lib_utf8_encoder(unicode, data, size, errors); |
| 4785 | case PyUnicode_2BYTE_KIND: |
| 4786 | return ucs2lib_utf8_encoder(unicode, data, size, errors); |
| 4787 | case PyUnicode_4BYTE_KIND: |
| 4788 | return ucs4lib_utf8_encoder(unicode, data, size, errors); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4789 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4792 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4793 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4794 | Py_ssize_t size, |
| 4795 | const char *errors) |
| 4796 | { |
| 4797 | PyObject *v, *unicode; |
| 4798 | |
| 4799 | unicode = PyUnicode_FromUnicode(s, size); |
| 4800 | if (unicode == NULL) |
| 4801 | return NULL; |
| 4802 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4803 | Py_DECREF(unicode); |
| 4804 | return v; |
| 4805 | } |
| 4806 | |
| 4807 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4808 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4809 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4810 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4813 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4814 | |
| 4815 | PyObject * |
| 4816 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4817 | Py_ssize_t size, |
| 4818 | const char *errors, |
| 4819 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4820 | { |
| 4821 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4822 | } |
| 4823 | |
| 4824 | PyObject * |
| 4825 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4826 | Py_ssize_t size, |
| 4827 | const char *errors, |
| 4828 | int *byteorder, |
| 4829 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4830 | { |
| 4831 | const char *starts = s; |
| 4832 | Py_ssize_t startinpos; |
| 4833 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4834 | _PyUnicodeWriter writer; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4835 | const unsigned char *q, *e; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4836 | int le, bo = 0; /* assume native ordering by default */ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4837 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4838 | PyObject *errorHandler = NULL; |
| 4839 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4840 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4841 | q = (unsigned char *)s; |
| 4842 | e = q + size; |
| 4843 | |
| 4844 | if (byteorder) |
| 4845 | bo = *byteorder; |
| 4846 | |
| 4847 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4848 | byte order setting accordingly. In native mode, the leading BOM |
| 4849 | mark is skipped, in all other modes, it is copied to the output |
| 4850 | stream as-is (giving a ZWNBSP character). */ |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4851 | if (bo == 0 && size >= 4) { |
| 4852 | Py_UCS4 bom = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4853 | if (bom == 0x0000FEFF) { |
| 4854 | bo = -1; |
| 4855 | q += 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4856 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4857 | else if (bom == 0xFFFE0000) { |
| 4858 | bo = 1; |
| 4859 | q += 4; |
| 4860 | } |
| 4861 | if (byteorder) |
| 4862 | *byteorder = bo; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4865 | if (q == e) { |
| 4866 | if (consumed) |
| 4867 | *consumed = size; |
| 4868 | Py_INCREF(unicode_empty); |
| 4869 | return unicode_empty; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4872 | #ifdef WORDS_BIGENDIAN |
| 4873 | le = bo < 0; |
| 4874 | #else |
| 4875 | le = bo <= 0; |
| 4876 | #endif |
| 4877 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4878 | _PyUnicodeWriter_Init(&writer, 0); |
| 4879 | if (_PyUnicodeWriter_Prepare(&writer, (e - q + 3) / 4, 127) == -1) |
| 4880 | goto onError; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4881 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4882 | while (1) { |
| 4883 | Py_UCS4 ch = 0; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4884 | Py_UCS4 maxch = PyUnicode_MAX_CHAR_VALUE(writer.buffer); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4885 | |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4886 | if (e - q >= 4) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4887 | enum PyUnicode_Kind kind = writer.kind; |
| 4888 | void *data = writer.data; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4889 | const unsigned char *last = e - 4; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4890 | Py_ssize_t pos = writer.pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4891 | if (le) { |
| 4892 | do { |
| 4893 | ch = (q[3] << 24) | (q[2] << 16) | (q[1] << 8) | q[0]; |
| 4894 | if (ch > maxch) |
| 4895 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4896 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4897 | q += 4; |
| 4898 | } while (q <= last); |
| 4899 | } |
| 4900 | else { |
| 4901 | do { |
| 4902 | ch = (q[0] << 24) | (q[1] << 16) | (q[2] << 8) | q[3]; |
| 4903 | if (ch > maxch) |
| 4904 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4905 | PyUnicode_WRITE(kind, data, pos++, ch); |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4906 | q += 4; |
| 4907 | } while (q <= last); |
| 4908 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4909 | writer.pos = pos; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | if (ch <= maxch) { |
| 4913 | if (q == e || consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4914 | break; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4915 | /* remaining bytes at the end? (size should be divisible by 4) */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4916 | errmsg = "truncated data"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4917 | startinpos = ((const char *)q) - starts; |
| 4918 | endinpos = ((const char *)e) - starts; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4919 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4920 | else { |
| 4921 | if (ch < 0x110000) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4922 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4923 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4924 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 4925 | writer.pos++; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4926 | q += 4; |
| 4927 | continue; |
| 4928 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4929 | errmsg = "codepoint not in range(0x110000)"; |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4930 | startinpos = ((const char *)q) - starts; |
| 4931 | endinpos = startinpos + 4; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4932 | } |
Victor Stinner | e64322e | 2012-10-30 23:12:47 +0100 | [diff] [blame] | 4933 | |
| 4934 | /* The remaining input chars are ignored if the callback |
| 4935 | chooses to skip the input */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4936 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4937 | errors, &errorHandler, |
| 4938 | "utf32", errmsg, |
| 4939 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4940 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4941 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4944 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4945 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4946 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4947 | Py_XDECREF(errorHandler); |
| 4948 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4949 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4950 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4951 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 4952 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4953 | Py_XDECREF(errorHandler); |
| 4954 | Py_XDECREF(exc); |
| 4955 | return NULL; |
| 4956 | } |
| 4957 | |
| 4958 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4959 | _PyUnicode_EncodeUTF32(PyObject *str, |
| 4960 | const char *errors, |
| 4961 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4962 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4963 | int kind; |
| 4964 | void *data; |
| 4965 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4966 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4967 | unsigned char *p; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4968 | Py_ssize_t nsize, i; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4969 | /* Offsets from p for storing byte pairs in the right order. */ |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 4970 | #if PY_LITTLE_ENDIAN |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4971 | int iorder[] = {0, 1, 2, 3}; |
| 4972 | #else |
| 4973 | int iorder[] = {3, 2, 1, 0}; |
| 4974 | #endif |
| 4975 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4976 | #define STORECHAR(CH) \ |
| 4977 | do { \ |
| 4978 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4979 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4980 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4981 | p[iorder[0]] = (CH) & 0xff; \ |
| 4982 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4983 | } while(0) |
| 4984 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4985 | if (!PyUnicode_Check(str)) { |
| 4986 | PyErr_BadArgument(); |
| 4987 | return NULL; |
| 4988 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 4989 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 4990 | return NULL; |
| 4991 | kind = PyUnicode_KIND(str); |
| 4992 | data = PyUnicode_DATA(str); |
| 4993 | len = PyUnicode_GET_LENGTH(str); |
| 4994 | |
| 4995 | nsize = len + (byteorder == 0); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4996 | if (nsize > PY_SSIZE_T_MAX / 4) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4997 | return PyErr_NoMemory(); |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 4998 | v = PyBytes_FromStringAndSize(NULL, nsize * 4); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4999 | if (v == NULL) |
| 5000 | return NULL; |
| 5001 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5002 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5003 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5004 | STORECHAR(0xFEFF); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5005 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5006 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5007 | |
| 5008 | if (byteorder == -1) { |
| 5009 | /* force LE */ |
| 5010 | iorder[0] = 0; |
| 5011 | iorder[1] = 1; |
| 5012 | iorder[2] = 2; |
| 5013 | iorder[3] = 3; |
| 5014 | } |
| 5015 | else if (byteorder == 1) { |
| 5016 | /* force BE */ |
| 5017 | iorder[0] = 3; |
| 5018 | iorder[1] = 2; |
| 5019 | iorder[2] = 1; |
| 5020 | iorder[3] = 0; |
| 5021 | } |
| 5022 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5023 | for (i = 0; i < len; i++) |
| 5024 | STORECHAR(PyUnicode_READ(kind, data, i)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5025 | |
| 5026 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5027 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5028 | #undef STORECHAR |
| 5029 | } |
| 5030 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5031 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5032 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 5033 | Py_ssize_t size, |
| 5034 | const char *errors, |
| 5035 | int byteorder) |
| 5036 | { |
| 5037 | PyObject *result; |
| 5038 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5039 | if (tmp == NULL) |
| 5040 | return NULL; |
| 5041 | result = _PyUnicode_EncodeUTF32(tmp, errors, byteorder); |
| 5042 | Py_DECREF(tmp); |
| 5043 | return result; |
| 5044 | } |
| 5045 | |
| 5046 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5047 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5048 | { |
Victor Stinner | b960b34 | 2011-11-20 19:12:52 +0100 | [diff] [blame] | 5049 | return _PyUnicode_EncodeUTF32(unicode, NULL, 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5052 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 5053 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5054 | PyObject * |
| 5055 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5056 | Py_ssize_t size, |
| 5057 | const char *errors, |
| 5058 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5059 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5060 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 5061 | } |
| 5062 | |
| 5063 | PyObject * |
| 5064 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | Py_ssize_t size, |
| 5066 | const char *errors, |
| 5067 | int *byteorder, |
| 5068 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5069 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5070 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5071 | Py_ssize_t startinpos; |
| 5072 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5073 | _PyUnicodeWriter writer; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5074 | const unsigned char *q, *e; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5075 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5076 | int native_ordering; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 5077 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5078 | PyObject *errorHandler = NULL; |
| 5079 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5080 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5081 | q = (unsigned char *)s; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5082 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5083 | |
| 5084 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5085 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5086 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5087 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 5088 | byte order setting accordingly. In native mode, the leading BOM |
| 5089 | mark is skipped, in all other modes, it is copied to the output |
| 5090 | stream as-is (giving a ZWNBSP character). */ |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5091 | if (bo == 0 && size >= 2) { |
| 5092 | const Py_UCS4 bom = (q[1] << 8) | q[0]; |
| 5093 | if (bom == 0xFEFF) { |
| 5094 | q += 2; |
| 5095 | bo = -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5096 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5097 | else if (bom == 0xFFFE) { |
| 5098 | q += 2; |
| 5099 | bo = 1; |
| 5100 | } |
| 5101 | if (byteorder) |
| 5102 | *byteorder = bo; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 5103 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5104 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5105 | if (q == e) { |
| 5106 | if (consumed) |
| 5107 | *consumed = size; |
| 5108 | Py_INCREF(unicode_empty); |
| 5109 | return unicode_empty; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5110 | } |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5111 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5112 | #if PY_LITTLE_ENDIAN |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5113 | native_ordering = bo <= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5114 | #else |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5115 | native_ordering = bo >= 0; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5116 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5117 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5118 | /* Note: size will always be longer than the resulting Unicode |
| 5119 | character count */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5120 | _PyUnicodeWriter_Init(&writer, 0); |
| 5121 | if (_PyUnicodeWriter_Prepare(&writer, (e - q + 1) / 2, 127) == -1) |
| 5122 | goto onError; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5123 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5124 | while (1) { |
| 5125 | Py_UCS4 ch = 0; |
| 5126 | if (e - q >= 2) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5127 | int kind = writer.kind; |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5128 | if (kind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5129 | if (PyUnicode_IS_ASCII(writer.buffer)) |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5130 | ch = asciilib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5131 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5132 | native_ordering); |
| 5133 | else |
| 5134 | ch = ucs1lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5135 | (Py_UCS1*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5136 | native_ordering); |
| 5137 | } else if (kind == PyUnicode_2BYTE_KIND) { |
| 5138 | ch = ucs2lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5139 | (Py_UCS2*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5140 | native_ordering); |
| 5141 | } else { |
| 5142 | assert(kind == PyUnicode_4BYTE_KIND); |
| 5143 | ch = ucs4lib_utf16_decode(&q, e, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5144 | (Py_UCS4*)writer.data, &writer.pos, |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5145 | native_ordering); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5146 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5147 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5148 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5149 | switch (ch) |
| 5150 | { |
| 5151 | case 0: |
| 5152 | /* remaining byte at the end? (size should be even) */ |
| 5153 | if (q == e || consumed) |
| 5154 | goto End; |
| 5155 | errmsg = "truncated data"; |
| 5156 | startinpos = ((const char *)q) - starts; |
| 5157 | endinpos = ((const char *)e) - starts; |
| 5158 | break; |
| 5159 | /* The remaining input chars are ignored if the callback |
| 5160 | chooses to skip the input */ |
| 5161 | case 1: |
| 5162 | errmsg = "unexpected end of data"; |
| 5163 | startinpos = ((const char *)q) - 2 - starts; |
| 5164 | endinpos = ((const char *)e) - starts; |
| 5165 | break; |
| 5166 | case 2: |
| 5167 | errmsg = "illegal encoding"; |
| 5168 | startinpos = ((const char *)q) - 2 - starts; |
| 5169 | endinpos = startinpos + 2; |
| 5170 | break; |
| 5171 | case 3: |
| 5172 | errmsg = "illegal UTF-16 surrogate"; |
| 5173 | startinpos = ((const char *)q) - 4 - starts; |
| 5174 | endinpos = startinpos + 2; |
| 5175 | break; |
| 5176 | default: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5177 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5178 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5179 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 5180 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5181 | continue; |
| 5182 | } |
| 5183 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5184 | if (unicode_decode_call_errorhandler_writer( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5185 | errors, |
| 5186 | &errorHandler, |
| 5187 | "utf16", errmsg, |
| 5188 | &starts, |
| 5189 | (const char **)&e, |
| 5190 | &startinpos, |
| 5191 | &endinpos, |
| 5192 | &exc, |
| 5193 | (const char **)&q, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5194 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5195 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5196 | } |
| 5197 | |
Antoine Pitrou | 63065d7 | 2012-05-15 23:48:04 +0200 | [diff] [blame] | 5198 | End: |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5199 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5200 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5201 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5202 | Py_XDECREF(errorHandler); |
| 5203 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5204 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5206 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5207 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5208 | Py_XDECREF(errorHandler); |
| 5209 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5210 | return NULL; |
| 5211 | } |
| 5212 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5213 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5214 | _PyUnicode_EncodeUTF16(PyObject *str, |
| 5215 | const char *errors, |
| 5216 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5217 | { |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5218 | enum PyUnicode_Kind kind; |
| 5219 | const void *data; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5220 | Py_ssize_t len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5221 | PyObject *v; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5222 | unsigned short *out; |
| 5223 | Py_ssize_t bytesize; |
| 5224 | Py_ssize_t pairs; |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 5225 | #if PY_BIG_ENDIAN |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5226 | int native_ordering = byteorder >= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5227 | #else |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5228 | int native_ordering = byteorder <= 0; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5229 | #endif |
| 5230 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5231 | if (!PyUnicode_Check(str)) { |
| 5232 | PyErr_BadArgument(); |
| 5233 | return NULL; |
| 5234 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5235 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5236 | return NULL; |
| 5237 | kind = PyUnicode_KIND(str); |
| 5238 | data = PyUnicode_DATA(str); |
| 5239 | len = PyUnicode_GET_LENGTH(str); |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5240 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5241 | pairs = 0; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5242 | if (kind == PyUnicode_4BYTE_KIND) { |
| 5243 | const Py_UCS4 *in = (const Py_UCS4 *)data; |
| 5244 | const Py_UCS4 *end = in + len; |
| 5245 | while (in < end) |
| 5246 | if (*in++ >= 0x10000) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5247 | pairs++; |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5248 | } |
| 5249 | if (len > PY_SSIZE_T_MAX / 2 - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5250 | return PyErr_NoMemory(); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5251 | bytesize = (len + pairs + (byteorder == 0)) * 2; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5252 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5253 | if (v == NULL) |
| 5254 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5255 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5256 | /* output buffer is 2-bytes aligned */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 5257 | assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(v), 2)); |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5258 | out = (unsigned short *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5259 | if (byteorder == 0) |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5260 | *out++ = 0xFEFF; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5261 | if (len == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5262 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5263 | |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5264 | switch (kind) { |
| 5265 | case PyUnicode_1BYTE_KIND: { |
| 5266 | ucs1lib_utf16_encode(out, (const Py_UCS1 *)data, len, native_ordering); |
| 5267 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5268 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5269 | case PyUnicode_2BYTE_KIND: { |
| 5270 | ucs2lib_utf16_encode(out, (const Py_UCS2 *)data, len, native_ordering); |
| 5271 | break; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5272 | } |
Antoine Pitrou | 27f6a3b | 2012-06-15 22:15:23 +0200 | [diff] [blame] | 5273 | case PyUnicode_4BYTE_KIND: { |
| 5274 | ucs4lib_utf16_encode(out, (const Py_UCS4 *)data, len, native_ordering); |
| 5275 | break; |
| 5276 | } |
| 5277 | default: |
| 5278 | assert(0); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5279 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5280 | |
| 5281 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5282 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5285 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5286 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
| 5287 | Py_ssize_t size, |
| 5288 | const char *errors, |
| 5289 | int byteorder) |
| 5290 | { |
| 5291 | PyObject *result; |
| 5292 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5293 | if (tmp == NULL) |
| 5294 | return NULL; |
| 5295 | result = _PyUnicode_EncodeUTF16(tmp, errors, byteorder); |
| 5296 | Py_DECREF(tmp); |
| 5297 | return result; |
| 5298 | } |
| 5299 | |
| 5300 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5301 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5302 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5303 | return _PyUnicode_EncodeUTF16(unicode, NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5304 | } |
| 5305 | |
| 5306 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5307 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5308 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5309 | if all the escapes in the string make it still a valid ASCII string. |
| 5310 | Returns -1 if any escapes were found which cause the string to |
| 5311 | pop out of ASCII range. Otherwise returns the length of the |
| 5312 | required buffer to hold the string. |
| 5313 | */ |
Antoine Pitrou | 53bb548 | 2011-10-10 23:49:24 +0200 | [diff] [blame] | 5314 | static Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5315 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5316 | { |
| 5317 | const unsigned char *p = (const unsigned char *)s; |
| 5318 | const unsigned char *end = p + size; |
| 5319 | Py_ssize_t length = 0; |
| 5320 | |
| 5321 | if (size < 0) |
| 5322 | return -1; |
| 5323 | |
| 5324 | for (; p < end; ++p) { |
| 5325 | if (*p > 127) { |
| 5326 | /* Non-ASCII */ |
| 5327 | return -1; |
| 5328 | } |
| 5329 | else if (*p != '\\') { |
| 5330 | /* Normal character */ |
| 5331 | ++length; |
| 5332 | } |
| 5333 | else { |
| 5334 | /* Backslash-escape, check next char */ |
| 5335 | ++p; |
| 5336 | /* Escape sequence reaches till end of string or |
| 5337 | non-ASCII follow-up. */ |
| 5338 | if (p >= end || *p > 127) |
| 5339 | return -1; |
| 5340 | switch (*p) { |
| 5341 | case '\n': |
| 5342 | /* backslash + \n result in zero characters */ |
| 5343 | break; |
| 5344 | case '\\': case '\'': case '\"': |
| 5345 | case 'b': case 'f': case 't': |
| 5346 | case 'n': case 'r': case 'v': case 'a': |
| 5347 | ++length; |
| 5348 | break; |
| 5349 | case '0': case '1': case '2': case '3': |
| 5350 | case '4': case '5': case '6': case '7': |
| 5351 | case 'x': case 'u': case 'U': case 'N': |
| 5352 | /* these do not guarantee ASCII characters */ |
| 5353 | return -1; |
| 5354 | default: |
| 5355 | /* count the backslash + the other character */ |
| 5356 | length += 2; |
| 5357 | } |
| 5358 | } |
| 5359 | } |
| 5360 | return length; |
| 5361 | } |
| 5362 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5363 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5364 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5365 | PyObject * |
| 5366 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5367 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5368 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5369 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5370 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5371 | Py_ssize_t startinpos; |
| 5372 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5373 | int j; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5374 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5375 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5376 | char* message; |
| 5377 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5378 | PyObject *errorHandler = NULL; |
| 5379 | PyObject *exc = NULL; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5380 | Py_ssize_t len; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5381 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5382 | len = length_of_escaped_ascii_string(s, size); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5383 | if (len == 0) { |
| 5384 | Py_INCREF(unicode_empty); |
| 5385 | return unicode_empty; |
| 5386 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5387 | |
| 5388 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5389 | either the string is pure ASCII with named escapes like \n, etc. |
| 5390 | and we determined it's exact size (common case) |
| 5391 | or it contains \x, \u, ... escape sequences. then we create a |
| 5392 | legacy wchar string and resize it at the end of this function. */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5393 | _PyUnicodeWriter_Init(&writer, 0); |
| 5394 | if (len > 0) { |
| 5395 | if (_PyUnicodeWriter_Prepare(&writer, len, 127) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5396 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5397 | assert(writer.kind == PyUnicode_1BYTE_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5398 | } |
| 5399 | else { |
| 5400 | /* Escaped strings will always be longer than the resulting |
| 5401 | Unicode string, so we start with size here and then reduce the |
| 5402 | length after conversion to the true value. |
| 5403 | (but if the error callback returns a long replacement string |
| 5404 | we'll have to allocate more space) */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5405 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5406 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5407 | } |
| 5408 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5409 | if (size == 0) |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5410 | return _PyUnicodeWriter_Finish(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5411 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5412 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5413 | while (s < end) { |
| 5414 | unsigned char c; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 5415 | Py_UCS4 x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5416 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5417 | |
| 5418 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5419 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5420 | x = (unsigned char)*s; |
| 5421 | s++; |
| 5422 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5423 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5424 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5425 | writer.pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5426 | continue; |
| 5427 | } |
| 5428 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5429 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5430 | /* \ - Escapes */ |
| 5431 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5432 | c = *s++; |
| 5433 | if (s > end) |
| 5434 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5435 | |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5436 | /* The only case in which i == ascii_length is a backslash |
| 5437 | followed by a newline. */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5438 | 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] | 5439 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5440 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5442 | /* \x escapes */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5443 | #define WRITECHAR(ch) \ |
| 5444 | do { \ |
| 5445 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) \ |
| 5446 | goto onError; \ |
| 5447 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); \ |
| 5448 | writer.pos++; \ |
| 5449 | } while(0) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5450 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5451 | case '\n': break; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5452 | case '\\': WRITECHAR('\\'); break; |
| 5453 | case '\'': WRITECHAR('\''); break; |
| 5454 | case '\"': WRITECHAR('\"'); break; |
| 5455 | case 'b': WRITECHAR('\b'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5456 | /* FF */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5457 | case 'f': WRITECHAR('\014'); break; |
| 5458 | case 't': WRITECHAR('\t'); break; |
| 5459 | case 'n': WRITECHAR('\n'); break; |
| 5460 | case 'r': WRITECHAR('\r'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5461 | /* VT */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5462 | case 'v': WRITECHAR('\013'); break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5463 | /* BEL, not classic C */ |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5464 | case 'a': WRITECHAR('\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5465 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5466 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5467 | case '0': case '1': case '2': case '3': |
| 5468 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5469 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5470 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5471 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5472 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5473 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5474 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5475 | WRITECHAR(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5476 | break; |
| 5477 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5478 | /* hex escapes */ |
| 5479 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5481 | digits = 2; |
| 5482 | message = "truncated \\xXX escape"; |
| 5483 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5484 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5485 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5486 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5487 | digits = 4; |
| 5488 | message = "truncated \\uXXXX 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 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5492 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5493 | digits = 8; |
| 5494 | message = "truncated \\UXXXXXXXX escape"; |
| 5495 | hexescape: |
| 5496 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5497 | if (s+digits>end) { |
| 5498 | endinpos = size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5499 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5500 | errors, &errorHandler, |
| 5501 | "unicodeescape", "end of string in escape sequence", |
| 5502 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5503 | &writer)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5504 | goto onError; |
| 5505 | goto nextByte; |
| 5506 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5507 | for (j = 0; j < digits; ++j) { |
| 5508 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5509 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5510 | endinpos = (s+j+1)-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5511 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5512 | errors, &errorHandler, |
| 5513 | "unicodeescape", message, |
| 5514 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5515 | &writer)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5516 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5517 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5518 | } |
| 5519 | chr = (chr<<4) & ~0xF; |
| 5520 | if (c >= '0' && c <= '9') |
| 5521 | chr += c - '0'; |
| 5522 | else if (c >= 'a' && c <= 'f') |
| 5523 | chr += 10 + c - 'a'; |
| 5524 | else |
| 5525 | chr += 10 + c - 'A'; |
| 5526 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5527 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5528 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5529 | /* _decoding_error will have already written into the |
| 5530 | target buffer. */ |
| 5531 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5532 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5533 | /* when we get here, chr is a 32-bit unicode character */ |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5534 | if (chr <= MAX_UNICODE) { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5535 | WRITECHAR(chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5536 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5537 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5538 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5539 | errors, &errorHandler, |
| 5540 | "unicodeescape", "illegal Unicode character", |
| 5541 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5542 | &writer)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5543 | goto onError; |
| 5544 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5545 | break; |
| 5546 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5547 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5548 | case 'N': |
| 5549 | message = "malformed \\N character escape"; |
| 5550 | if (ucnhash_CAPI == NULL) { |
| 5551 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5552 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5553 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5554 | if (ucnhash_CAPI == NULL) |
| 5555 | goto ucnhashError; |
| 5556 | } |
| 5557 | if (*s == '{') { |
| 5558 | const char *start = s+1; |
| 5559 | /* look for the closing brace */ |
| 5560 | while (*s != '}' && s < end) |
| 5561 | s++; |
| 5562 | if (s > start && s < end && *s == '}') { |
| 5563 | /* found a name. look it up in the unicode database */ |
| 5564 | message = "unknown Unicode character name"; |
| 5565 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5566 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 5567 | &chr, 0)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5568 | goto store; |
| 5569 | } |
| 5570 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5571 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5572 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5573 | errors, &errorHandler, |
| 5574 | "unicodeescape", message, |
| 5575 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5576 | &writer)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5577 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5578 | break; |
| 5579 | |
| 5580 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5581 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5582 | message = "\\ at end of string"; |
| 5583 | s--; |
| 5584 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5585 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5586 | errors, &errorHandler, |
| 5587 | "unicodeescape", message, |
| 5588 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5589 | &writer)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5590 | goto onError; |
| 5591 | } |
| 5592 | else { |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5593 | WRITECHAR('\\'); |
| 5594 | WRITECHAR(s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5595 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5596 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5597 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5598 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5599 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5600 | } |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5601 | #undef WRITECHAR |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5602 | |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5603 | Py_XDECREF(errorHandler); |
| 5604 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5605 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5606 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5607 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5608 | PyErr_SetString( |
| 5609 | PyExc_UnicodeError, |
| 5610 | "\\N escapes not supported (can't load unicodedata module)" |
| 5611 | ); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5612 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5613 | Py_XDECREF(errorHandler); |
| 5614 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5615 | return NULL; |
| 5616 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5617 | onError: |
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); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5621 | return NULL; |
| 5622 | } |
| 5623 | |
| 5624 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5625 | |
| 5626 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5627 | appropriate. |
| 5628 | |
| 5629 | */ |
| 5630 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5631 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5632 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5633 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5634 | Py_ssize_t i, len; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5635 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5636 | char *p; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5637 | int kind; |
| 5638 | void *data; |
| 5639 | Py_ssize_t expandsize = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5640 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5641 | /* Initial allocation is based on the longest-possible character |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5642 | escape. |
| 5643 | |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 5644 | For UCS1 strings it's '\xxx', 4 bytes per source character. |
| 5645 | For UCS2 strings it's '\uxxxx', 6 bytes per source character. |
| 5646 | For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5647 | */ |
| 5648 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5649 | if (!PyUnicode_Check(unicode)) { |
| 5650 | PyErr_BadArgument(); |
| 5651 | return NULL; |
| 5652 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5653 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5654 | return NULL; |
| 5655 | len = PyUnicode_GET_LENGTH(unicode); |
| 5656 | kind = PyUnicode_KIND(unicode); |
| 5657 | data = PyUnicode_DATA(unicode); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 5658 | switch (kind) { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5659 | case PyUnicode_1BYTE_KIND: expandsize = 4; break; |
| 5660 | case PyUnicode_2BYTE_KIND: expandsize = 6; break; |
| 5661 | case PyUnicode_4BYTE_KIND: expandsize = 10; break; |
| 5662 | } |
| 5663 | |
| 5664 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5665 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5666 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5667 | if (len > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5668 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5669 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5670 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5671 | 2 |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5672 | + expandsize*len |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5673 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5674 | if (repr == NULL) |
| 5675 | return NULL; |
| 5676 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5677 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5678 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5679 | for (i = 0; i < len; i++) { |
Victor Stinner | 3326cb6 | 2011-11-10 20:15:25 +0100 | [diff] [blame] | 5680 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5681 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5682 | /* Escape backslashes */ |
| 5683 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 | *p++ = '\\'; |
| 5685 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5686 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5687 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5688 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5689 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5690 | else if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5691 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5692 | *p++ = '\\'; |
| 5693 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5694 | *p++ = Py_hexdigits[(ch >> 28) & 0x0000000F]; |
| 5695 | *p++ = Py_hexdigits[(ch >> 24) & 0x0000000F]; |
| 5696 | *p++ = Py_hexdigits[(ch >> 20) & 0x0000000F]; |
| 5697 | *p++ = Py_hexdigits[(ch >> 16) & 0x0000000F]; |
| 5698 | *p++ = Py_hexdigits[(ch >> 12) & 0x0000000F]; |
| 5699 | *p++ = Py_hexdigits[(ch >> 8) & 0x0000000F]; |
| 5700 | *p++ = Py_hexdigits[(ch >> 4) & 0x0000000F]; |
| 5701 | *p++ = Py_hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5702 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5703 | } |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5704 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5705 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5706 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5707 | *p++ = '\\'; |
| 5708 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5709 | *p++ = Py_hexdigits[(ch >> 12) & 0x000F]; |
| 5710 | *p++ = Py_hexdigits[(ch >> 8) & 0x000F]; |
| 5711 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5712 | *p++ = Py_hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5713 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5714 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5715 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5716 | else if (ch == '\t') { |
| 5717 | *p++ = '\\'; |
| 5718 | *p++ = 't'; |
| 5719 | } |
| 5720 | else if (ch == '\n') { |
| 5721 | *p++ = '\\'; |
| 5722 | *p++ = 'n'; |
| 5723 | } |
| 5724 | else if (ch == '\r') { |
| 5725 | *p++ = '\\'; |
| 5726 | *p++ = 'r'; |
| 5727 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5728 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5729 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5730 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5731 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5732 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5733 | *p++ = Py_hexdigits[(ch >> 4) & 0x000F]; |
| 5734 | *p++ = Py_hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5735 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5736 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | /* Copy everything else as-is */ |
| 5738 | else |
| 5739 | *p++ = (char) ch; |
| 5740 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5742 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5743 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5744 | return NULL; |
| 5745 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5748 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5749 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 5750 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5752 | PyObject *result; |
| 5753 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5754 | if (tmp == NULL) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5755 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5756 | result = PyUnicode_AsUnicodeEscapeString(tmp); |
| 5757 | Py_DECREF(tmp); |
| 5758 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5759 | } |
| 5760 | |
| 5761 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5762 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5763 | PyObject * |
| 5764 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5765 | Py_ssize_t size, |
| 5766 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5767 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5768 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5769 | Py_ssize_t startinpos; |
| 5770 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5771 | _PyUnicodeWriter writer; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5772 | const char *end; |
| 5773 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5774 | PyObject *errorHandler = NULL; |
| 5775 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5776 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5777 | if (size == 0) { |
| 5778 | Py_INCREF(unicode_empty); |
| 5779 | return unicode_empty; |
| 5780 | } |
| 5781 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5782 | /* Escaped strings will always be longer than the resulting |
| 5783 | 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] | 5784 | length after conversion to the true value. (But decoding error |
| 5785 | handler might have to resize the string) */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5786 | _PyUnicodeWriter_Init(&writer, 1); |
| 5787 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5788 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5789 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | end = s + size; |
| 5791 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5792 | unsigned char c; |
| 5793 | Py_UCS4 x; |
| 5794 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5795 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5796 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5797 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5798 | if (*s != '\\') { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5799 | x = (unsigned char)*s++; |
| 5800 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5801 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5802 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5803 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5804 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5805 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5806 | startinpos = s-starts; |
| 5807 | |
| 5808 | /* \u-escapes are only interpreted iff the number of leading |
| 5809 | backslashes if odd */ |
| 5810 | bs = s; |
| 5811 | for (;s < end;) { |
| 5812 | if (*s != '\\') |
| 5813 | break; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5814 | x = (unsigned char)*s++; |
| 5815 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5816 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5817 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5818 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5819 | } |
| 5820 | if (((s - bs) & 1) == 0 || |
| 5821 | s >= end || |
| 5822 | (*s != 'u' && *s != 'U')) { |
| 5823 | continue; |
| 5824 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5825 | writer.pos--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5826 | count = *s=='u' ? 4 : 8; |
| 5827 | s++; |
| 5828 | |
| 5829 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5830 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5831 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5832 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5833 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5834 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5835 | errors, &errorHandler, |
| 5836 | "rawunicodeescape", "truncated \\uXXXX", |
| 5837 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5838 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5839 | goto onError; |
| 5840 | goto nextByte; |
| 5841 | } |
| 5842 | x = (x<<4) & ~0xF; |
| 5843 | if (c >= '0' && c <= '9') |
| 5844 | x += c - '0'; |
| 5845 | else if (c >= 'a' && c <= 'f') |
| 5846 | x += 10 + c - 'a'; |
| 5847 | else |
| 5848 | x += 10 + c - 'A'; |
| 5849 | } |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5850 | if (x <= MAX_UNICODE) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5851 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 5852 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5853 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 5854 | writer.pos++; |
| 5855 | } |
| 5856 | else { |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5857 | endinpos = s-starts; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5858 | if (unicode_decode_call_errorhandler_writer( |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5859 | errors, &errorHandler, |
| 5860 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5861 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5862 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5863 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5864 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5865 | nextByte: |
| 5866 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5867 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5868 | Py_XDECREF(errorHandler); |
| 5869 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5870 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5871 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5872 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5873 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5874 | Py_XDECREF(errorHandler); |
| 5875 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 | return NULL; |
| 5877 | } |
| 5878 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5879 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5880 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5881 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5882 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5883 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5884 | char *p; |
| 5885 | char *q; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5886 | Py_ssize_t expandsize, pos; |
| 5887 | int kind; |
| 5888 | void *data; |
| 5889 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5891 | if (!PyUnicode_Check(unicode)) { |
| 5892 | PyErr_BadArgument(); |
| 5893 | return NULL; |
| 5894 | } |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 5895 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5896 | return NULL; |
| 5897 | kind = PyUnicode_KIND(unicode); |
| 5898 | data = PyUnicode_DATA(unicode); |
| 5899 | len = PyUnicode_GET_LENGTH(unicode); |
Benjamin Peterson | 1518e87 | 2011-11-23 10:44:52 -0600 | [diff] [blame] | 5900 | /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6 |
| 5901 | bytes, and 1 byte characters 4. */ |
| 5902 | expandsize = kind * 2 + 2; |
Victor Stinner | 0e36826 | 2011-11-10 20:12:49 +0100 | [diff] [blame] | 5903 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5904 | if (len > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5905 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5906 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5907 | repr = PyBytes_FromStringAndSize(NULL, expandsize * len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5908 | if (repr == NULL) |
| 5909 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5910 | if (len == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5911 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5913 | p = q = PyBytes_AS_STRING(repr); |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5914 | for (pos = 0; pos < len; pos++) { |
| 5915 | Py_UCS4 ch = PyUnicode_READ(kind, data, pos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5916 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5917 | if (ch >= 0x10000) { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 5918 | assert(ch <= MAX_UNICODE); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5919 | *p++ = '\\'; |
| 5920 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5921 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 5922 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 5923 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 5924 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 5925 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5926 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5927 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5928 | *p++ = Py_hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5929 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5930 | /* Map 16-bit characters to '\uxxxx' */ |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5931 | else if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5932 | *p++ = '\\'; |
| 5933 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 5934 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 5935 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 5936 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 5937 | *p++ = Py_hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5939 | /* Copy everything else as-is */ |
| 5940 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5941 | *p++ = (char) ch; |
| 5942 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5943 | |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5944 | assert(p > q); |
| 5945 | if (_PyBytes_Resize(&repr, p - q) < 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5946 | return NULL; |
| 5947 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5948 | } |
| 5949 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5950 | PyObject * |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5951 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
| 5952 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5953 | { |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5954 | PyObject *result; |
| 5955 | PyObject *tmp = PyUnicode_FromUnicode(s, size); |
| 5956 | if (tmp == NULL) |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5957 | return NULL; |
Martin v. Löwis | 1db7c13 | 2011-11-10 18:24:32 +0100 | [diff] [blame] | 5958 | result = PyUnicode_AsRawUnicodeEscapeString(tmp); |
| 5959 | Py_DECREF(tmp); |
| 5960 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5963 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 5964 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5965 | PyObject * |
| 5966 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5967 | Py_ssize_t size, |
| 5968 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5969 | { |
| 5970 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5971 | Py_ssize_t startinpos; |
| 5972 | Py_ssize_t endinpos; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5973 | _PyUnicodeWriter writer; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5974 | const char *end; |
| 5975 | const char *reason; |
| 5976 | PyObject *errorHandler = NULL; |
| 5977 | PyObject *exc = NULL; |
| 5978 | |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 5979 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 5980 | "unicode_internal codec has been deprecated", |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 5981 | 1)) |
| 5982 | return NULL; |
| 5983 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5984 | if (size == 0) { |
| 5985 | Py_INCREF(unicode_empty); |
| 5986 | return unicode_empty; |
| 5987 | } |
| 5988 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5989 | /* XXX overflow detection missing */ |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 5990 | _PyUnicodeWriter_Init(&writer, 0); |
| 5991 | 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] | 5992 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5993 | end = s + size; |
| 5994 | |
| 5995 | while (s < end) { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 5996 | Py_UNICODE uch; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 5997 | Py_UCS4 ch; |
| 5998 | /* We copy the raw representation one byte at a time because the |
| 5999 | pointer may be unaligned (see test_codeccallbacks). */ |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6000 | ((char *) &uch)[0] = s[0]; |
| 6001 | ((char *) &uch)[1] = s[1]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6002 | #ifdef Py_UNICODE_WIDE |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6003 | ((char *) &uch)[2] = s[2]; |
| 6004 | ((char *) &uch)[3] = s[3]; |
Antoine Pitrou | 44c6aff | 2011-11-11 02:59:42 +0100 | [diff] [blame] | 6005 | #endif |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6006 | ch = uch; |
| 6007 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6008 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6009 | some malformed UCS-4 data. */ |
| 6010 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6011 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6012 | ch > 0x10ffff || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6013 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6014 | end-s < Py_UNICODE_SIZE |
| 6015 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6016 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6017 | startinpos = s - starts; |
| 6018 | if (end-s < Py_UNICODE_SIZE) { |
| 6019 | endinpos = end-starts; |
| 6020 | reason = "truncated input"; |
| 6021 | } |
| 6022 | else { |
| 6023 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6024 | reason = "illegal code point (> 0x10FFFF)"; |
| 6025 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6026 | if (unicode_decode_call_errorhandler_writer( |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6027 | errors, &errorHandler, |
| 6028 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6029 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6030 | &writer)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6031 | goto onError; |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6032 | continue; |
| 6033 | } |
| 6034 | |
| 6035 | s += Py_UNICODE_SIZE; |
| 6036 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6037 | if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && s < end) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6038 | { |
Antoine Pitrou | 0290c7a | 2011-11-11 13:29:12 +0100 | [diff] [blame] | 6039 | Py_UNICODE uch2; |
| 6040 | ((char *) &uch2)[0] = s[0]; |
| 6041 | ((char *) &uch2)[1] = s[1]; |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6042 | if (Py_UNICODE_IS_LOW_SURROGATE(uch2)) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6043 | { |
Victor Stinner | 551ac95 | 2011-11-29 22:58:13 +0100 | [diff] [blame] | 6044 | ch = Py_UNICODE_JOIN_SURROGATES(uch, uch2); |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6045 | s += Py_UNICODE_SIZE; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6046 | } |
| 6047 | } |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6048 | #endif |
| 6049 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6050 | if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) |
Victor Stinner | 9f4b1e9 | 2011-11-10 20:56:30 +0100 | [diff] [blame] | 6051 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6052 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); |
| 6053 | writer.pos++; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6054 | } |
| 6055 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6056 | Py_XDECREF(errorHandler); |
| 6057 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6058 | return _PyUnicodeWriter_Finish(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6059 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6060 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6061 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6062 | Py_XDECREF(errorHandler); |
| 6063 | Py_XDECREF(exc); |
| 6064 | return NULL; |
| 6065 | } |
| 6066 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6067 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6068 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6069 | PyObject * |
| 6070 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6071 | Py_ssize_t size, |
| 6072 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6073 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6075 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | } |
| 6077 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6078 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6079 | static void |
| 6080 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6081 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6082 | PyObject *unicode, |
| 6083 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6084 | const char *reason) |
| 6085 | { |
| 6086 | if (*exceptionObject == NULL) { |
| 6087 | *exceptionObject = PyObject_CallFunction( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6088 | PyExc_UnicodeEncodeError, "sOnns", |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6089 | encoding, unicode, startpos, endpos, reason); |
| 6090 | } |
| 6091 | else { |
| 6092 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6093 | goto onError; |
| 6094 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6095 | goto onError; |
| 6096 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6097 | goto onError; |
| 6098 | return; |
| 6099 | onError: |
| 6100 | Py_DECREF(*exceptionObject); |
| 6101 | *exceptionObject = NULL; |
| 6102 | } |
| 6103 | } |
| 6104 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6105 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6106 | static void |
| 6107 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6108 | const char *encoding, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6109 | PyObject *unicode, |
| 6110 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6111 | const char *reason) |
| 6112 | { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6113 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 9e81668 | 2011-11-02 12:45:42 +0100 | [diff] [blame] | 6114 | encoding, unicode, startpos, endpos, reason); |
| 6115 | if (*exceptionObject != NULL) |
| 6116 | PyCodec_StrictErrors(*exceptionObject); |
| 6117 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6118 | |
| 6119 | /* error handling callback helper: |
| 6120 | build arguments, call the callback and check the arguments, |
| 6121 | put the result into newpos and return the replacement string, which |
| 6122 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6123 | static PyObject * |
| 6124 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6125 | PyObject **errorHandler, |
| 6126 | const char *encoding, const char *reason, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6127 | PyObject *unicode, PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6128 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6129 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6130 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6131 | 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] | 6132 | Py_ssize_t len; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6133 | PyObject *restuple; |
| 6134 | PyObject *resunicode; |
| 6135 | |
| 6136 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6137 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6138 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6139 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6142 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6143 | return NULL; |
| 6144 | len = PyUnicode_GET_LENGTH(unicode); |
| 6145 | |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6146 | make_encode_exception(exceptionObject, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6147 | encoding, unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6148 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6149 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6150 | |
| 6151 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6152 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6153 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6154 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6155 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6156 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6157 | Py_DECREF(restuple); |
| 6158 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6159 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6160 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6161 | &resunicode, newpos)) { |
| 6162 | Py_DECREF(restuple); |
| 6163 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6164 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6165 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6166 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6167 | Py_DECREF(restuple); |
| 6168 | return NULL; |
| 6169 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6170 | if (*newpos<0) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6171 | *newpos = len + *newpos; |
| 6172 | if (*newpos<0 || *newpos>len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6173 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6174 | Py_DECREF(restuple); |
| 6175 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6176 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6177 | Py_INCREF(resunicode); |
| 6178 | Py_DECREF(restuple); |
| 6179 | return resunicode; |
| 6180 | } |
| 6181 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6182 | static PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6183 | unicode_encode_ucs1(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6184 | const char *errors, |
Victor Stinner | fcd9653 | 2011-11-04 00:28:50 +0100 | [diff] [blame] | 6185 | unsigned int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6186 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6187 | /* input state */ |
| 6188 | Py_ssize_t pos=0, size; |
| 6189 | int kind; |
| 6190 | void *data; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6191 | /* output object */ |
| 6192 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6193 | /* pointer into the output */ |
| 6194 | char *str; |
| 6195 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6196 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6197 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6198 | 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] | 6199 | PyObject *errorHandler = NULL; |
| 6200 | PyObject *exc = NULL; |
| 6201 | /* the following variable is used for caching string comparisons |
| 6202 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6203 | int known_errorHandler = -1; |
| 6204 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6205 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6206 | return NULL; |
| 6207 | size = PyUnicode_GET_LENGTH(unicode); |
| 6208 | kind = PyUnicode_KIND(unicode); |
| 6209 | data = PyUnicode_DATA(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6210 | /* allocate enough for a simple encoding without |
| 6211 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6212 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6213 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6214 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6215 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6216 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6217 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6218 | ressize = size; |
| 6219 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6220 | while (pos < size) { |
| 6221 | Py_UCS4 c = PyUnicode_READ(kind, data, pos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6222 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6223 | /* can we encode this? */ |
| 6224 | if (c<limit) { |
| 6225 | /* no overflow check, because we know that the space is enough */ |
| 6226 | *str++ = (char)c; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6227 | ++pos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6228 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6229 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6230 | Py_ssize_t requiredsize; |
| 6231 | PyObject *repunicode; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6232 | Py_ssize_t repsize, newpos, respos, i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6233 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6234 | Py_ssize_t collstart = pos; |
| 6235 | Py_ssize_t collend = pos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6236 | /* find all unecodable characters */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6237 | while ((collend < size) && (PyUnicode_READ(kind, data, collend)>=limit)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6238 | ++collend; |
| 6239 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6240 | if (known_errorHandler==-1) { |
| 6241 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6242 | known_errorHandler = 1; |
| 6243 | else if (!strcmp(errors, "replace")) |
| 6244 | known_errorHandler = 2; |
| 6245 | else if (!strcmp(errors, "ignore")) |
| 6246 | known_errorHandler = 3; |
| 6247 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6248 | known_errorHandler = 4; |
| 6249 | else |
| 6250 | known_errorHandler = 0; |
| 6251 | } |
| 6252 | switch (known_errorHandler) { |
| 6253 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6254 | raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6255 | goto onError; |
| 6256 | case 2: /* replace */ |
| 6257 | while (collstart++<collend) |
| 6258 | *str++ = '?'; /* fall through */ |
| 6259 | case 3: /* ignore */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6260 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6261 | break; |
| 6262 | case 4: /* xmlcharrefreplace */ |
| 6263 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6264 | /* determine replacement size */ |
| 6265 | for (i = collstart, repsize = 0; i < collend; ++i) { |
| 6266 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 6267 | if (ch < 10) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6268 | repsize += 2+1+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6269 | else if (ch < 100) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6270 | repsize += 2+2+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6271 | else if (ch < 1000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6272 | repsize += 2+3+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6273 | else if (ch < 10000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6274 | repsize += 2+4+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6275 | else if (ch < 100000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6276 | repsize += 2+5+1; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6277 | else if (ch < 1000000) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6278 | repsize += 2+6+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6279 | else { |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 6280 | assert(ch <= MAX_UNICODE); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6281 | repsize += 2+7+1; |
Victor Stinner | 0d3721d | 2011-11-22 03:27:53 +0100 | [diff] [blame] | 6282 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6283 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6284 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6285 | if (requiredsize > ressize) { |
| 6286 | if (requiredsize<2*ressize) |
| 6287 | requiredsize = 2*ressize; |
| 6288 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6289 | goto onError; |
| 6290 | str = PyBytes_AS_STRING(res) + respos; |
| 6291 | ressize = requiredsize; |
| 6292 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6293 | /* generate replacement */ |
| 6294 | for (i = collstart; i < collend; ++i) { |
| 6295 | str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6296 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6297 | pos = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6298 | break; |
| 6299 | default: |
| 6300 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6301 | encoding, reason, unicode, &exc, |
| 6302 | collstart, collend, &newpos); |
| 6303 | if (repunicode == NULL || (PyUnicode_Check(repunicode) && |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 6304 | PyUnicode_READY(repunicode) == -1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6305 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6306 | if (PyBytes_Check(repunicode)) { |
| 6307 | /* Directly copy bytes result to output. */ |
| 6308 | repsize = PyBytes_Size(repunicode); |
| 6309 | if (repsize > 1) { |
| 6310 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6311 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6312 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6313 | Py_DECREF(repunicode); |
| 6314 | goto onError; |
| 6315 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6316 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6317 | ressize += repsize-1; |
| 6318 | } |
| 6319 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6320 | str += repsize; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6321 | pos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6322 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6323 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6324 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6325 | /* need more space? (at least enough for what we |
| 6326 | have+the replacement+the rest of the string, so |
| 6327 | we won't have to check space for encodable characters) */ |
| 6328 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6329 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 6330 | requiredsize = respos+repsize+(size-collend); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6331 | if (requiredsize > ressize) { |
| 6332 | if (requiredsize<2*ressize) |
| 6333 | requiredsize = 2*ressize; |
| 6334 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6335 | Py_DECREF(repunicode); |
| 6336 | goto onError; |
| 6337 | } |
| 6338 | str = PyBytes_AS_STRING(res) + respos; |
| 6339 | ressize = requiredsize; |
| 6340 | } |
| 6341 | /* check if there is anything unencodable in the replacement |
| 6342 | and copy it to the output */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6343 | for (i = 0; repsize-->0; ++i, ++str) { |
| 6344 | c = PyUnicode_READ_CHAR(repunicode, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6345 | if (c >= limit) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 6346 | raise_encode_exception(&exc, encoding, unicode, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6347 | pos, pos+1, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6348 | Py_DECREF(repunicode); |
| 6349 | goto onError; |
| 6350 | } |
| 6351 | *str = (char)c; |
| 6352 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6353 | pos = newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6354 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6355 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6356 | } |
| 6357 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6358 | /* Resize if we allocated to much */ |
| 6359 | size = str - PyBytes_AS_STRING(res); |
| 6360 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6361 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6362 | if (_PyBytes_Resize(&res, size) < 0) |
| 6363 | goto onError; |
| 6364 | } |
| 6365 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6366 | Py_XDECREF(errorHandler); |
| 6367 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6368 | return res; |
| 6369 | |
| 6370 | onError: |
| 6371 | Py_XDECREF(res); |
| 6372 | Py_XDECREF(errorHandler); |
| 6373 | Py_XDECREF(exc); |
| 6374 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6377 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6378 | PyObject * |
| 6379 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6380 | Py_ssize_t size, |
| 6381 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6382 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6383 | PyObject *result; |
| 6384 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6385 | if (unicode == NULL) |
| 6386 | return NULL; |
| 6387 | result = unicode_encode_ucs1(unicode, errors, 256); |
| 6388 | Py_DECREF(unicode); |
| 6389 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6392 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6393 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6394 | { |
| 6395 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6396 | PyErr_BadArgument(); |
| 6397 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6398 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6399 | if (PyUnicode_READY(unicode) == -1) |
| 6400 | return NULL; |
| 6401 | /* Fast path: if it is a one-byte string, construct |
| 6402 | bytes object directly. */ |
| 6403 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6404 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6405 | PyUnicode_GET_LENGTH(unicode)); |
| 6406 | /* Non-Latin-1 characters present. Defer to above function to |
| 6407 | raise the exception. */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6408 | return unicode_encode_ucs1(unicode, errors, 256); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6409 | } |
| 6410 | |
| 6411 | PyObject* |
| 6412 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6413 | { |
| 6414 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6415 | } |
| 6416 | |
| 6417 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6418 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6419 | PyObject * |
| 6420 | PyUnicode_DecodeASCII(const char *s, |
| 6421 | Py_ssize_t size, |
| 6422 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6423 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6424 | const char *starts = s; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6425 | _PyUnicodeWriter writer; |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 6426 | int kind; |
| 6427 | void *data; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6428 | Py_ssize_t startinpos; |
| 6429 | Py_ssize_t endinpos; |
| 6430 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6431 | const char *e; |
| 6432 | PyObject *errorHandler = NULL; |
| 6433 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6434 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6435 | if (size == 0) { |
| 6436 | Py_INCREF(unicode_empty); |
| 6437 | return unicode_empty; |
| 6438 | } |
| 6439 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6440 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Victor Stinner | 702c734 | 2011-10-05 13:50:52 +0200 | [diff] [blame] | 6441 | if (size == 1 && (unsigned char)s[0] < 128) |
| 6442 | return get_latin1_char((unsigned char)s[0]); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6443 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6444 | _PyUnicodeWriter_Init(&writer, 0); |
| 6445 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6446 | goto onError; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6447 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6448 | e = s + size; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6449 | data = writer.data; |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6450 | outpos = ascii_decode(s, e, (Py_UCS1 *)data); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6451 | writer.pos = outpos; |
| 6452 | if (writer.pos == size) |
| 6453 | return _PyUnicodeWriter_Finish(&writer); |
Antoine Pitrou | ca5f91b | 2012-05-10 16:36:02 +0200 | [diff] [blame] | 6454 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6455 | s += writer.pos; |
| 6456 | kind = writer.kind; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6457 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6458 | register unsigned char c = (unsigned char)*s; |
| 6459 | if (c < 128) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6460 | PyUnicode_WRITE(kind, data, writer.pos, c); |
| 6461 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6462 | ++s; |
| 6463 | } |
| 6464 | else { |
| 6465 | startinpos = s-starts; |
| 6466 | endinpos = startinpos + 1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6467 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6468 | errors, &errorHandler, |
| 6469 | "ascii", "ordinal not in range(128)", |
| 6470 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6471 | &writer)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6472 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6473 | kind = writer.kind; |
| 6474 | data = writer.data; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6475 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6476 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6477 | Py_XDECREF(errorHandler); |
| 6478 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6479 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6480 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6481 | onError: |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6482 | _PyUnicodeWriter_Dealloc(&writer); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6483 | Py_XDECREF(errorHandler); |
| 6484 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6485 | return NULL; |
| 6486 | } |
| 6487 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6488 | /* Deprecated */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6489 | PyObject * |
| 6490 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6491 | Py_ssize_t size, |
| 6492 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6493 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6494 | PyObject *result; |
| 6495 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 6496 | if (unicode == NULL) |
| 6497 | return NULL; |
| 6498 | result = unicode_encode_ucs1(unicode, errors, 128); |
| 6499 | Py_DECREF(unicode); |
| 6500 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6501 | } |
| 6502 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6503 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6504 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6505 | { |
| 6506 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6507 | PyErr_BadArgument(); |
| 6508 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6509 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6510 | if (PyUnicode_READY(unicode) == -1) |
| 6511 | return NULL; |
| 6512 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6513 | directly. Else defer to above function to raise the exception. */ |
| 6514 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6515 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6516 | PyUnicode_GET_LENGTH(unicode)); |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 6517 | return unicode_encode_ucs1(unicode, errors, 128); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6518 | } |
| 6519 | |
| 6520 | PyObject * |
| 6521 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6522 | { |
| 6523 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6524 | } |
| 6525 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6526 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6527 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6528 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6529 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6530 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6531 | #define NEED_RETRY |
| 6532 | #endif |
| 6533 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6534 | #ifndef WC_ERR_INVALID_CHARS |
| 6535 | # define WC_ERR_INVALID_CHARS 0x0080 |
| 6536 | #endif |
| 6537 | |
| 6538 | static char* |
| 6539 | code_page_name(UINT code_page, PyObject **obj) |
| 6540 | { |
| 6541 | *obj = NULL; |
| 6542 | if (code_page == CP_ACP) |
| 6543 | return "mbcs"; |
| 6544 | if (code_page == CP_UTF7) |
| 6545 | return "CP_UTF7"; |
| 6546 | if (code_page == CP_UTF8) |
| 6547 | return "CP_UTF8"; |
| 6548 | |
| 6549 | *obj = PyBytes_FromFormat("cp%u", code_page); |
| 6550 | if (*obj == NULL) |
| 6551 | return NULL; |
| 6552 | return PyBytes_AS_STRING(*obj); |
| 6553 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6554 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6555 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6556 | is_dbcs_lead_byte(UINT code_page, const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6557 | { |
| 6558 | const char *curr = s + offset; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6559 | const char *prev; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6560 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6561 | if (!IsDBCSLeadByteEx(code_page, *curr)) |
| 6562 | return 0; |
| 6563 | |
| 6564 | prev = CharPrevExA(code_page, s, curr, 0); |
| 6565 | if (prev == curr) |
| 6566 | return 1; |
| 6567 | /* FIXME: This code is limited to "true" double-byte encodings, |
| 6568 | as it assumes an incomplete character consists of a single |
| 6569 | byte. */ |
| 6570 | if (curr - prev == 2) |
| 6571 | return 1; |
| 6572 | if (!IsDBCSLeadByteEx(code_page, *prev)) |
| 6573 | return 1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6574 | return 0; |
| 6575 | } |
| 6576 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6577 | static DWORD |
| 6578 | decode_code_page_flags(UINT code_page) |
| 6579 | { |
| 6580 | if (code_page == CP_UTF7) { |
| 6581 | /* The CP_UTF7 decoder only supports flags=0 */ |
| 6582 | return 0; |
| 6583 | } |
| 6584 | else |
| 6585 | return MB_ERR_INVALID_CHARS; |
| 6586 | } |
| 6587 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6588 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6589 | * Decode a byte string from a Windows code page into unicode object in strict |
| 6590 | * mode. |
| 6591 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6592 | * Returns consumed size if succeed, returns -2 on decode error, or raise an |
| 6593 | * OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6594 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6595 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6596 | decode_code_page_strict(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6597 | PyObject **v, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6598 | const char *in, |
| 6599 | int insize) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6600 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6601 | const DWORD flags = decode_code_page_flags(code_page); |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6602 | wchar_t *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6603 | DWORD outsize; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6604 | |
| 6605 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6606 | assert(insize > 0); |
| 6607 | outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0); |
| 6608 | if (outsize <= 0) |
| 6609 | goto error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6610 | |
| 6611 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6612 | /* Create unicode object */ |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6613 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6614 | *v = (PyObject*)_PyUnicode_New(outsize); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6615 | if (*v == NULL) |
| 6616 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6617 | out = PyUnicode_AS_UNICODE(*v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6618 | } |
| 6619 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6620 | /* Extend unicode object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6621 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6622 | if (unicode_resize(v, n + outsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6623 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6624 | out = PyUnicode_AS_UNICODE(*v) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6625 | } |
| 6626 | |
| 6627 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6628 | outsize = MultiByteToWideChar(code_page, flags, in, insize, out, outsize); |
| 6629 | if (outsize <= 0) |
| 6630 | goto error; |
| 6631 | return insize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6632 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6633 | error: |
| 6634 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6635 | return -2; |
| 6636 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6637 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6638 | } |
| 6639 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6640 | /* |
| 6641 | * Decode a byte string from a code page into unicode object with an error |
| 6642 | * handler. |
| 6643 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6644 | * Returns consumed size if succeed, or raise an OSError or |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6645 | * UnicodeDecodeError exception and returns -1 on error. |
| 6646 | */ |
| 6647 | static int |
| 6648 | decode_code_page_errors(UINT code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6649 | PyObject **v, |
| 6650 | const char *in, const int size, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6651 | const char *errors) |
| 6652 | { |
| 6653 | const char *startin = in; |
| 6654 | const char *endin = in + size; |
| 6655 | const DWORD flags = decode_code_page_flags(code_page); |
| 6656 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 6657 | 2000 English version of the message. */ |
| 6658 | const char *reason = "No mapping for the Unicode character exists " |
| 6659 | "in the target code page."; |
| 6660 | /* each step cannot decode more than 1 character, but a character can be |
| 6661 | represented as a surrogate pair */ |
| 6662 | wchar_t buffer[2], *startout, *out; |
| 6663 | int insize, outsize; |
| 6664 | PyObject *errorHandler = NULL; |
| 6665 | PyObject *exc = NULL; |
| 6666 | PyObject *encoding_obj = NULL; |
| 6667 | char *encoding; |
| 6668 | DWORD err; |
| 6669 | int ret = -1; |
| 6670 | |
| 6671 | assert(size > 0); |
| 6672 | |
| 6673 | encoding = code_page_name(code_page, &encoding_obj); |
| 6674 | if (encoding == NULL) |
| 6675 | return -1; |
| 6676 | |
| 6677 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 6678 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, then we raise a |
| 6679 | UnicodeDecodeError. */ |
| 6680 | make_decode_exception(&exc, encoding, in, size, 0, 0, reason); |
| 6681 | if (exc != NULL) { |
| 6682 | PyCodec_StrictErrors(exc); |
| 6683 | Py_CLEAR(exc); |
| 6684 | } |
| 6685 | goto error; |
| 6686 | } |
| 6687 | |
| 6688 | if (*v == NULL) { |
| 6689 | /* Create unicode object */ |
| 6690 | if (size > PY_SSIZE_T_MAX / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6691 | PyErr_NoMemory(); |
| 6692 | goto error; |
| 6693 | } |
Victor Stinner | ab59594 | 2011-12-17 04:59:06 +0100 | [diff] [blame] | 6694 | /* FIXME: don't use _PyUnicode_New(), but allocate a wchar_t* buffer */ |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6695 | *v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer)); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6696 | if (*v == NULL) |
| 6697 | goto error; |
| 6698 | startout = PyUnicode_AS_UNICODE(*v); |
| 6699 | } |
| 6700 | else { |
| 6701 | /* Extend unicode object */ |
| 6702 | Py_ssize_t n = PyUnicode_GET_SIZE(*v); |
| 6703 | if (size > (PY_SSIZE_T_MAX - n) / (Py_ssize_t)Py_ARRAY_LENGTH(buffer)) { |
| 6704 | PyErr_NoMemory(); |
| 6705 | goto error; |
| 6706 | } |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6707 | if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6708 | goto error; |
| 6709 | startout = PyUnicode_AS_UNICODE(*v) + n; |
| 6710 | } |
| 6711 | |
| 6712 | /* Decode the byte string character per character */ |
| 6713 | out = startout; |
| 6714 | while (in < endin) |
| 6715 | { |
| 6716 | /* Decode a character */ |
| 6717 | insize = 1; |
| 6718 | do |
| 6719 | { |
| 6720 | outsize = MultiByteToWideChar(code_page, flags, |
| 6721 | in, insize, |
| 6722 | buffer, Py_ARRAY_LENGTH(buffer)); |
| 6723 | if (outsize > 0) |
| 6724 | break; |
| 6725 | err = GetLastError(); |
| 6726 | if (err != ERROR_NO_UNICODE_TRANSLATION |
| 6727 | && err != ERROR_INSUFFICIENT_BUFFER) |
| 6728 | { |
| 6729 | PyErr_SetFromWindowsErr(0); |
| 6730 | goto error; |
| 6731 | } |
| 6732 | insize++; |
| 6733 | } |
| 6734 | /* 4=maximum length of a UTF-8 sequence */ |
| 6735 | while (insize <= 4 && (in + insize) <= endin); |
| 6736 | |
| 6737 | if (outsize <= 0) { |
| 6738 | Py_ssize_t startinpos, endinpos, outpos; |
| 6739 | |
| 6740 | startinpos = in - startin; |
| 6741 | endinpos = startinpos + 1; |
| 6742 | outpos = out - PyUnicode_AS_UNICODE(*v); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 6743 | if (unicode_decode_call_errorhandler_wchar( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6744 | errors, &errorHandler, |
| 6745 | encoding, reason, |
| 6746 | &startin, &endin, &startinpos, &endinpos, &exc, &in, |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6747 | v, &outpos)) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6748 | { |
| 6749 | goto error; |
| 6750 | } |
Victor Stinner | 596a6c4 | 2011-11-09 00:02:18 +0100 | [diff] [blame] | 6751 | out = PyUnicode_AS_UNICODE(*v) + outpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6752 | } |
| 6753 | else { |
| 6754 | in += insize; |
| 6755 | memcpy(out, buffer, outsize * sizeof(wchar_t)); |
| 6756 | out += outsize; |
| 6757 | } |
| 6758 | } |
| 6759 | |
| 6760 | /* write a NUL character at the end */ |
| 6761 | *out = 0; |
| 6762 | |
| 6763 | /* Extend unicode object */ |
| 6764 | outsize = out - startout; |
| 6765 | assert(outsize <= PyUnicode_WSTR_LENGTH(*v)); |
Victor Stinner | 16e6a80 | 2011-12-12 13:24:15 +0100 | [diff] [blame] | 6766 | if (unicode_resize(v, outsize) < 0) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6767 | goto error; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6768 | ret = size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6769 | |
| 6770 | error: |
| 6771 | Py_XDECREF(encoding_obj); |
| 6772 | Py_XDECREF(errorHandler); |
| 6773 | Py_XDECREF(exc); |
| 6774 | return ret; |
| 6775 | } |
| 6776 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6777 | static PyObject * |
| 6778 | decode_code_page_stateful(int code_page, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6779 | const char *s, Py_ssize_t size, |
| 6780 | const char *errors, Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6781 | { |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6782 | PyObject *v = NULL; |
| 6783 | int chunk_size, final, converted, done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6784 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6785 | if (code_page < 0) { |
| 6786 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 6787 | return NULL; |
| 6788 | } |
| 6789 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6790 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6791 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6792 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6793 | do |
| 6794 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6795 | #ifdef NEED_RETRY |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6796 | if (size > INT_MAX) { |
| 6797 | chunk_size = INT_MAX; |
| 6798 | final = 0; |
| 6799 | done = 0; |
| 6800 | } |
| 6801 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6802 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6803 | { |
| 6804 | chunk_size = (int)size; |
| 6805 | final = (consumed == NULL); |
| 6806 | done = 1; |
| 6807 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6808 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6809 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6810 | if (!final && is_dbcs_lead_byte(code_page, s, chunk_size - 1)) |
| 6811 | --chunk_size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6812 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6813 | if (chunk_size == 0 && done) { |
| 6814 | if (v != NULL) |
| 6815 | break; |
| 6816 | Py_INCREF(unicode_empty); |
| 6817 | return unicode_empty; |
| 6818 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6819 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 6820 | |
| 6821 | converted = decode_code_page_strict(code_page, &v, |
| 6822 | s, chunk_size); |
| 6823 | if (converted == -2) |
| 6824 | converted = decode_code_page_errors(code_page, &v, |
| 6825 | s, chunk_size, |
| 6826 | errors); |
| 6827 | assert(converted != 0); |
| 6828 | |
| 6829 | if (converted < 0) { |
| 6830 | Py_XDECREF(v); |
| 6831 | return NULL; |
| 6832 | } |
| 6833 | |
| 6834 | if (consumed) |
| 6835 | *consumed += converted; |
| 6836 | |
| 6837 | s += converted; |
| 6838 | size -= converted; |
| 6839 | } while (!done); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6840 | |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 6841 | return unicode_result(v); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6842 | } |
| 6843 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6844 | PyObject * |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6845 | PyUnicode_DecodeCodePageStateful(int code_page, |
| 6846 | const char *s, |
| 6847 | Py_ssize_t size, |
| 6848 | const char *errors, |
| 6849 | Py_ssize_t *consumed) |
| 6850 | { |
| 6851 | return decode_code_page_stateful(code_page, s, size, errors, consumed); |
| 6852 | } |
| 6853 | |
| 6854 | PyObject * |
| 6855 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6856 | Py_ssize_t size, |
| 6857 | const char *errors, |
| 6858 | Py_ssize_t *consumed) |
| 6859 | { |
| 6860 | return decode_code_page_stateful(CP_ACP, s, size, errors, consumed); |
| 6861 | } |
| 6862 | |
| 6863 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6864 | PyUnicode_DecodeMBCS(const char *s, |
| 6865 | Py_ssize_t size, |
| 6866 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6867 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6868 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6869 | } |
| 6870 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6871 | static DWORD |
| 6872 | encode_code_page_flags(UINT code_page, const char *errors) |
| 6873 | { |
| 6874 | if (code_page == CP_UTF8) { |
| 6875 | if (winver.dwMajorVersion >= 6) |
| 6876 | /* CP_UTF8 supports WC_ERR_INVALID_CHARS on Windows Vista |
| 6877 | and later */ |
| 6878 | return WC_ERR_INVALID_CHARS; |
| 6879 | else |
| 6880 | /* CP_UTF8 only supports flags=0 on Windows older than Vista */ |
| 6881 | return 0; |
| 6882 | } |
| 6883 | else if (code_page == CP_UTF7) { |
| 6884 | /* CP_UTF7 only supports flags=0 */ |
| 6885 | return 0; |
| 6886 | } |
| 6887 | else { |
| 6888 | if (errors != NULL && strcmp(errors, "replace") == 0) |
| 6889 | return 0; |
| 6890 | else |
| 6891 | return WC_NO_BEST_FIT_CHARS; |
| 6892 | } |
| 6893 | } |
| 6894 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6895 | /* |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6896 | * Encode a Unicode string to a Windows code page into a byte string in strict |
| 6897 | * mode. |
| 6898 | * |
| 6899 | * Returns consumed characters if succeed, returns -2 on encode error, or raise |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6900 | * an OSError and returns -1 on other error. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6901 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6902 | static int |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6903 | encode_code_page_strict(UINT code_page, PyObject **outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6904 | PyObject *unicode, Py_ssize_t offset, int len, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6905 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6906 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6907 | BOOL usedDefaultChar = FALSE; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6908 | BOOL *pusedDefaultChar = &usedDefaultChar; |
| 6909 | int outsize; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6910 | PyObject *exc = NULL; |
Victor Stinner | 24729f3 | 2011-11-10 20:31:37 +0100 | [diff] [blame] | 6911 | wchar_t *p; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6912 | Py_ssize_t size; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6913 | const DWORD flags = encode_code_page_flags(code_page, NULL); |
| 6914 | char *out; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6915 | /* Create a substring so that we can get the UTF-16 representation |
| 6916 | of just the slice under consideration. */ |
| 6917 | PyObject *substring; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6918 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6919 | assert(len > 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6920 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6921 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6922 | pusedDefaultChar = &usedDefaultChar; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6923 | else |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6924 | pusedDefaultChar = NULL; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6925 | |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6926 | substring = PyUnicode_Substring(unicode, offset, offset+len); |
| 6927 | if (substring == NULL) |
| 6928 | return -1; |
| 6929 | p = PyUnicode_AsUnicodeAndSize(substring, &size); |
| 6930 | if (p == NULL) { |
| 6931 | Py_DECREF(substring); |
| 6932 | return -1; |
| 6933 | } |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 6934 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6935 | /* First get the size of the result */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6936 | outsize = WideCharToMultiByte(code_page, flags, |
| 6937 | p, size, |
| 6938 | NULL, 0, |
| 6939 | NULL, pusedDefaultChar); |
| 6940 | if (outsize <= 0) |
| 6941 | goto error; |
| 6942 | /* If we used a default char, then we failed! */ |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6943 | if (pusedDefaultChar && *pusedDefaultChar) { |
| 6944 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6945 | return -2; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6946 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6947 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6948 | if (*outbytes == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6949 | /* Create string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6950 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6951 | if (*outbytes == NULL) { |
| 6952 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6953 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6954 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6955 | out = PyBytes_AS_STRING(*outbytes); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6956 | } |
| 6957 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6958 | /* Extend string object */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6959 | const Py_ssize_t n = PyBytes_Size(*outbytes); |
| 6960 | if (outsize > PY_SSIZE_T_MAX - n) { |
| 6961 | PyErr_NoMemory(); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6962 | Py_DECREF(substring); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6963 | return -1; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6964 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6965 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) { |
| 6966 | Py_DECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6967 | return -1; |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6968 | } |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6969 | out = PyBytes_AS_STRING(*outbytes) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6970 | } |
| 6971 | |
| 6972 | /* Do the conversion */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6973 | outsize = WideCharToMultiByte(code_page, flags, |
| 6974 | p, size, |
| 6975 | out, outsize, |
| 6976 | NULL, pusedDefaultChar); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6977 | Py_CLEAR(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6978 | if (outsize <= 0) |
| 6979 | goto error; |
| 6980 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6981 | return -2; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6982 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6983 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6984 | error: |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 6985 | Py_XDECREF(substring); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6986 | if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) |
| 6987 | return -2; |
| 6988 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6989 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6990 | } |
| 6991 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6992 | /* |
| 6993 | * Encode a Unicode string to a Windows code page into a byte string using a |
| 6994 | * error handler. |
| 6995 | * |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 6996 | * Returns consumed characters if succeed, or raise an OSError and returns |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 6997 | * -1 on other error. |
| 6998 | */ |
| 6999 | static int |
| 7000 | encode_code_page_errors(UINT code_page, PyObject **outbytes, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7001 | PyObject *unicode, Py_ssize_t unicode_offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7002 | Py_ssize_t insize, const char* errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7003 | { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7004 | const DWORD flags = encode_code_page_flags(code_page, errors); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7005 | Py_ssize_t pos = unicode_offset; |
| 7006 | Py_ssize_t endin = unicode_offset + insize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7007 | /* Ideally, we should get reason from FormatMessage. This is the Windows |
| 7008 | 2000 English version of the message. */ |
| 7009 | const char *reason = "invalid character"; |
| 7010 | /* 4=maximum length of a UTF-8 sequence */ |
| 7011 | char buffer[4]; |
| 7012 | BOOL usedDefaultChar = FALSE, *pusedDefaultChar; |
| 7013 | Py_ssize_t outsize; |
| 7014 | char *out; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7015 | PyObject *errorHandler = NULL; |
| 7016 | PyObject *exc = NULL; |
| 7017 | PyObject *encoding_obj = NULL; |
| 7018 | char *encoding; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7019 | Py_ssize_t newpos, newoutsize; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7020 | PyObject *rep; |
| 7021 | int ret = -1; |
| 7022 | |
| 7023 | assert(insize > 0); |
| 7024 | |
| 7025 | encoding = code_page_name(code_page, &encoding_obj); |
| 7026 | if (encoding == NULL) |
| 7027 | return -1; |
| 7028 | |
| 7029 | if (errors == NULL || strcmp(errors, "strict") == 0) { |
| 7030 | /* The last error was ERROR_NO_UNICODE_TRANSLATION, |
| 7031 | then we raise a UnicodeEncodeError. */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7032 | make_encode_exception(&exc, encoding, unicode, 0, 0, reason); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7033 | if (exc != NULL) { |
| 7034 | PyCodec_StrictErrors(exc); |
| 7035 | Py_DECREF(exc); |
| 7036 | } |
| 7037 | Py_XDECREF(encoding_obj); |
| 7038 | return -1; |
| 7039 | } |
| 7040 | |
| 7041 | if (code_page != CP_UTF8 && code_page != CP_UTF7) |
| 7042 | pusedDefaultChar = &usedDefaultChar; |
| 7043 | else |
| 7044 | pusedDefaultChar = NULL; |
| 7045 | |
| 7046 | if (Py_ARRAY_LENGTH(buffer) > PY_SSIZE_T_MAX / insize) { |
| 7047 | PyErr_NoMemory(); |
| 7048 | goto error; |
| 7049 | } |
| 7050 | outsize = insize * Py_ARRAY_LENGTH(buffer); |
| 7051 | |
| 7052 | if (*outbytes == NULL) { |
| 7053 | /* Create string object */ |
| 7054 | *outbytes = PyBytes_FromStringAndSize(NULL, outsize); |
| 7055 | if (*outbytes == NULL) |
| 7056 | goto error; |
| 7057 | out = PyBytes_AS_STRING(*outbytes); |
| 7058 | } |
| 7059 | else { |
| 7060 | /* Extend string object */ |
| 7061 | Py_ssize_t n = PyBytes_Size(*outbytes); |
| 7062 | if (n > PY_SSIZE_T_MAX - outsize) { |
| 7063 | PyErr_NoMemory(); |
| 7064 | goto error; |
| 7065 | } |
| 7066 | if (_PyBytes_Resize(outbytes, n + outsize) < 0) |
| 7067 | goto error; |
| 7068 | out = PyBytes_AS_STRING(*outbytes) + n; |
| 7069 | } |
| 7070 | |
| 7071 | /* Encode the string character per character */ |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7072 | while (pos < endin) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7073 | { |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7074 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, pos); |
| 7075 | wchar_t chars[2]; |
| 7076 | int charsize; |
| 7077 | if (ch < 0x10000) { |
| 7078 | chars[0] = (wchar_t)ch; |
| 7079 | charsize = 1; |
| 7080 | } |
| 7081 | else { |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 7082 | chars[0] = Py_UNICODE_HIGH_SURROGATE(ch); |
| 7083 | chars[1] = Py_UNICODE_LOW_SURROGATE(ch); |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7084 | charsize = 2; |
| 7085 | } |
| 7086 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7087 | outsize = WideCharToMultiByte(code_page, flags, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7088 | chars, charsize, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7089 | buffer, Py_ARRAY_LENGTH(buffer), |
| 7090 | NULL, pusedDefaultChar); |
| 7091 | if (outsize > 0) { |
| 7092 | if (pusedDefaultChar == NULL || !(*pusedDefaultChar)) |
| 7093 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7094 | pos++; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7095 | memcpy(out, buffer, outsize); |
| 7096 | out += outsize; |
| 7097 | continue; |
| 7098 | } |
| 7099 | } |
| 7100 | else if (GetLastError() != ERROR_NO_UNICODE_TRANSLATION) { |
| 7101 | PyErr_SetFromWindowsErr(0); |
| 7102 | goto error; |
| 7103 | } |
| 7104 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7105 | rep = unicode_encode_call_errorhandler( |
| 7106 | errors, &errorHandler, encoding, reason, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7107 | unicode, &exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7108 | pos, pos + 1, &newpos); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7109 | if (rep == NULL) |
| 7110 | goto error; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7111 | pos = newpos; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7112 | |
| 7113 | if (PyBytes_Check(rep)) { |
| 7114 | outsize = PyBytes_GET_SIZE(rep); |
| 7115 | if (outsize != 1) { |
| 7116 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7117 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7118 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7119 | Py_DECREF(rep); |
| 7120 | goto error; |
| 7121 | } |
| 7122 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7123 | } |
| 7124 | memcpy(out, PyBytes_AS_STRING(rep), outsize); |
| 7125 | out += outsize; |
| 7126 | } |
| 7127 | else { |
| 7128 | Py_ssize_t i; |
| 7129 | enum PyUnicode_Kind kind; |
| 7130 | void *data; |
| 7131 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7132 | if (PyUnicode_READY(rep) == -1) { |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7133 | Py_DECREF(rep); |
| 7134 | goto error; |
| 7135 | } |
| 7136 | |
| 7137 | outsize = PyUnicode_GET_LENGTH(rep); |
| 7138 | if (outsize != 1) { |
| 7139 | Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes); |
| 7140 | newoutsize = PyBytes_GET_SIZE(*outbytes) + (outsize - 1); |
| 7141 | if (_PyBytes_Resize(outbytes, newoutsize) < 0) { |
| 7142 | Py_DECREF(rep); |
| 7143 | goto error; |
| 7144 | } |
| 7145 | out = PyBytes_AS_STRING(*outbytes) + offset; |
| 7146 | } |
| 7147 | kind = PyUnicode_KIND(rep); |
| 7148 | data = PyUnicode_DATA(rep); |
| 7149 | for (i=0; i < outsize; i++) { |
| 7150 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7151 | if (ch > 127) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7152 | raise_encode_exception(&exc, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7153 | encoding, unicode, |
| 7154 | pos, pos + 1, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7155 | "unable to encode error handler result to ASCII"); |
| 7156 | Py_DECREF(rep); |
| 7157 | goto error; |
| 7158 | } |
| 7159 | *out = (unsigned char)ch; |
| 7160 | out++; |
| 7161 | } |
| 7162 | } |
| 7163 | Py_DECREF(rep); |
| 7164 | } |
| 7165 | /* write a NUL byte */ |
| 7166 | *out = 0; |
| 7167 | outsize = out - PyBytes_AS_STRING(*outbytes); |
| 7168 | assert(outsize <= PyBytes_GET_SIZE(*outbytes)); |
| 7169 | if (_PyBytes_Resize(outbytes, outsize) < 0) |
| 7170 | goto error; |
| 7171 | ret = 0; |
| 7172 | |
| 7173 | error: |
| 7174 | Py_XDECREF(encoding_obj); |
| 7175 | Py_XDECREF(errorHandler); |
| 7176 | Py_XDECREF(exc); |
| 7177 | return ret; |
| 7178 | } |
| 7179 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7180 | static PyObject * |
| 7181 | encode_code_page(int code_page, |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7182 | PyObject *unicode, |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7183 | const char *errors) |
| 7184 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7185 | Py_ssize_t len; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7186 | PyObject *outbytes = NULL; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7187 | Py_ssize_t offset; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7188 | int chunk_len, ret, done; |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7189 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7190 | if (PyUnicode_READY(unicode) == -1) |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7191 | return NULL; |
| 7192 | len = PyUnicode_GET_LENGTH(unicode); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 7193 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7194 | if (code_page < 0) { |
| 7195 | PyErr_SetString(PyExc_ValueError, "invalid code page number"); |
| 7196 | return NULL; |
| 7197 | } |
| 7198 | |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7199 | if (len == 0) |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7200 | return PyBytes_FromStringAndSize(NULL, 0); |
| 7201 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7202 | offset = 0; |
| 7203 | do |
| 7204 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7205 | #ifdef NEED_RETRY |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7206 | /* 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] | 7207 | chunks. */ |
| 7208 | if (len > INT_MAX/2) { |
| 7209 | chunk_len = INT_MAX/2; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7210 | done = 0; |
| 7211 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7212 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7213 | #endif |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7214 | { |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7215 | chunk_len = (int)len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7216 | done = 1; |
| 7217 | } |
Victor Stinner | 2fc507f | 2011-11-04 20:06:39 +0100 | [diff] [blame] | 7218 | |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7219 | ret = encode_code_page_strict(code_page, &outbytes, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7220 | unicode, offset, chunk_len, |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7221 | errors); |
| 7222 | if (ret == -2) |
| 7223 | ret = encode_code_page_errors(code_page, &outbytes, |
| 7224 | unicode, offset, |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7225 | chunk_len, errors); |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7226 | if (ret < 0) { |
| 7227 | Py_XDECREF(outbytes); |
| 7228 | return NULL; |
| 7229 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7230 | |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7231 | offset += chunk_len; |
Martin v. Löwis | 3d32519 | 2011-11-04 18:23:06 +0100 | [diff] [blame] | 7232 | len -= chunk_len; |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 7233 | } while (!done); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7234 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7235 | return outbytes; |
| 7236 | } |
| 7237 | |
| 7238 | PyObject * |
| 7239 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 7240 | Py_ssize_t size, |
| 7241 | const char *errors) |
| 7242 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7243 | PyObject *unicode, *res; |
| 7244 | unicode = PyUnicode_FromUnicode(p, size); |
| 7245 | if (unicode == NULL) |
| 7246 | return NULL; |
| 7247 | res = encode_code_page(CP_ACP, unicode, errors); |
| 7248 | Py_DECREF(unicode); |
| 7249 | return res; |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 7250 | } |
| 7251 | |
| 7252 | PyObject * |
| 7253 | PyUnicode_EncodeCodePage(int code_page, |
| 7254 | PyObject *unicode, |
| 7255 | const char *errors) |
| 7256 | { |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7257 | return encode_code_page(code_page, unicode, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7258 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 7259 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7260 | PyObject * |
| 7261 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7262 | { |
| 7263 | if (!PyUnicode_Check(unicode)) { |
| 7264 | PyErr_BadArgument(); |
| 7265 | return NULL; |
| 7266 | } |
Victor Stinner | 7581cef | 2011-11-03 22:32:33 +0100 | [diff] [blame] | 7267 | return PyUnicode_EncodeCodePage(CP_ACP, unicode, NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 7268 | } |
| 7269 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7270 | #undef NEED_RETRY |
| 7271 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 7272 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 7273 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7274 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 7275 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7276 | PyObject * |
| 7277 | PyUnicode_DecodeCharmap(const char *s, |
| 7278 | Py_ssize_t size, |
| 7279 | PyObject *mapping, |
| 7280 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7281 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7282 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7283 | Py_ssize_t startinpos; |
| 7284 | Py_ssize_t endinpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7285 | const char *e; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7286 | _PyUnicodeWriter writer; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7287 | PyObject *errorHandler = NULL; |
| 7288 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7290 | /* Default to Latin-1 */ |
| 7291 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7292 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7293 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7294 | if (size == 0) { |
| 7295 | Py_INCREF(unicode_empty); |
| 7296 | return unicode_empty; |
| 7297 | } |
| 7298 | _PyUnicodeWriter_Init(&writer, 0); |
| 7299 | if (_PyUnicodeWriter_Prepare(&writer, size, 127) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7300 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7301 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7302 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7303 | if (PyUnicode_CheckExact(mapping)) { |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7304 | Py_ssize_t maplen; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7305 | enum PyUnicode_Kind mapkind; |
| 7306 | void *mapdata; |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7307 | Py_UCS4 x; |
| 7308 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7309 | if (PyUnicode_READY(mapping) == -1) |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7310 | return NULL; |
| 7311 | |
| 7312 | maplen = PyUnicode_GET_LENGTH(mapping); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7313 | mapdata = PyUnicode_DATA(mapping); |
| 7314 | mapkind = PyUnicode_KIND(mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7315 | while (s < e) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7316 | unsigned char ch; |
| 7317 | if (mapkind == PyUnicode_2BYTE_KIND && maplen >= 256) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7318 | enum PyUnicode_Kind outkind = writer.kind; |
| 7319 | void *outdata = writer.data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7320 | if (outkind == PyUnicode_1BYTE_KIND) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7321 | Py_UCS4 maxchar = writer.maxchar; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7322 | while (s < e) { |
| 7323 | unsigned char ch = *s; |
| 7324 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7325 | if (x > maxchar) |
| 7326 | goto Error; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7327 | PyUnicode_WRITE(PyUnicode_1BYTE_KIND, outdata, writer.pos, x); |
| 7328 | writer.pos++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7329 | ++s; |
| 7330 | } |
| 7331 | break; |
| 7332 | } |
| 7333 | else if (outkind == PyUnicode_2BYTE_KIND) { |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7334 | while (s < e) { |
| 7335 | unsigned char ch = *s; |
| 7336 | x = PyUnicode_READ(PyUnicode_2BYTE_KIND, mapdata, ch); |
| 7337 | if (x == 0xFFFE) |
| 7338 | goto Error; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7339 | PyUnicode_WRITE(PyUnicode_2BYTE_KIND, outdata, writer.pos, x); |
| 7340 | writer.pos++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7341 | ++s; |
| 7342 | } |
| 7343 | break; |
| 7344 | } |
| 7345 | } |
| 7346 | ch = *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7347 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7348 | if (ch < maplen) |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7349 | x = PyUnicode_READ(mapkind, mapdata, ch); |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7350 | else |
| 7351 | x = 0xfffe; /* invalid value */ |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7352 | Error: |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7353 | if (x == 0xfffe) |
| 7354 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7355 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7356 | startinpos = s-starts; |
| 7357 | endinpos = startinpos+1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7358 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7359 | errors, &errorHandler, |
| 7360 | "charmap", "character maps to <undefined>", |
| 7361 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7362 | &writer)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7363 | goto onError; |
| 7364 | } |
| 7365 | continue; |
| 7366 | } |
Victor Stinner | ebf3ba8 | 2011-11-10 20:30:22 +0100 | [diff] [blame] | 7367 | |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7368 | if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7369 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7370 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x); |
| 7371 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7372 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7373 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7374 | } |
| 7375 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7376 | while (s < e) { |
| 7377 | unsigned char ch = *s; |
| 7378 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 7379 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7380 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 7381 | w = PyLong_FromLong((long)ch); |
| 7382 | if (w == NULL) |
| 7383 | goto onError; |
| 7384 | x = PyObject_GetItem(mapping, w); |
| 7385 | Py_DECREF(w); |
| 7386 | if (x == NULL) { |
| 7387 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7388 | /* No mapping found means: mapping is undefined. */ |
| 7389 | PyErr_Clear(); |
| 7390 | x = Py_None; |
| 7391 | Py_INCREF(x); |
| 7392 | } else |
| 7393 | goto onError; |
| 7394 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7395 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7396 | /* Apply mapping */ |
| 7397 | if (PyLong_Check(x)) { |
| 7398 | long value = PyLong_AS_LONG(x); |
Antoine Pitrou | a1f7655 | 2012-09-23 20:00:04 +0200 | [diff] [blame] | 7399 | if (value < 0 || value > MAX_UNICODE) { |
| 7400 | PyErr_Format(PyExc_TypeError, |
| 7401 | "character mapping must be in range(0x%lx)", |
| 7402 | (unsigned long)MAX_UNICODE + 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7403 | Py_DECREF(x); |
| 7404 | goto onError; |
| 7405 | } |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7406 | |
| 7407 | if (_PyUnicodeWriter_Prepare(&writer, 1, value) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7408 | goto onError; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7409 | PyUnicode_WRITE(writer.kind, writer.data, writer.pos, value); |
| 7410 | writer.pos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7411 | } |
| 7412 | else if (x == Py_None) { |
| 7413 | /* undefined mapping */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7414 | startinpos = s-starts; |
| 7415 | endinpos = startinpos+1; |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7416 | if (unicode_decode_call_errorhandler_writer( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7417 | errors, &errorHandler, |
| 7418 | "charmap", "character maps to <undefined>", |
| 7419 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7420 | &writer)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7421 | Py_DECREF(x); |
| 7422 | goto onError; |
| 7423 | } |
| 7424 | Py_DECREF(x); |
| 7425 | continue; |
| 7426 | } |
| 7427 | else if (PyUnicode_Check(x)) { |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7428 | writer.overallocate = 1; |
| 7429 | if (_PyUnicodeWriter_WriteStr(&writer, x) == -1) |
Martin v. Löwis | e9b11c1 | 2011-11-08 17:35:34 +0100 | [diff] [blame] | 7430 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7431 | } |
| 7432 | else { |
| 7433 | /* wrong return value */ |
| 7434 | PyErr_SetString(PyExc_TypeError, |
| 7435 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7436 | Py_DECREF(x); |
| 7437 | goto onError; |
| 7438 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7439 | Py_DECREF(x); |
| 7440 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7441 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7442 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7443 | Py_XDECREF(errorHandler); |
| 7444 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7445 | return _PyUnicodeWriter_Finish(&writer); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7446 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7447 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7448 | Py_XDECREF(errorHandler); |
| 7449 | Py_XDECREF(exc); |
Victor Stinner | fc009ef | 2012-11-07 00:36:38 +0100 | [diff] [blame] | 7450 | _PyUnicodeWriter_Dealloc(&writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7451 | return NULL; |
| 7452 | } |
| 7453 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7454 | /* Charmap encoding: the lookup table */ |
| 7455 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7456 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7457 | PyObject_HEAD |
| 7458 | unsigned char level1[32]; |
| 7459 | int count2, count3; |
| 7460 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7461 | }; |
| 7462 | |
| 7463 | static PyObject* |
| 7464 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7465 | { |
| 7466 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7467 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7468 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7469 | } |
| 7470 | |
| 7471 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7472 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7473 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7474 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7475 | }; |
| 7476 | |
| 7477 | static void |
| 7478 | encoding_map_dealloc(PyObject* o) |
| 7479 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7480 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7481 | } |
| 7482 | |
| 7483 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7484 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7485 | "EncodingMap", /*tp_name*/ |
| 7486 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7487 | 0, /*tp_itemsize*/ |
| 7488 | /* methods */ |
| 7489 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7490 | 0, /*tp_print*/ |
| 7491 | 0, /*tp_getattr*/ |
| 7492 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7493 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7494 | 0, /*tp_repr*/ |
| 7495 | 0, /*tp_as_number*/ |
| 7496 | 0, /*tp_as_sequence*/ |
| 7497 | 0, /*tp_as_mapping*/ |
| 7498 | 0, /*tp_hash*/ |
| 7499 | 0, /*tp_call*/ |
| 7500 | 0, /*tp_str*/ |
| 7501 | 0, /*tp_getattro*/ |
| 7502 | 0, /*tp_setattro*/ |
| 7503 | 0, /*tp_as_buffer*/ |
| 7504 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7505 | 0, /*tp_doc*/ |
| 7506 | 0, /*tp_traverse*/ |
| 7507 | 0, /*tp_clear*/ |
| 7508 | 0, /*tp_richcompare*/ |
| 7509 | 0, /*tp_weaklistoffset*/ |
| 7510 | 0, /*tp_iter*/ |
| 7511 | 0, /*tp_iternext*/ |
| 7512 | encoding_map_methods, /*tp_methods*/ |
| 7513 | 0, /*tp_members*/ |
| 7514 | 0, /*tp_getset*/ |
| 7515 | 0, /*tp_base*/ |
| 7516 | 0, /*tp_dict*/ |
| 7517 | 0, /*tp_descr_get*/ |
| 7518 | 0, /*tp_descr_set*/ |
| 7519 | 0, /*tp_dictoffset*/ |
| 7520 | 0, /*tp_init*/ |
| 7521 | 0, /*tp_alloc*/ |
| 7522 | 0, /*tp_new*/ |
| 7523 | 0, /*tp_free*/ |
| 7524 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7525 | }; |
| 7526 | |
| 7527 | PyObject* |
| 7528 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7529 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7530 | PyObject *result; |
| 7531 | struct encoding_map *mresult; |
| 7532 | int i; |
| 7533 | int need_dict = 0; |
| 7534 | unsigned char level1[32]; |
| 7535 | unsigned char level2[512]; |
| 7536 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7537 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7538 | int kind; |
| 7539 | void *data; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7540 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7541 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7542 | |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7543 | if (!PyUnicode_Check(string) || !PyUnicode_GET_LENGTH(string)) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7544 | PyErr_BadArgument(); |
| 7545 | return NULL; |
| 7546 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7547 | kind = PyUnicode_KIND(string); |
| 7548 | data = PyUnicode_DATA(string); |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7549 | length = PyUnicode_GET_LENGTH(string); |
| 7550 | length = Py_MIN(length, 256); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7551 | memset(level1, 0xFF, sizeof level1); |
| 7552 | memset(level2, 0xFF, sizeof level2); |
| 7553 | |
| 7554 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7555 | or if there are non-BMP characters, we need to use |
| 7556 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7557 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7558 | need_dict = 1; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7559 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7560 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7561 | ch = PyUnicode_READ(kind, data, i); |
| 7562 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7563 | need_dict = 1; |
| 7564 | break; |
| 7565 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7566 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7567 | /* unmapped character */ |
| 7568 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7569 | l1 = ch >> 11; |
| 7570 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7571 | if (level1[l1] == 0xFF) |
| 7572 | level1[l1] = count2++; |
| 7573 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7574 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7575 | } |
| 7576 | |
| 7577 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7578 | need_dict = 1; |
| 7579 | |
| 7580 | if (need_dict) { |
| 7581 | PyObject *result = PyDict_New(); |
| 7582 | PyObject *key, *value; |
| 7583 | if (!result) |
| 7584 | return NULL; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7585 | for (i = 0; i < length; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7586 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7587 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7588 | if (!key || !value) |
| 7589 | goto failed1; |
| 7590 | if (PyDict_SetItem(result, key, value) == -1) |
| 7591 | goto failed1; |
| 7592 | Py_DECREF(key); |
| 7593 | Py_DECREF(value); |
| 7594 | } |
| 7595 | return result; |
| 7596 | failed1: |
| 7597 | Py_XDECREF(key); |
| 7598 | Py_XDECREF(value); |
| 7599 | Py_DECREF(result); |
| 7600 | return NULL; |
| 7601 | } |
| 7602 | |
| 7603 | /* Create a three-level trie */ |
| 7604 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7605 | 16*count2 + 128*count3 - 1); |
| 7606 | if (!result) |
| 7607 | return PyErr_NoMemory(); |
| 7608 | PyObject_Init(result, &EncodingMapType); |
| 7609 | mresult = (struct encoding_map*)result; |
| 7610 | mresult->count2 = count2; |
| 7611 | mresult->count3 = count3; |
| 7612 | mlevel1 = mresult->level1; |
| 7613 | mlevel2 = mresult->level23; |
| 7614 | mlevel3 = mresult->level23 + 16*count2; |
| 7615 | memcpy(mlevel1, level1, 32); |
| 7616 | memset(mlevel2, 0xFF, 16*count2); |
| 7617 | memset(mlevel3, 0, 128*count3); |
| 7618 | count3 = 0; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7619 | for (i = 1; i < length; i++) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7620 | int o1, o2, o3, i2, i3; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7621 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 7622 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7623 | /* unmapped character */ |
| 7624 | continue; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7625 | o1 = ch>>11; |
| 7626 | o2 = (ch>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7627 | i2 = 16*mlevel1[o1] + o2; |
| 7628 | if (mlevel2[i2] == 0xFF) |
| 7629 | mlevel2[i2] = count3++; |
Antoine Pitrou | aaefac7 | 2012-06-16 22:48:21 +0200 | [diff] [blame] | 7630 | o3 = ch & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7631 | i3 = 128*mlevel2[i2] + o3; |
| 7632 | mlevel3[i3] = i; |
| 7633 | } |
| 7634 | return result; |
| 7635 | } |
| 7636 | |
| 7637 | static int |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7638 | encoding_map_lookup(Py_UCS4 c, PyObject *mapping) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7639 | { |
| 7640 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7641 | int l1 = c>>11; |
| 7642 | int l2 = (c>>7) & 0xF; |
| 7643 | int l3 = c & 0x7F; |
| 7644 | int i; |
| 7645 | |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7646 | if (c > 0xFFFF) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7647 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7648 | if (c == 0) |
| 7649 | return 0; |
| 7650 | /* level 1*/ |
| 7651 | i = map->level1[l1]; |
| 7652 | if (i == 0xFF) { |
| 7653 | return -1; |
| 7654 | } |
| 7655 | /* level 2*/ |
| 7656 | i = map->level23[16*i+l2]; |
| 7657 | if (i == 0xFF) { |
| 7658 | return -1; |
| 7659 | } |
| 7660 | /* level 3 */ |
| 7661 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7662 | if (i == 0) { |
| 7663 | return -1; |
| 7664 | } |
| 7665 | return i; |
| 7666 | } |
| 7667 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7668 | /* Lookup the character ch in the mapping. If the character |
| 7669 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7670 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7671 | static PyObject * |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7672 | charmapencode_lookup(Py_UCS4 c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7673 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7674 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7675 | PyObject *x; |
| 7676 | |
| 7677 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7678 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7679 | x = PyObject_GetItem(mapping, w); |
| 7680 | Py_DECREF(w); |
| 7681 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7682 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7683 | /* No mapping found means: mapping is undefined. */ |
| 7684 | PyErr_Clear(); |
| 7685 | x = Py_None; |
| 7686 | Py_INCREF(x); |
| 7687 | return x; |
| 7688 | } else |
| 7689 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7690 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7691 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7692 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7693 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7694 | long value = PyLong_AS_LONG(x); |
| 7695 | if (value < 0 || value > 255) { |
| 7696 | PyErr_SetString(PyExc_TypeError, |
| 7697 | "character mapping must be in range(256)"); |
| 7698 | Py_DECREF(x); |
| 7699 | return NULL; |
| 7700 | } |
| 7701 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7702 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7703 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7704 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7705 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7706 | /* wrong return value */ |
| 7707 | PyErr_Format(PyExc_TypeError, |
| 7708 | "character mapping must return integer, bytes or None, not %.400s", |
| 7709 | x->ob_type->tp_name); |
| 7710 | Py_DECREF(x); |
| 7711 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7712 | } |
| 7713 | } |
| 7714 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7715 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7716 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7717 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7718 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7719 | /* exponentially overallocate to minimize reallocations */ |
| 7720 | if (requiredsize < 2*outsize) |
| 7721 | requiredsize = 2*outsize; |
| 7722 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7723 | return -1; |
| 7724 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7725 | } |
| 7726 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7727 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7728 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7729 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7730 | /* 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] | 7731 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7732 | space is available. Return a new reference to the object that |
| 7733 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7734 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7735 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7736 | static charmapencode_result |
Victor Stinner | 2216899 | 2011-11-20 17:09:18 +0100 | [diff] [blame] | 7737 | charmapencode_output(Py_UCS4 c, PyObject *mapping, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7738 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7739 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7740 | PyObject *rep; |
| 7741 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7742 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7743 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7744 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7745 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7746 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7747 | if (res == -1) |
| 7748 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7749 | if (outsize<requiredsize) |
| 7750 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7751 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7752 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7753 | outstart[(*outpos)++] = (char)res; |
| 7754 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7755 | } |
| 7756 | |
| 7757 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7758 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7759 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7760 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7761 | Py_DECREF(rep); |
| 7762 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7763 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7764 | if (PyLong_Check(rep)) { |
| 7765 | Py_ssize_t requiredsize = *outpos+1; |
| 7766 | if (outsize<requiredsize) |
| 7767 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7768 | Py_DECREF(rep); |
| 7769 | return enc_EXCEPTION; |
| 7770 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7771 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7772 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7773 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7774 | else { |
| 7775 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7776 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7777 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7778 | if (outsize<requiredsize) |
| 7779 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7780 | Py_DECREF(rep); |
| 7781 | return enc_EXCEPTION; |
| 7782 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7783 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7784 | memcpy(outstart + *outpos, repchars, repsize); |
| 7785 | *outpos += repsize; |
| 7786 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7787 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7788 | Py_DECREF(rep); |
| 7789 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7790 | } |
| 7791 | |
| 7792 | /* handle an error in PyUnicode_EncodeCharmap |
| 7793 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7794 | static int |
| 7795 | charmap_encoding_error( |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7796 | PyObject *unicode, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7797 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7798 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7799 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7800 | { |
| 7801 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7802 | Py_ssize_t size, repsize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7803 | Py_ssize_t newpos; |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7804 | enum PyUnicode_Kind kind; |
| 7805 | void *data; |
| 7806 | Py_ssize_t index; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7807 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7808 | Py_ssize_t collstartpos = *inpos; |
| 7809 | Py_ssize_t collendpos = *inpos+1; |
| 7810 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7811 | char *encoding = "charmap"; |
| 7812 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7813 | charmapencode_result x; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7814 | Py_UCS4 ch; |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7815 | int val; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7816 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7817 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7818 | return -1; |
| 7819 | size = PyUnicode_GET_LENGTH(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7820 | /* find all unencodable characters */ |
| 7821 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7822 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7823 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7824 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
Brian Curtin | 2787ea4 | 2011-11-02 15:09:37 -0500 | [diff] [blame] | 7825 | val = encoding_map_lookup(ch, mapping); |
| 7826 | if (val != -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7827 | break; |
| 7828 | ++collendpos; |
| 7829 | continue; |
| 7830 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7831 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7832 | ch = PyUnicode_READ_CHAR(unicode, collendpos); |
| 7833 | rep = charmapencode_lookup(ch, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7834 | if (rep==NULL) |
| 7835 | return -1; |
| 7836 | else if (rep!=Py_None) { |
| 7837 | Py_DECREF(rep); |
| 7838 | break; |
| 7839 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7840 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7841 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7842 | } |
| 7843 | /* cache callback name lookup |
| 7844 | * (if not done yet, i.e. it's the first error) */ |
| 7845 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7846 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7847 | *known_errorHandler = 1; |
| 7848 | else if (!strcmp(errors, "replace")) |
| 7849 | *known_errorHandler = 2; |
| 7850 | else if (!strcmp(errors, "ignore")) |
| 7851 | *known_errorHandler = 3; |
| 7852 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7853 | *known_errorHandler = 4; |
| 7854 | else |
| 7855 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7856 | } |
| 7857 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7858 | case 1: /* strict */ |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7859 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7860 | return -1; |
| 7861 | case 2: /* replace */ |
| 7862 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7863 | x = charmapencode_output('?', mapping, res, respos); |
| 7864 | if (x==enc_EXCEPTION) { |
| 7865 | return -1; |
| 7866 | } |
| 7867 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7868 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7869 | return -1; |
| 7870 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7871 | } |
| 7872 | /* fall through */ |
| 7873 | case 3: /* ignore */ |
| 7874 | *inpos = collendpos; |
| 7875 | break; |
| 7876 | case 4: /* xmlcharrefreplace */ |
| 7877 | /* generate replacement (temporarily (mis)uses p) */ |
| 7878 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7879 | char buffer[2+29+1+1]; |
| 7880 | char *cp; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7881 | sprintf(buffer, "&#%d;", (int)PyUnicode_READ_CHAR(unicode, collpos)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7882 | for (cp = buffer; *cp; ++cp) { |
| 7883 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7884 | if (x==enc_EXCEPTION) |
| 7885 | return -1; |
| 7886 | else if (x==enc_FAILED) { |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7887 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7888 | return -1; |
| 7889 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7890 | } |
| 7891 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7892 | *inpos = collendpos; |
| 7893 | break; |
| 7894 | default: |
| 7895 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7896 | encoding, reason, unicode, exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7897 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7898 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7899 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7900 | if (PyBytes_Check(repunicode)) { |
| 7901 | /* Directly copy bytes result to output. */ |
| 7902 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7903 | Py_ssize_t requiredsize; |
| 7904 | repsize = PyBytes_Size(repunicode); |
| 7905 | requiredsize = *respos + repsize; |
| 7906 | if (requiredsize > outsize) |
| 7907 | /* Make room for all additional bytes. */ |
| 7908 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7909 | Py_DECREF(repunicode); |
| 7910 | return -1; |
| 7911 | } |
| 7912 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7913 | PyBytes_AsString(repunicode), repsize); |
| 7914 | *respos += repsize; |
| 7915 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7916 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7917 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7918 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7919 | /* generate replacement */ |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7920 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7921 | Py_DECREF(repunicode); |
| 7922 | return -1; |
| 7923 | } |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 7924 | repsize = PyUnicode_GET_LENGTH(repunicode); |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7925 | data = PyUnicode_DATA(repunicode); |
| 7926 | kind = PyUnicode_KIND(repunicode); |
| 7927 | for (index = 0; index < repsize; index++) { |
| 7928 | Py_UCS4 repch = PyUnicode_READ(kind, data, index); |
| 7929 | x = charmapencode_output(repch, mapping, res, respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7930 | if (x==enc_EXCEPTION) { |
Victor Stinner | ae4f7c8 | 2011-11-20 18:28:55 +0100 | [diff] [blame] | 7931 | Py_DECREF(repunicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7932 | return -1; |
| 7933 | } |
| 7934 | else if (x==enc_FAILED) { |
| 7935 | Py_DECREF(repunicode); |
Martin v. Löwis | 12be46c | 2011-11-04 19:04:15 +0100 | [diff] [blame] | 7936 | raise_encode_exception(exceptionObject, encoding, unicode, collstartpos, collendpos, reason); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7937 | return -1; |
| 7938 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7939 | } |
| 7940 | *inpos = newpos; |
| 7941 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7942 | } |
| 7943 | return 0; |
| 7944 | } |
| 7945 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7946 | PyObject * |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7947 | _PyUnicode_EncodeCharmap(PyObject *unicode, |
| 7948 | PyObject *mapping, |
| 7949 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7950 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7951 | /* output object */ |
| 7952 | PyObject *res = NULL; |
| 7953 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7954 | Py_ssize_t inpos = 0; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7955 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7956 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7957 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7958 | PyObject *errorHandler = NULL; |
| 7959 | PyObject *exc = NULL; |
| 7960 | /* the following variable is used for caching string comparisons |
| 7961 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7962 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7963 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7964 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 7965 | if (PyUnicode_READY(unicode) == -1) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7966 | return NULL; |
| 7967 | size = PyUnicode_GET_LENGTH(unicode); |
| 7968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7969 | /* Default to Latin-1 */ |
| 7970 | if (mapping == NULL) |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7971 | return unicode_encode_ucs1(unicode, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7972 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7973 | /* allocate enough for a simple encoding without |
| 7974 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7975 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7976 | if (res == NULL) |
| 7977 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7978 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7979 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7980 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7981 | while (inpos<size) { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7982 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, inpos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7983 | /* try to encode it */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7984 | charmapencode_result x = charmapencode_output(ch, mapping, &res, &respos); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7985 | if (x==enc_EXCEPTION) /* error */ |
| 7986 | goto onError; |
| 7987 | if (x==enc_FAILED) { /* unencodable character */ |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 7988 | if (charmap_encoding_error(unicode, &inpos, mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7989 | &exc, |
| 7990 | &known_errorHandler, &errorHandler, errors, |
| 7991 | &res, &respos)) { |
| 7992 | goto onError; |
| 7993 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7994 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7995 | else |
| 7996 | /* done with this character => adjust input position */ |
| 7997 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7998 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7999 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8000 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8001 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 8002 | if (_PyBytes_Resize(&res, respos) < 0) |
| 8003 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 8004 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8005 | Py_XDECREF(exc); |
| 8006 | Py_XDECREF(errorHandler); |
| 8007 | return res; |
| 8008 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8009 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8010 | Py_XDECREF(res); |
| 8011 | Py_XDECREF(exc); |
| 8012 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8013 | return NULL; |
| 8014 | } |
| 8015 | |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8016 | /* Deprecated */ |
| 8017 | PyObject * |
| 8018 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 8019 | Py_ssize_t size, |
| 8020 | PyObject *mapping, |
| 8021 | const char *errors) |
| 8022 | { |
| 8023 | PyObject *result; |
| 8024 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8025 | if (unicode == NULL) |
| 8026 | return NULL; |
| 8027 | result = _PyUnicode_EncodeCharmap(unicode, mapping, errors); |
| 8028 | Py_DECREF(unicode); |
Victor Stinner | fc026c9 | 2011-11-04 00:24:51 +0100 | [diff] [blame] | 8029 | return result; |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8030 | } |
| 8031 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8032 | PyObject * |
| 8033 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 8034 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8035 | { |
| 8036 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8037 | PyErr_BadArgument(); |
| 8038 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8039 | } |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8040 | return _PyUnicode_EncodeCharmap(unicode, mapping, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8041 | } |
| 8042 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8043 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8044 | static void |
| 8045 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8046 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8047 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8048 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8049 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8050 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8051 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 8052 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8053 | } |
| 8054 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8055 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 8056 | goto onError; |
| 8057 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 8058 | goto onError; |
| 8059 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 8060 | goto onError; |
| 8061 | return; |
| 8062 | onError: |
| 8063 | Py_DECREF(*exceptionObject); |
| 8064 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8065 | } |
| 8066 | } |
| 8067 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8068 | /* error handling callback helper: |
| 8069 | build arguments, call the callback and check the arguments, |
| 8070 | put the result into newpos and return the replacement string, which |
| 8071 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8072 | static PyObject * |
| 8073 | unicode_translate_call_errorhandler(const char *errors, |
| 8074 | PyObject **errorHandler, |
| 8075 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8076 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8077 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 8078 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8079 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8080 | 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] | 8081 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8082 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8083 | PyObject *restuple; |
| 8084 | PyObject *resunicode; |
| 8085 | |
| 8086 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8087 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8088 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8089 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8090 | } |
| 8091 | |
| 8092 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8093 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8094 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8095 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8096 | |
| 8097 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8098 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8099 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8100 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8101 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 8102 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8103 | Py_DECREF(restuple); |
| 8104 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8105 | } |
| 8106 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8107 | &resunicode, &i_newpos)) { |
| 8108 | Py_DECREF(restuple); |
| 8109 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8110 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8111 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8112 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8113 | else |
| 8114 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8115 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8116 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 8117 | Py_DECREF(restuple); |
| 8118 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 8119 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8120 | Py_INCREF(resunicode); |
| 8121 | Py_DECREF(restuple); |
| 8122 | return resunicode; |
| 8123 | } |
| 8124 | |
| 8125 | /* Lookup the character ch in the mapping and put the result in result, |
| 8126 | which must be decrefed by the caller. |
| 8127 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8128 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8129 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8130 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8131 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8132 | PyObject *x; |
| 8133 | |
| 8134 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8135 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8136 | x = PyObject_GetItem(mapping, w); |
| 8137 | Py_DECREF(w); |
| 8138 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8139 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 8140 | /* No mapping found means: use 1:1 mapping. */ |
| 8141 | PyErr_Clear(); |
| 8142 | *result = NULL; |
| 8143 | return 0; |
| 8144 | } else |
| 8145 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8146 | } |
| 8147 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8148 | *result = x; |
| 8149 | return 0; |
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 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8152 | long value = PyLong_AS_LONG(x); |
| 8153 | long max = PyUnicode_GetMax(); |
| 8154 | if (value < 0 || value > max) { |
| 8155 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 8156 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8157 | Py_DECREF(x); |
| 8158 | return -1; |
| 8159 | } |
| 8160 | *result = x; |
| 8161 | return 0; |
| 8162 | } |
| 8163 | else if (PyUnicode_Check(x)) { |
| 8164 | *result = x; |
| 8165 | return 0; |
| 8166 | } |
| 8167 | else { |
| 8168 | /* wrong return value */ |
| 8169 | PyErr_SetString(PyExc_TypeError, |
| 8170 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8171 | Py_DECREF(x); |
| 8172 | return -1; |
| 8173 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8174 | } |
| 8175 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8176 | if not reallocate and adjust various state variables. |
| 8177 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8178 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8179 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8180 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8181 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8182 | Py_ssize_t oldsize = *psize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8183 | Py_UCS4 *new_outobj; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 8184 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8185 | /* exponentially overallocate to minimize reallocations */ |
| 8186 | if (requiredsize < 2 * oldsize) |
| 8187 | requiredsize = 2 * oldsize; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8188 | new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 8189 | if (new_outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8190 | return -1; |
Kristjan Valur Jonsson | 85634d7 | 2012-05-31 09:37:31 +0000 | [diff] [blame] | 8191 | *outobj = new_outobj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8192 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8193 | } |
| 8194 | return 0; |
| 8195 | } |
| 8196 | /* lookup the character, put the result in the output string and adjust |
| 8197 | various state variables. Return a new reference to the object that |
| 8198 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 8199 | undefined (in which case no character was written). |
| 8200 | The called must decref result. |
| 8201 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8202 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8203 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 8204 | PyObject *mapping, Py_UCS4 **output, |
| 8205 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8206 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8207 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8208 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 8209 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8210 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8211 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8212 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8213 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8214 | } |
| 8215 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8216 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8217 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8218 | /* 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] | 8219 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8220 | } |
| 8221 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8222 | Py_ssize_t repsize; |
| 8223 | if (PyUnicode_READY(*res) == -1) |
| 8224 | return -1; |
| 8225 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8226 | if (repsize==1) { |
| 8227 | /* 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] | 8228 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8229 | } |
| 8230 | else if (repsize!=0) { |
| 8231 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8232 | Py_ssize_t requiredsize = *opos + |
| 8233 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8234 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8235 | Py_ssize_t i; |
| 8236 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8237 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8238 | for(i = 0; i < repsize; i++) |
| 8239 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8240 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8241 | } |
| 8242 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8243 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8244 | return 0; |
| 8245 | } |
| 8246 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8247 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8248 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 8249 | PyObject *mapping, |
| 8250 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8251 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8252 | /* input object */ |
| 8253 | char *idata; |
| 8254 | Py_ssize_t size, i; |
| 8255 | int kind; |
| 8256 | /* output buffer */ |
| 8257 | Py_UCS4 *output = NULL; |
| 8258 | Py_ssize_t osize; |
| 8259 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8260 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8261 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8262 | char *reason = "character maps to <undefined>"; |
| 8263 | PyObject *errorHandler = NULL; |
| 8264 | PyObject *exc = NULL; |
| 8265 | /* the following variable is used for caching string comparisons |
| 8266 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 8267 | * 3=ignore, 4=xmlcharrefreplace */ |
| 8268 | int known_errorHandler = -1; |
| 8269 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8270 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8271 | PyErr_BadArgument(); |
| 8272 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8273 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8274 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8275 | if (PyUnicode_READY(input) == -1) |
| 8276 | return NULL; |
| 8277 | idata = (char*)PyUnicode_DATA(input); |
| 8278 | kind = PyUnicode_KIND(input); |
| 8279 | size = PyUnicode_GET_LENGTH(input); |
| 8280 | i = 0; |
| 8281 | |
| 8282 | if (size == 0) { |
| 8283 | Py_INCREF(input); |
| 8284 | return input; |
| 8285 | } |
| 8286 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8287 | /* allocate enough for a simple 1:1 translation without |
| 8288 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8289 | osize = size; |
| 8290 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 8291 | opos = 0; |
| 8292 | if (output == NULL) { |
| 8293 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8294 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8295 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8296 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8297 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8298 | /* try to encode it */ |
| 8299 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8300 | if (charmaptranslate_output(input, i, mapping, |
| 8301 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8302 | Py_XDECREF(x); |
| 8303 | goto onError; |
| 8304 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8305 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8306 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8307 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8308 | else { /* untranslatable character */ |
| 8309 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 8310 | Py_ssize_t repsize; |
| 8311 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8312 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8313 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8314 | Py_ssize_t collstart = i; |
| 8315 | Py_ssize_t collend = i+1; |
| 8316 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8317 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8318 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8319 | while (collend < size) { |
| 8320 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8321 | goto onError; |
| 8322 | Py_XDECREF(x); |
| 8323 | if (x!=Py_None) |
| 8324 | break; |
| 8325 | ++collend; |
| 8326 | } |
| 8327 | /* cache callback name lookup |
| 8328 | * (if not done yet, i.e. it's the first error) */ |
| 8329 | if (known_errorHandler==-1) { |
| 8330 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8331 | known_errorHandler = 1; |
| 8332 | else if (!strcmp(errors, "replace")) |
| 8333 | known_errorHandler = 2; |
| 8334 | else if (!strcmp(errors, "ignore")) |
| 8335 | known_errorHandler = 3; |
| 8336 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8337 | known_errorHandler = 4; |
| 8338 | else |
| 8339 | known_errorHandler = 0; |
| 8340 | } |
| 8341 | switch (known_errorHandler) { |
| 8342 | case 1: /* strict */ |
Victor Stinner | 6fa6275 | 2012-10-23 02:51:50 +0200 | [diff] [blame] | 8343 | make_translate_exception(&exc, |
| 8344 | input, collstart, collend, reason); |
| 8345 | if (exc != NULL) |
| 8346 | PyCodec_StrictErrors(exc); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8347 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8348 | case 2: /* replace */ |
| 8349 | /* 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] | 8350 | for (coll = collstart; coll<collend; coll++) |
| 8351 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8352 | /* fall through */ |
| 8353 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8354 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8355 | break; |
| 8356 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8357 | /* generate replacement (temporarily (mis)uses i) */ |
| 8358 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8359 | char buffer[2+29+1+1]; |
| 8360 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8361 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 8362 | if (charmaptranslate_makespace(&output, &osize, |
| 8363 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8364 | goto onError; |
| 8365 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8366 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8367 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8368 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8369 | break; |
| 8370 | default: |
| 8371 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8372 | reason, input, &exc, |
| 8373 | collstart, collend, &newpos); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8374 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8375 | goto onError; |
Benjamin Peterson | 9ca3ffa | 2012-01-01 16:04:29 -0600 | [diff] [blame] | 8376 | if (PyUnicode_READY(repunicode) == -1) { |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8377 | Py_DECREF(repunicode); |
| 8378 | goto onError; |
| 8379 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8380 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8381 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 8382 | if (charmaptranslate_makespace(&output, &osize, |
| 8383 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8384 | Py_DECREF(repunicode); |
| 8385 | goto onError; |
| 8386 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8387 | for (uni2 = 0; repsize-->0; ++uni2) |
| 8388 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 8389 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8390 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8391 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8392 | } |
| 8393 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8394 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 8395 | if (!res) |
| 8396 | goto onError; |
| 8397 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8398 | Py_XDECREF(exc); |
| 8399 | Py_XDECREF(errorHandler); |
| 8400 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8401 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8402 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8403 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8404 | Py_XDECREF(exc); |
| 8405 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8406 | return NULL; |
| 8407 | } |
| 8408 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8409 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 8410 | PyObject * |
| 8411 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 8412 | Py_ssize_t size, |
| 8413 | PyObject *mapping, |
| 8414 | const char *errors) |
| 8415 | { |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8416 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8417 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 8418 | if (!unicode) |
| 8419 | return NULL; |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8420 | result = _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 8421 | Py_DECREF(unicode); |
| 8422 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8423 | } |
| 8424 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8425 | PyObject * |
| 8426 | PyUnicode_Translate(PyObject *str, |
| 8427 | PyObject *mapping, |
| 8428 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8429 | { |
| 8430 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8432 | str = PyUnicode_FromObject(str); |
| 8433 | if (str == NULL) |
Christian Heimes | 5f520f4 | 2012-09-11 14:03:25 +0200 | [diff] [blame] | 8434 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8435 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8436 | Py_DECREF(str); |
| 8437 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8438 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8439 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8440 | static Py_UCS4 |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8441 | fix_decimal_and_space_to_ascii(PyObject *self) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8442 | { |
| 8443 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8444 | called as a callback from fixup() which does it already. */ |
| 8445 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8446 | const int kind = PyUnicode_KIND(self); |
| 8447 | void *data = PyUnicode_DATA(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8448 | Py_UCS4 maxchar = 127, ch, fixed; |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8449 | int modified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8450 | Py_ssize_t i; |
| 8451 | |
| 8452 | for (i = 0; i < len; ++i) { |
| 8453 | ch = PyUnicode_READ(kind, data, i); |
| 8454 | fixed = 0; |
| 8455 | if (ch > 127) { |
| 8456 | if (Py_UNICODE_ISSPACE(ch)) |
| 8457 | fixed = ' '; |
| 8458 | else { |
| 8459 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8460 | if (decimal >= 0) |
| 8461 | fixed = '0' + decimal; |
| 8462 | } |
| 8463 | if (fixed != 0) { |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8464 | modified = 1; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8465 | maxchar = MAX_MAXCHAR(maxchar, fixed); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8466 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8467 | } |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8468 | else |
| 8469 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8470 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8471 | } |
| 8472 | |
Benjamin Peterson | 821e4cf | 2012-01-12 15:40:18 -0500 | [diff] [blame] | 8473 | return (modified) ? maxchar : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8474 | } |
| 8475 | |
| 8476 | PyObject * |
| 8477 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8478 | { |
| 8479 | if (!PyUnicode_Check(unicode)) { |
| 8480 | PyErr_BadInternalCall(); |
| 8481 | return NULL; |
| 8482 | } |
| 8483 | if (PyUnicode_READY(unicode) == -1) |
| 8484 | return NULL; |
| 8485 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8486 | /* If the string is already ASCII, just return the same string */ |
| 8487 | Py_INCREF(unicode); |
| 8488 | return unicode; |
| 8489 | } |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 8490 | return fixup(unicode, fix_decimal_and_space_to_ascii); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8491 | } |
| 8492 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8493 | PyObject * |
| 8494 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8495 | Py_ssize_t length) |
| 8496 | { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8497 | PyObject *decimal; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8498 | Py_ssize_t i; |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8499 | Py_UCS4 maxchar; |
| 8500 | enum PyUnicode_Kind kind; |
| 8501 | void *data; |
| 8502 | |
Victor Stinner | 99d7ad0 | 2012-02-22 13:37:39 +0100 | [diff] [blame] | 8503 | maxchar = 127; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8504 | for (i = 0; i < length; i++) { |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8505 | Py_UNICODE ch = s[i]; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8506 | if (ch > 127) { |
| 8507 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8508 | if (decimal >= 0) |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8509 | ch = '0' + decimal; |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8510 | maxchar = MAX_MAXCHAR(maxchar, ch); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8511 | } |
| 8512 | } |
Victor Stinner | f012450 | 2011-11-21 23:12:56 +0100 | [diff] [blame] | 8513 | |
| 8514 | /* Copy to a new string */ |
| 8515 | decimal = PyUnicode_New(length, maxchar); |
| 8516 | if (decimal == NULL) |
| 8517 | return decimal; |
| 8518 | kind = PyUnicode_KIND(decimal); |
| 8519 | data = PyUnicode_DATA(decimal); |
| 8520 | /* Iterate over code points */ |
| 8521 | for (i = 0; i < length; i++) { |
| 8522 | Py_UNICODE ch = s[i]; |
| 8523 | if (ch > 127) { |
| 8524 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8525 | if (decimal >= 0) |
| 8526 | ch = '0' + decimal; |
| 8527 | } |
| 8528 | PyUnicode_WRITE(kind, data, i, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8529 | } |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 8530 | return unicode_result(decimal); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8531 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8532 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8533 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8534 | int |
| 8535 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8536 | Py_ssize_t length, |
| 8537 | char *output, |
| 8538 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8539 | { |
Martin v. Löwis | 23e275b | 2011-11-02 18:02:51 +0100 | [diff] [blame] | 8540 | PyObject *unicode; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8541 | Py_ssize_t i; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8542 | enum PyUnicode_Kind kind; |
| 8543 | void *data; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8544 | |
| 8545 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8546 | PyErr_BadArgument(); |
| 8547 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8548 | } |
| 8549 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8550 | unicode = PyUnicode_FromUnicode(s, length); |
| 8551 | if (unicode == NULL) |
| 8552 | return -1; |
| 8553 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 8554 | if (PyUnicode_READY(unicode) == -1) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8555 | Py_DECREF(unicode); |
| 8556 | return -1; |
| 8557 | } |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8558 | kind = PyUnicode_KIND(unicode); |
| 8559 | data = PyUnicode_DATA(unicode); |
| 8560 | |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8561 | for (i=0; i < length; ) { |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8562 | PyObject *exc; |
| 8563 | Py_UCS4 ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8564 | int decimal; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8565 | Py_ssize_t startpos; |
| 8566 | |
| 8567 | ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8568 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8569 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8570 | *output++ = ' '; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8571 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8572 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8573 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8574 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8575 | if (decimal >= 0) { |
| 8576 | *output++ = '0' + decimal; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8577 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8578 | continue; |
| 8579 | } |
| 8580 | if (0 < ch && ch < 256) { |
| 8581 | *output++ = (char)ch; |
Victor Stinner | b84d723 | 2011-11-22 01:50:07 +0100 | [diff] [blame] | 8582 | i++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8583 | continue; |
| 8584 | } |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8585 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8586 | startpos = i; |
Victor Stinner | 6345be9 | 2011-11-25 20:09:01 +0100 | [diff] [blame] | 8587 | exc = NULL; |
| 8588 | raise_encode_exception(&exc, "decimal", unicode, |
| 8589 | startpos, startpos+1, |
| 8590 | "invalid decimal Unicode string"); |
| 8591 | Py_XDECREF(exc); |
| 8592 | Py_DECREF(unicode); |
| 8593 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8594 | } |
| 8595 | /* 0-terminate the output string */ |
| 8596 | *output++ = '\0'; |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 8597 | Py_DECREF(unicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8598 | return 0; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8599 | } |
| 8600 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8601 | /* --- Helpers ------------------------------------------------------------ */ |
| 8602 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8603 | static Py_ssize_t |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8604 | any_find_slice(int direction, PyObject* s1, PyObject* s2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8605 | Py_ssize_t start, |
| 8606 | Py_ssize_t end) |
| 8607 | { |
| 8608 | int kind1, kind2, kind; |
| 8609 | void *buf1, *buf2; |
| 8610 | Py_ssize_t len1, len2, result; |
| 8611 | |
| 8612 | kind1 = PyUnicode_KIND(s1); |
| 8613 | kind2 = PyUnicode_KIND(s2); |
| 8614 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8615 | buf1 = PyUnicode_DATA(s1); |
| 8616 | buf2 = PyUnicode_DATA(s2); |
| 8617 | if (kind1 != kind) |
| 8618 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8619 | if (!buf1) |
| 8620 | return -2; |
| 8621 | if (kind2 != kind) |
| 8622 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8623 | if (!buf2) { |
| 8624 | if (kind1 != kind) PyMem_Free(buf1); |
| 8625 | return -2; |
| 8626 | } |
| 8627 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8628 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8629 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8630 | if (direction > 0) { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8631 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8632 | case PyUnicode_1BYTE_KIND: |
| 8633 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8634 | result = asciilib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8635 | else |
| 8636 | result = ucs1lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8637 | break; |
| 8638 | case PyUnicode_2BYTE_KIND: |
| 8639 | result = ucs2lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8640 | break; |
| 8641 | case PyUnicode_4BYTE_KIND: |
| 8642 | result = ucs4lib_find_slice(buf1, len1, buf2, len2, start, end); |
| 8643 | break; |
| 8644 | default: |
| 8645 | assert(0); result = -2; |
| 8646 | } |
| 8647 | } |
| 8648 | else { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8649 | switch (kind) { |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8650 | case PyUnicode_1BYTE_KIND: |
| 8651 | if (PyUnicode_IS_ASCII(s1) && PyUnicode_IS_ASCII(s2)) |
| 8652 | result = asciilib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8653 | else |
| 8654 | result = ucs1lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8655 | break; |
| 8656 | case PyUnicode_2BYTE_KIND: |
| 8657 | result = ucs2lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8658 | break; |
| 8659 | case PyUnicode_4BYTE_KIND: |
| 8660 | result = ucs4lib_rfind_slice(buf1, len1, buf2, len2, start, end); |
| 8661 | break; |
| 8662 | default: |
| 8663 | assert(0); result = -2; |
| 8664 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8665 | } |
| 8666 | |
| 8667 | if (kind1 != kind) |
| 8668 | PyMem_Free(buf1); |
| 8669 | if (kind2 != kind) |
| 8670 | PyMem_Free(buf2); |
| 8671 | |
| 8672 | return result; |
| 8673 | } |
| 8674 | |
| 8675 | Py_ssize_t |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8676 | _PyUnicode_InsertThousandsGrouping( |
| 8677 | PyObject *unicode, Py_ssize_t index, |
| 8678 | Py_ssize_t n_buffer, |
| 8679 | void *digits, Py_ssize_t n_digits, |
| 8680 | Py_ssize_t min_width, |
| 8681 | const char *grouping, PyObject *thousands_sep, |
| 8682 | Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8683 | { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8684 | unsigned int kind, thousands_sep_kind; |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8685 | char *data, *thousands_sep_data; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8686 | Py_ssize_t thousands_sep_len; |
| 8687 | Py_ssize_t len; |
| 8688 | |
| 8689 | if (unicode != NULL) { |
| 8690 | kind = PyUnicode_KIND(unicode); |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8691 | data = (char *) PyUnicode_DATA(unicode) + index * kind; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8692 | } |
| 8693 | else { |
| 8694 | kind = PyUnicode_1BYTE_KIND; |
| 8695 | data = NULL; |
| 8696 | } |
| 8697 | thousands_sep_kind = PyUnicode_KIND(thousands_sep); |
| 8698 | thousands_sep_data = PyUnicode_DATA(thousands_sep); |
| 8699 | thousands_sep_len = PyUnicode_GET_LENGTH(thousands_sep); |
| 8700 | if (unicode != NULL && thousands_sep_kind != kind) { |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8701 | if (thousands_sep_kind < kind) { |
| 8702 | thousands_sep_data = _PyUnicode_AsKind(thousands_sep, kind); |
| 8703 | if (!thousands_sep_data) |
| 8704 | return -1; |
| 8705 | } |
| 8706 | else { |
| 8707 | data = _PyUnicode_AsKind(unicode, thousands_sep_kind); |
| 8708 | if (!data) |
| 8709 | return -1; |
| 8710 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8711 | } |
| 8712 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8713 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8714 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8715 | if (unicode != NULL && PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8716 | len = asciilib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8717 | (Py_UCS1 *) data, n_buffer, (Py_UCS1 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8718 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8719 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8720 | else |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8721 | len = ucs1lib_InsertThousandsGrouping( |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8722 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8723 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8724 | (Py_UCS1 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8725 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8726 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8727 | len = ucs2lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8728 | (Py_UCS2 *) data, n_buffer, (Py_UCS2 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8729 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8730 | (Py_UCS2 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8731 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8732 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8733 | len = ucs4lib_InsertThousandsGrouping( |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8734 | (Py_UCS4 *) data, n_buffer, (Py_UCS4 *) digits, n_digits, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8735 | min_width, grouping, |
Antoine Pitrou | 842c0f1 | 2012-02-24 13:30:46 +0100 | [diff] [blame] | 8736 | (Py_UCS4 *) thousands_sep_data, thousands_sep_len); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8737 | break; |
| 8738 | default: |
| 8739 | assert(0); |
| 8740 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8741 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 8742 | if (unicode != NULL && thousands_sep_kind != kind) { |
| 8743 | if (thousands_sep_kind < kind) |
| 8744 | PyMem_Free(thousands_sep_data); |
| 8745 | else |
| 8746 | PyMem_Free(data); |
| 8747 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8748 | if (unicode == NULL) { |
| 8749 | *maxchar = 127; |
| 8750 | if (len != n_digits) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 8751 | *maxchar = MAX_MAXCHAR(*maxchar, |
| 8752 | PyUnicode_MAX_CHAR_VALUE(thousands_sep)); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 8753 | } |
| 8754 | } |
| 8755 | return len; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8756 | } |
| 8757 | |
| 8758 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8759 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8760 | #define ADJUST_INDICES(start, end, len) \ |
| 8761 | if (end > len) \ |
| 8762 | end = len; \ |
| 8763 | else if (end < 0) { \ |
| 8764 | end += len; \ |
| 8765 | if (end < 0) \ |
| 8766 | end = 0; \ |
| 8767 | } \ |
| 8768 | if (start < 0) { \ |
| 8769 | start += len; \ |
| 8770 | if (start < 0) \ |
| 8771 | start = 0; \ |
| 8772 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8773 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8774 | Py_ssize_t |
| 8775 | PyUnicode_Count(PyObject *str, |
| 8776 | PyObject *substr, |
| 8777 | Py_ssize_t start, |
| 8778 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8779 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8780 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8781 | PyObject* str_obj; |
| 8782 | PyObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8783 | int kind1, kind2, kind; |
| 8784 | void *buf1 = NULL, *buf2 = NULL; |
| 8785 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8786 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8787 | str_obj = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8788 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8789 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8790 | sub_obj = PyUnicode_FromObject(substr); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8791 | if (!sub_obj) { |
| 8792 | Py_DECREF(str_obj); |
| 8793 | return -1; |
| 8794 | } |
Benjamin Peterson | 4c13a4a | 2012-01-02 09:07:38 -0600 | [diff] [blame] | 8795 | if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
Benjamin Peterson | 5e458f5 | 2012-01-02 10:12:13 -0600 | [diff] [blame] | 8796 | Py_DECREF(sub_obj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8797 | Py_DECREF(str_obj); |
| 8798 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8799 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8800 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8801 | kind1 = PyUnicode_KIND(str_obj); |
| 8802 | kind2 = PyUnicode_KIND(sub_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8803 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8804 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8805 | buf2 = PyUnicode_DATA(sub_obj); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8806 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8807 | if (kind2 > kind) { |
| 8808 | Py_DECREF(sub_obj); |
| 8809 | Py_DECREF(str_obj); |
Antoine Pitrou | e45c0c5 | 2012-05-12 15:49:07 +0200 | [diff] [blame] | 8810 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 8811 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 8812 | buf2 = _PyUnicode_AsKind(sub_obj, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 8813 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8814 | if (!buf2) |
| 8815 | goto onError; |
| 8816 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8817 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8818 | |
| 8819 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 8820 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8821 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 8822 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj)) |
| 8823 | result = asciilib_count( |
| 8824 | ((Py_UCS1*)buf1) + start, end - start, |
| 8825 | buf2, len2, PY_SSIZE_T_MAX |
| 8826 | ); |
| 8827 | else |
| 8828 | result = ucs1lib_count( |
| 8829 | ((Py_UCS1*)buf1) + start, end - start, |
| 8830 | buf2, len2, PY_SSIZE_T_MAX |
| 8831 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8832 | break; |
| 8833 | case PyUnicode_2BYTE_KIND: |
| 8834 | result = ucs2lib_count( |
| 8835 | ((Py_UCS2*)buf1) + start, end - start, |
| 8836 | buf2, len2, PY_SSIZE_T_MAX |
| 8837 | ); |
| 8838 | break; |
| 8839 | case PyUnicode_4BYTE_KIND: |
| 8840 | result = ucs4lib_count( |
| 8841 | ((Py_UCS4*)buf1) + start, end - start, |
| 8842 | buf2, len2, PY_SSIZE_T_MAX |
| 8843 | ); |
| 8844 | break; |
| 8845 | default: |
| 8846 | assert(0); result = 0; |
| 8847 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8848 | |
| 8849 | Py_DECREF(sub_obj); |
| 8850 | Py_DECREF(str_obj); |
| 8851 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8852 | if (kind2 != kind) |
| 8853 | PyMem_Free(buf2); |
| 8854 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8855 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8856 | onError: |
| 8857 | Py_DECREF(sub_obj); |
| 8858 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8859 | if (kind2 != kind && buf2) |
| 8860 | PyMem_Free(buf2); |
| 8861 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8862 | } |
| 8863 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8864 | Py_ssize_t |
| 8865 | PyUnicode_Find(PyObject *str, |
| 8866 | PyObject *sub, |
| 8867 | Py_ssize_t start, |
| 8868 | Py_ssize_t end, |
| 8869 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8870 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8871 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8872 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8873 | str = PyUnicode_FromObject(str); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8874 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8875 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8876 | sub = PyUnicode_FromObject(sub); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 8877 | if (!sub) { |
| 8878 | Py_DECREF(str); |
| 8879 | return -2; |
| 8880 | } |
| 8881 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 8882 | Py_DECREF(sub); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8883 | Py_DECREF(str); |
| 8884 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8885 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8886 | |
Victor Stinner | 794d567 | 2011-10-10 03:21:36 +0200 | [diff] [blame] | 8887 | result = any_find_slice(direction, |
| 8888 | str, sub, start, end |
| 8889 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8890 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8891 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8892 | Py_DECREF(sub); |
| 8893 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8894 | return result; |
| 8895 | } |
| 8896 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8897 | Py_ssize_t |
| 8898 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8899 | Py_ssize_t start, Py_ssize_t end, |
| 8900 | int direction) |
| 8901 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8902 | int kind; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8903 | Py_ssize_t result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8904 | if (PyUnicode_READY(str) == -1) |
| 8905 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8906 | if (start < 0 || end < 0) { |
| 8907 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8908 | return -2; |
| 8909 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8910 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8911 | end = PyUnicode_GET_LENGTH(str); |
| 8912 | kind = PyUnicode_KIND(str); |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8913 | result = findchar(PyUnicode_1BYTE_DATA(str) + kind*start, |
| 8914 | kind, end-start, ch, direction); |
| 8915 | if (result == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8916 | return -1; |
Antoine Pitrou | f0b934b | 2011-10-13 18:55:09 +0200 | [diff] [blame] | 8917 | else |
| 8918 | return start + result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8919 | } |
| 8920 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8921 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 8922 | tailmatch(PyObject *self, |
| 8923 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8924 | Py_ssize_t start, |
| 8925 | Py_ssize_t end, |
| 8926 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8927 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8928 | int kind_self; |
| 8929 | int kind_sub; |
| 8930 | void *data_self; |
| 8931 | void *data_sub; |
| 8932 | Py_ssize_t offset; |
| 8933 | Py_ssize_t i; |
| 8934 | Py_ssize_t end_sub; |
| 8935 | |
| 8936 | if (PyUnicode_READY(self) == -1 || |
| 8937 | PyUnicode_READY(substring) == -1) |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame^] | 8938 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8939 | |
| 8940 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8941 | return 1; |
| 8942 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8943 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8944 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8945 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8946 | return 0; |
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 | kind_self = PyUnicode_KIND(self); |
| 8949 | data_self = PyUnicode_DATA(self); |
| 8950 | kind_sub = PyUnicode_KIND(substring); |
| 8951 | data_sub = PyUnicode_DATA(substring); |
| 8952 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8953 | |
| 8954 | if (direction > 0) |
| 8955 | offset = end; |
| 8956 | else |
| 8957 | offset = start; |
| 8958 | |
| 8959 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8960 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8961 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8962 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8963 | /* If both are of the same kind, memcmp is sufficient */ |
| 8964 | if (kind_self == kind_sub) { |
| 8965 | return ! memcmp((char *)data_self + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8966 | (offset * PyUnicode_KIND(substring)), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8967 | data_sub, |
| 8968 | PyUnicode_GET_LENGTH(substring) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 8969 | PyUnicode_KIND(substring)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8970 | } |
| 8971 | /* otherwise we have to compare each character by first accesing it */ |
| 8972 | else { |
| 8973 | /* We do not need to compare 0 and len(substring)-1 because |
| 8974 | the if statement above ensured already that they are equal |
| 8975 | when we end up here. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8976 | for (i = 1; i < end_sub; ++i) { |
| 8977 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8978 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8979 | return 0; |
| 8980 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8981 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8982 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8983 | } |
| 8984 | |
| 8985 | return 0; |
| 8986 | } |
| 8987 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8988 | Py_ssize_t |
| 8989 | PyUnicode_Tailmatch(PyObject *str, |
| 8990 | PyObject *substr, |
| 8991 | Py_ssize_t start, |
| 8992 | Py_ssize_t end, |
| 8993 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8994 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8995 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8996 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8997 | str = PyUnicode_FromObject(str); |
| 8998 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8999 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9000 | substr = PyUnicode_FromObject(substr); |
| 9001 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9002 | Py_DECREF(str); |
| 9003 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9004 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9005 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 9006 | result = tailmatch(str, substr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9007 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9008 | Py_DECREF(str); |
| 9009 | Py_DECREF(substr); |
| 9010 | return result; |
| 9011 | } |
| 9012 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9013 | /* Apply fixfct filter to the Unicode object self and return a |
| 9014 | reference to the modified object */ |
| 9015 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9016 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9017 | fixup(PyObject *self, |
| 9018 | Py_UCS4 (*fixfct)(PyObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9019 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9020 | PyObject *u; |
| 9021 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9022 | PyObject *v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9023 | |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 9024 | u = _PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9025 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9026 | return NULL; |
Victor Stinner | 87af4f2 | 2011-11-21 23:03:47 +0100 | [diff] [blame] | 9027 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(u); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9028 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9029 | /* fix functions return the new maximum character in a string, |
| 9030 | if the kind of the resulting unicode object does not change, |
| 9031 | everything is fine. Otherwise we need to change the string kind |
| 9032 | and re-run the fix function. */ |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9033 | maxchar_new = fixfct(u); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9034 | |
| 9035 | if (maxchar_new == 0) { |
| 9036 | /* no changes */; |
| 9037 | if (PyUnicode_CheckExact(self)) { |
| 9038 | Py_DECREF(u); |
| 9039 | Py_INCREF(self); |
| 9040 | return self; |
| 9041 | } |
| 9042 | else |
| 9043 | return u; |
| 9044 | } |
| 9045 | |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9046 | maxchar_new = align_maxchar(maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9047 | |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9048 | if (maxchar_new == maxchar_old) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9049 | return u; |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9050 | |
| 9051 | /* In case the maximum character changed, we need to |
| 9052 | convert the string to the new category. */ |
| 9053 | v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
| 9054 | if (v == NULL) { |
| 9055 | Py_DECREF(u); |
| 9056 | return NULL; |
| 9057 | } |
| 9058 | if (maxchar_new > maxchar_old) { |
| 9059 | /* If the maxchar increased so that the kind changed, not all |
| 9060 | characters are representable anymore and we need to fix the |
| 9061 | string again. This only happens in very few cases. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9062 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9063 | self, 0, PyUnicode_GET_LENGTH(self)); |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9064 | maxchar_old = fixfct(v); |
| 9065 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9066 | } |
| 9067 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9068 | _PyUnicode_FastCopyCharacters(v, 0, |
| 9069 | u, 0, PyUnicode_GET_LENGTH(self)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9070 | } |
Victor Stinner | eaab604 | 2011-12-11 22:22:39 +0100 | [diff] [blame] | 9071 | Py_DECREF(u); |
| 9072 | assert(_PyUnicode_CheckConsistency(v, 1)); |
| 9073 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9074 | } |
| 9075 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9076 | static PyObject * |
| 9077 | ascii_upper_or_lower(PyObject *self, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9078 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9079 | Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 9080 | char *resdata, *data = PyUnicode_DATA(self); |
| 9081 | PyObject *res; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9082 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9083 | res = PyUnicode_New(len, 127); |
| 9084 | if (res == NULL) |
| 9085 | return NULL; |
| 9086 | resdata = PyUnicode_DATA(res); |
| 9087 | if (lower) |
| 9088 | _Py_bytes_lower(resdata, data, len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9089 | else |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9090 | _Py_bytes_upper(resdata, data, len); |
| 9091 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9092 | } |
| 9093 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9094 | static Py_UCS4 |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9095 | 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] | 9096 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9097 | Py_ssize_t j; |
| 9098 | int final_sigma; |
| 9099 | Py_UCS4 c; |
| 9100 | /* 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] | 9101 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9102 | \p{cased}\p{case-ignorable}*U+03A3!(\p{case-ignorable}*\p{cased}) |
| 9103 | |
| 9104 | where ! is a negation and \p{xxx} is a character with property xxx. |
| 9105 | */ |
| 9106 | for (j = i - 1; j >= 0; j--) { |
| 9107 | c = PyUnicode_READ(kind, data, j); |
| 9108 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9109 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9110 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9111 | final_sigma = j >= 0 && _PyUnicode_IsCased(c); |
| 9112 | if (final_sigma) { |
| 9113 | for (j = i + 1; j < length; j++) { |
| 9114 | c = PyUnicode_READ(kind, data, j); |
| 9115 | if (!_PyUnicode_IsCaseIgnorable(c)) |
| 9116 | break; |
| 9117 | } |
| 9118 | final_sigma = j == length || !_PyUnicode_IsCased(c); |
| 9119 | } |
| 9120 | return (final_sigma) ? 0x3C2 : 0x3C3; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9121 | } |
| 9122 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9123 | static int |
| 9124 | lower_ucs4(int kind, void *data, Py_ssize_t length, Py_ssize_t i, |
| 9125 | Py_UCS4 c, Py_UCS4 *mapped) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9126 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9127 | /* Obscure special case. */ |
| 9128 | if (c == 0x3A3) { |
| 9129 | mapped[0] = handle_capital_sigma(kind, data, length, i); |
| 9130 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9131 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9132 | return _PyUnicode_ToLowerFull(c, mapped); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9133 | } |
| 9134 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9135 | static Py_ssize_t |
| 9136 | 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] | 9137 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9138 | Py_ssize_t i, k = 0; |
| 9139 | int n_res, j; |
| 9140 | Py_UCS4 c, mapped[3]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9141 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9142 | c = PyUnicode_READ(kind, data, 0); |
| 9143 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9144 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9145 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9146 | res[k++] = mapped[j]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9147 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9148 | for (i = 1; i < length; i++) { |
| 9149 | c = PyUnicode_READ(kind, data, i); |
| 9150 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9151 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9152 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9153 | res[k++] = mapped[j]; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9154 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 9155 | } |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9156 | return k; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9157 | } |
| 9158 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9159 | static Py_ssize_t |
| 9160 | do_swapcase(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) { |
| 9161 | Py_ssize_t i, k = 0; |
| 9162 | |
| 9163 | for (i = 0; i < length; i++) { |
| 9164 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9165 | int n_res, j; |
| 9166 | if (Py_UNICODE_ISUPPER(c)) { |
| 9167 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9168 | } |
| 9169 | else if (Py_UNICODE_ISLOWER(c)) { |
| 9170 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9171 | } |
| 9172 | else { |
| 9173 | n_res = 1; |
| 9174 | mapped[0] = c; |
| 9175 | } |
| 9176 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9177 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9178 | res[k++] = mapped[j]; |
| 9179 | } |
| 9180 | } |
| 9181 | return k; |
| 9182 | } |
| 9183 | |
| 9184 | static Py_ssize_t |
| 9185 | do_upper_or_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, |
| 9186 | Py_UCS4 *maxchar, int lower) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9187 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9188 | Py_ssize_t i, k = 0; |
| 9189 | |
| 9190 | for (i = 0; i < length; i++) { |
| 9191 | Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3]; |
| 9192 | int n_res, j; |
| 9193 | if (lower) |
| 9194 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9195 | else |
| 9196 | n_res = _PyUnicode_ToUpperFull(c, mapped); |
| 9197 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9198 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9199 | res[k++] = mapped[j]; |
| 9200 | } |
| 9201 | } |
| 9202 | return k; |
| 9203 | } |
| 9204 | |
| 9205 | static Py_ssize_t |
| 9206 | do_upper(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9207 | { |
| 9208 | return do_upper_or_lower(kind, data, length, res, maxchar, 0); |
| 9209 | } |
| 9210 | |
| 9211 | static Py_ssize_t |
| 9212 | do_lower(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9213 | { |
| 9214 | return do_upper_or_lower(kind, data, length, res, maxchar, 1); |
| 9215 | } |
| 9216 | |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9217 | static Py_ssize_t |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9218 | do_casefold(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9219 | { |
| 9220 | Py_ssize_t i, k = 0; |
| 9221 | |
| 9222 | for (i = 0; i < length; i++) { |
| 9223 | Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9224 | Py_UCS4 mapped[3]; |
| 9225 | int j, n_res = _PyUnicode_ToFoldedFull(c, mapped); |
| 9226 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9227 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 9228 | res[k++] = mapped[j]; |
| 9229 | } |
| 9230 | } |
| 9231 | return k; |
| 9232 | } |
| 9233 | |
| 9234 | static Py_ssize_t |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9235 | do_title(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *maxchar) |
| 9236 | { |
| 9237 | Py_ssize_t i, k = 0; |
| 9238 | int previous_is_cased; |
| 9239 | |
| 9240 | previous_is_cased = 0; |
| 9241 | for (i = 0; i < length; i++) { |
| 9242 | const Py_UCS4 c = PyUnicode_READ(kind, data, i); |
| 9243 | Py_UCS4 mapped[3]; |
| 9244 | int n_res, j; |
| 9245 | |
| 9246 | if (previous_is_cased) |
| 9247 | n_res = lower_ucs4(kind, data, length, i, c, mapped); |
| 9248 | else |
| 9249 | n_res = _PyUnicode_ToTitleFull(c, mapped); |
| 9250 | |
| 9251 | for (j = 0; j < n_res; j++) { |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9252 | *maxchar = MAX_MAXCHAR(*maxchar, mapped[j]); |
Benjamin Peterson | e51757f | 2012-01-12 21:10:29 -0500 | [diff] [blame] | 9253 | res[k++] = mapped[j]; |
| 9254 | } |
| 9255 | |
| 9256 | previous_is_cased = _PyUnicode_IsCased(c); |
| 9257 | } |
| 9258 | return k; |
| 9259 | } |
| 9260 | |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9261 | static PyObject * |
| 9262 | case_operation(PyObject *self, |
| 9263 | Py_ssize_t (*perform)(int, void *, Py_ssize_t, Py_UCS4 *, Py_UCS4 *)) |
| 9264 | { |
| 9265 | PyObject *res = NULL; |
| 9266 | Py_ssize_t length, newlength = 0; |
| 9267 | int kind, outkind; |
| 9268 | void *data, *outdata; |
| 9269 | Py_UCS4 maxchar = 0, *tmp, *tmpend; |
| 9270 | |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 9271 | assert(PyUnicode_IS_READY(self)); |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 9272 | |
| 9273 | kind = PyUnicode_KIND(self); |
| 9274 | data = PyUnicode_DATA(self); |
| 9275 | length = PyUnicode_GET_LENGTH(self); |
| 9276 | tmp = PyMem_MALLOC(sizeof(Py_UCS4) * 3 * length); |
| 9277 | if (tmp == NULL) |
| 9278 | return PyErr_NoMemory(); |
| 9279 | newlength = perform(kind, data, length, tmp, &maxchar); |
| 9280 | res = PyUnicode_New(newlength, maxchar); |
| 9281 | if (res == NULL) |
| 9282 | goto leave; |
| 9283 | tmpend = tmp + newlength; |
| 9284 | outdata = PyUnicode_DATA(res); |
| 9285 | outkind = PyUnicode_KIND(res); |
| 9286 | switch (outkind) { |
| 9287 | case PyUnicode_1BYTE_KIND: |
| 9288 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS1, tmp, tmpend, outdata); |
| 9289 | break; |
| 9290 | case PyUnicode_2BYTE_KIND: |
| 9291 | _PyUnicode_CONVERT_BYTES(Py_UCS4, Py_UCS2, tmp, tmpend, outdata); |
| 9292 | break; |
| 9293 | case PyUnicode_4BYTE_KIND: |
| 9294 | memcpy(outdata, tmp, sizeof(Py_UCS4) * newlength); |
| 9295 | break; |
| 9296 | default: |
| 9297 | assert(0); |
| 9298 | break; |
| 9299 | } |
| 9300 | leave: |
| 9301 | PyMem_FREE(tmp); |
| 9302 | return res; |
| 9303 | } |
| 9304 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9305 | PyObject * |
| 9306 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9307 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9308 | PyObject *sep = NULL; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9309 | Py_ssize_t seplen; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9310 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9311 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9312 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 9313 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9314 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9315 | Py_ssize_t sz, i, res_offset; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9316 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9317 | Py_UCS4 item_maxchar; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9318 | int use_memcpy; |
| 9319 | unsigned char *res_data = NULL, *sep_data = NULL; |
| 9320 | PyObject *last_obj; |
| 9321 | unsigned int kind = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9322 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9323 | fseq = PySequence_Fast(seq, ""); |
| 9324 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9325 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9326 | } |
| 9327 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9328 | /* NOTE: the following code can't call back into Python code, |
| 9329 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9330 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9331 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9332 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 9333 | /* If empty sequence, return u"". */ |
| 9334 | if (seqlen == 0) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9335 | Py_DECREF(fseq); |
| 9336 | Py_INCREF(unicode_empty); |
| 9337 | res = unicode_empty; |
| 9338 | return res; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9339 | } |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9340 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9341 | /* If singleton sequence with an exact Unicode, return that. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9342 | last_obj = NULL; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9343 | items = PySequence_Fast_ITEMS(fseq); |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9344 | if (seqlen == 1) { |
| 9345 | if (PyUnicode_CheckExact(items[0])) { |
| 9346 | res = items[0]; |
| 9347 | Py_INCREF(res); |
| 9348 | Py_DECREF(fseq); |
| 9349 | return res; |
| 9350 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9351 | seplen = 0; |
Victor Stinner | c6f0df7 | 2011-10-06 15:58:54 +0200 | [diff] [blame] | 9352 | maxchar = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9353 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9354 | else { |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9355 | /* Set up sep and seplen */ |
| 9356 | if (separator == NULL) { |
| 9357 | /* fall back to a blank space separator */ |
| 9358 | sep = PyUnicode_FromOrdinal(' '); |
| 9359 | if (!sep) |
| 9360 | goto onError; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9361 | seplen = 1; |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9362 | maxchar = 32; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9363 | } |
Victor Stinner | acf47b8 | 2011-10-06 12:32:37 +0200 | [diff] [blame] | 9364 | else { |
| 9365 | if (!PyUnicode_Check(separator)) { |
| 9366 | PyErr_Format(PyExc_TypeError, |
| 9367 | "separator: expected str instance," |
| 9368 | " %.80s found", |
| 9369 | Py_TYPE(separator)->tp_name); |
| 9370 | goto onError; |
| 9371 | } |
| 9372 | if (PyUnicode_READY(separator)) |
| 9373 | goto onError; |
| 9374 | sep = separator; |
| 9375 | seplen = PyUnicode_GET_LENGTH(separator); |
| 9376 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 9377 | /* inc refcount to keep this code path symmetric with the |
| 9378 | above case of a blank separator */ |
| 9379 | Py_INCREF(sep); |
| 9380 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9381 | last_obj = sep; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9382 | } |
| 9383 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9384 | /* There are at least two things to join, or else we have a subclass |
| 9385 | * of str in the sequence. |
| 9386 | * Do a pre-pass to figure out the total amount of space we'll |
| 9387 | * need (sz), and see whether all argument are strings. |
| 9388 | */ |
| 9389 | sz = 0; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9390 | #ifdef Py_DEBUG |
| 9391 | use_memcpy = 0; |
| 9392 | #else |
| 9393 | use_memcpy = 1; |
| 9394 | #endif |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9395 | for (i = 0; i < seqlen; i++) { |
| 9396 | const Py_ssize_t old_sz = sz; |
| 9397 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9398 | if (!PyUnicode_Check(item)) { |
| 9399 | PyErr_Format(PyExc_TypeError, |
| 9400 | "sequence item %zd: expected str instance," |
| 9401 | " %.80s found", |
| 9402 | i, Py_TYPE(item)->tp_name); |
| 9403 | goto onError; |
| 9404 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9405 | if (PyUnicode_READY(item) == -1) |
| 9406 | goto onError; |
| 9407 | sz += PyUnicode_GET_LENGTH(item); |
| 9408 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9409 | maxchar = MAX_MAXCHAR(maxchar, item_maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9410 | if (i != 0) |
| 9411 | sz += seplen; |
| 9412 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 9413 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9414 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9415 | goto onError; |
| 9416 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9417 | if (use_memcpy && last_obj != NULL) { |
| 9418 | if (PyUnicode_KIND(last_obj) != PyUnicode_KIND(item)) |
| 9419 | use_memcpy = 0; |
| 9420 | } |
| 9421 | last_obj = item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9422 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9423 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9424 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9425 | if (res == NULL) |
| 9426 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 9427 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9428 | /* Catenate everything. */ |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9429 | #ifdef Py_DEBUG |
| 9430 | use_memcpy = 0; |
| 9431 | #else |
| 9432 | if (use_memcpy) { |
| 9433 | res_data = PyUnicode_1BYTE_DATA(res); |
| 9434 | kind = PyUnicode_KIND(res); |
| 9435 | if (seplen != 0) |
| 9436 | sep_data = PyUnicode_1BYTE_DATA(sep); |
| 9437 | } |
| 9438 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9439 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 9440 | Py_ssize_t itemlen; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 9441 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9442 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9443 | if (i && seplen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9444 | if (use_memcpy) { |
| 9445 | Py_MEMCPY(res_data, |
| 9446 | sep_data, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9447 | kind * seplen); |
| 9448 | res_data += kind * seplen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9449 | } |
| 9450 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9451 | _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9452 | res_offset += seplen; |
| 9453 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9454 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9455 | itemlen = PyUnicode_GET_LENGTH(item); |
| 9456 | if (itemlen != 0) { |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9457 | if (use_memcpy) { |
| 9458 | Py_MEMCPY(res_data, |
| 9459 | PyUnicode_DATA(item), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9460 | kind * itemlen); |
| 9461 | res_data += kind * itemlen; |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9462 | } |
| 9463 | else { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9464 | _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9465 | res_offset += itemlen; |
| 9466 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 9467 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9468 | } |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9469 | if (use_memcpy) |
| 9470 | assert(res_data == PyUnicode_1BYTE_DATA(res) |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9471 | + kind * PyUnicode_GET_LENGTH(res)); |
Victor Stinner | dd07732 | 2011-10-07 17:02:31 +0200 | [diff] [blame] | 9472 | else |
| 9473 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9474 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9475 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9476 | Py_XDECREF(sep); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9477 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9478 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9479 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9480 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 9481 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9482 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 9483 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9484 | return NULL; |
| 9485 | } |
| 9486 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9487 | #define FILL(kind, data, value, start, length) \ |
| 9488 | do { \ |
| 9489 | Py_ssize_t i_ = 0; \ |
| 9490 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 9491 | switch ((kind)) { \ |
| 9492 | case PyUnicode_1BYTE_KIND: { \ |
| 9493 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 9494 | memset(to_, (unsigned char)value, (length)); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9495 | break; \ |
| 9496 | } \ |
| 9497 | case PyUnicode_2BYTE_KIND: { \ |
| 9498 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 9499 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9500 | break; \ |
| 9501 | } \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9502 | case PyUnicode_4BYTE_KIND: { \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9503 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 9504 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 9505 | break; \ |
Benjamin Peterson | e157cf1 | 2012-01-01 15:56:20 -0600 | [diff] [blame] | 9506 | default: assert(0); \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9507 | } \ |
| 9508 | } \ |
| 9509 | } while (0) |
| 9510 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9511 | void |
| 9512 | _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9513 | Py_UCS4 fill_char) |
| 9514 | { |
| 9515 | const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); |
| 9516 | const void *data = PyUnicode_DATA(unicode); |
| 9517 | assert(PyUnicode_IS_READY(unicode)); |
| 9518 | assert(unicode_modifiable(unicode)); |
| 9519 | assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 9520 | assert(start >= 0); |
| 9521 | assert(start + length <= PyUnicode_GET_LENGTH(unicode)); |
| 9522 | FILL(kind, data, fill_char, start, length); |
| 9523 | } |
| 9524 | |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9525 | Py_ssize_t |
| 9526 | PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, |
| 9527 | Py_UCS4 fill_char) |
| 9528 | { |
| 9529 | Py_ssize_t maxlen; |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9530 | |
| 9531 | if (!PyUnicode_Check(unicode)) { |
| 9532 | PyErr_BadInternalCall(); |
| 9533 | return -1; |
| 9534 | } |
| 9535 | if (PyUnicode_READY(unicode) == -1) |
| 9536 | return -1; |
| 9537 | if (unicode_check_modifiable(unicode)) |
| 9538 | return -1; |
| 9539 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9540 | if (start < 0) { |
| 9541 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 9542 | return -1; |
| 9543 | } |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9544 | if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { |
| 9545 | PyErr_SetString(PyExc_ValueError, |
| 9546 | "fill character is bigger than " |
| 9547 | "the string maximum character"); |
| 9548 | return -1; |
| 9549 | } |
| 9550 | |
| 9551 | maxlen = PyUnicode_GET_LENGTH(unicode) - start; |
| 9552 | length = Py_MIN(maxlen, length); |
| 9553 | if (length <= 0) |
| 9554 | return 0; |
| 9555 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9556 | _PyUnicode_FastFill(unicode, start, length, fill_char); |
Victor Stinner | 3fe5531 | 2012-01-04 00:33:50 +0100 | [diff] [blame] | 9557 | return length; |
| 9558 | } |
| 9559 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9560 | static PyObject * |
| 9561 | pad(PyObject *self, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9562 | Py_ssize_t left, |
| 9563 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9564 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9565 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9566 | PyObject *u; |
| 9567 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9568 | int kind; |
| 9569 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9570 | |
| 9571 | if (left < 0) |
| 9572 | left = 0; |
| 9573 | if (right < 0) |
| 9574 | right = 0; |
| 9575 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 9576 | if (left == 0 && right == 0) |
| 9577 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9578 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9579 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9580 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9581 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9582 | return NULL; |
| 9583 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9584 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9585 | maxchar = MAX_MAXCHAR(maxchar, fill); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9586 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9587 | if (!u) |
| 9588 | return NULL; |
| 9589 | |
| 9590 | kind = PyUnicode_KIND(u); |
| 9591 | data = PyUnicode_DATA(u); |
| 9592 | if (left) |
| 9593 | FILL(kind, data, fill, 0, left); |
| 9594 | if (right) |
| 9595 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9596 | _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 9597 | assert(_PyUnicode_CheckConsistency(u, 1)); |
| 9598 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9599 | } |
| 9600 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9601 | PyObject * |
| 9602 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9603 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9604 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9605 | |
| 9606 | string = PyUnicode_FromObject(string); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9607 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9608 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 9609 | if (PyUnicode_READY(string) == -1) { |
| 9610 | Py_DECREF(string); |
| 9611 | return NULL; |
| 9612 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9613 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9614 | switch (PyUnicode_KIND(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9615 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9616 | if (PyUnicode_IS_ASCII(string)) |
| 9617 | list = asciilib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9618 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9619 | PyUnicode_GET_LENGTH(string), keepends); |
| 9620 | else |
| 9621 | list = ucs1lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9622 | string, PyUnicode_1BYTE_DATA(string), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9623 | PyUnicode_GET_LENGTH(string), keepends); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9624 | break; |
| 9625 | case PyUnicode_2BYTE_KIND: |
| 9626 | list = ucs2lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9627 | string, PyUnicode_2BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9628 | PyUnicode_GET_LENGTH(string), keepends); |
| 9629 | break; |
| 9630 | case PyUnicode_4BYTE_KIND: |
| 9631 | list = ucs4lib_splitlines( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9632 | string, PyUnicode_4BYTE_DATA(string), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9633 | PyUnicode_GET_LENGTH(string), keepends); |
| 9634 | break; |
| 9635 | default: |
| 9636 | assert(0); |
| 9637 | list = 0; |
| 9638 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9639 | Py_DECREF(string); |
| 9640 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9641 | } |
| 9642 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9643 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9644 | split(PyObject *self, |
| 9645 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9646 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9647 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9648 | int kind1, kind2, kind; |
| 9649 | void *buf1, *buf2; |
| 9650 | Py_ssize_t len1, len2; |
| 9651 | PyObject* out; |
| 9652 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9653 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9654 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9655 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9656 | if (PyUnicode_READY(self) == -1) |
| 9657 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9658 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9659 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9660 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9661 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9662 | if (PyUnicode_IS_ASCII(self)) |
| 9663 | return asciilib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9664 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9665 | PyUnicode_GET_LENGTH(self), maxcount |
| 9666 | ); |
| 9667 | else |
| 9668 | return ucs1lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9669 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9670 | PyUnicode_GET_LENGTH(self), maxcount |
| 9671 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9672 | case PyUnicode_2BYTE_KIND: |
| 9673 | return ucs2lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9674 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9675 | PyUnicode_GET_LENGTH(self), maxcount |
| 9676 | ); |
| 9677 | case PyUnicode_4BYTE_KIND: |
| 9678 | return ucs4lib_split_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9679 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9680 | PyUnicode_GET_LENGTH(self), maxcount |
| 9681 | ); |
| 9682 | default: |
| 9683 | assert(0); |
| 9684 | return NULL; |
| 9685 | } |
| 9686 | |
| 9687 | if (PyUnicode_READY(substring) == -1) |
| 9688 | return NULL; |
| 9689 | |
| 9690 | kind1 = PyUnicode_KIND(self); |
| 9691 | kind2 = PyUnicode_KIND(substring); |
| 9692 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9693 | buf1 = PyUnicode_DATA(self); |
| 9694 | buf2 = PyUnicode_DATA(substring); |
| 9695 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9696 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9697 | if (!buf1) |
| 9698 | return NULL; |
| 9699 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9700 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9701 | if (!buf2) { |
| 9702 | if (kind1 != kind) PyMem_Free(buf1); |
| 9703 | return NULL; |
| 9704 | } |
| 9705 | len1 = PyUnicode_GET_LENGTH(self); |
| 9706 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9707 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9708 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9709 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9710 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9711 | out = asciilib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9712 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9713 | else |
| 9714 | out = ucs1lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9715 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9716 | break; |
| 9717 | case PyUnicode_2BYTE_KIND: |
| 9718 | out = ucs2lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9719 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9720 | break; |
| 9721 | case PyUnicode_4BYTE_KIND: |
| 9722 | out = ucs4lib_split( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9723 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9724 | break; |
| 9725 | default: |
| 9726 | out = NULL; |
| 9727 | } |
| 9728 | if (kind1 != kind) |
| 9729 | PyMem_Free(buf1); |
| 9730 | if (kind2 != kind) |
| 9731 | PyMem_Free(buf2); |
| 9732 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9733 | } |
| 9734 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9735 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 9736 | rsplit(PyObject *self, |
| 9737 | PyObject *substring, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9738 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9739 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9740 | int kind1, kind2, kind; |
| 9741 | void *buf1, *buf2; |
| 9742 | Py_ssize_t len1, len2; |
| 9743 | PyObject* out; |
| 9744 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9745 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9746 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9747 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9748 | if (PyUnicode_READY(self) == -1) |
| 9749 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9750 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9751 | if (substring == NULL) |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9752 | switch (PyUnicode_KIND(self)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9753 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9754 | if (PyUnicode_IS_ASCII(self)) |
| 9755 | return asciilib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9756 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9757 | PyUnicode_GET_LENGTH(self), maxcount |
| 9758 | ); |
| 9759 | else |
| 9760 | return ucs1lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9761 | self, PyUnicode_1BYTE_DATA(self), |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9762 | PyUnicode_GET_LENGTH(self), maxcount |
| 9763 | ); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9764 | case PyUnicode_2BYTE_KIND: |
| 9765 | return ucs2lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9766 | self, PyUnicode_2BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9767 | PyUnicode_GET_LENGTH(self), maxcount |
| 9768 | ); |
| 9769 | case PyUnicode_4BYTE_KIND: |
| 9770 | return ucs4lib_rsplit_whitespace( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9771 | self, PyUnicode_4BYTE_DATA(self), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9772 | PyUnicode_GET_LENGTH(self), maxcount |
| 9773 | ); |
| 9774 | default: |
| 9775 | assert(0); |
| 9776 | return NULL; |
| 9777 | } |
| 9778 | |
| 9779 | if (PyUnicode_READY(substring) == -1) |
| 9780 | return NULL; |
| 9781 | |
| 9782 | kind1 = PyUnicode_KIND(self); |
| 9783 | kind2 = PyUnicode_KIND(substring); |
| 9784 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9785 | buf1 = PyUnicode_DATA(self); |
| 9786 | buf2 = PyUnicode_DATA(substring); |
| 9787 | if (kind1 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9788 | buf1 = _PyUnicode_AsKind(self, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9789 | if (!buf1) |
| 9790 | return NULL; |
| 9791 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9792 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9793 | if (!buf2) { |
| 9794 | if (kind1 != kind) PyMem_Free(buf1); |
| 9795 | return NULL; |
| 9796 | } |
| 9797 | len1 = PyUnicode_GET_LENGTH(self); |
| 9798 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9799 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9800 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9801 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9802 | if (PyUnicode_IS_ASCII(self) && PyUnicode_IS_ASCII(substring)) |
| 9803 | out = asciilib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9804 | self, buf1, len1, buf2, len2, maxcount); |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9805 | else |
| 9806 | out = ucs1lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9807 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9808 | break; |
| 9809 | case PyUnicode_2BYTE_KIND: |
| 9810 | out = ucs2lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9811 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9812 | break; |
| 9813 | case PyUnicode_4BYTE_KIND: |
| 9814 | out = ucs4lib_rsplit( |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 9815 | self, buf1, len1, buf2, len2, maxcount); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9816 | break; |
| 9817 | default: |
| 9818 | out = NULL; |
| 9819 | } |
| 9820 | if (kind1 != kind) |
| 9821 | PyMem_Free(buf1); |
| 9822 | if (kind2 != kind) |
| 9823 | PyMem_Free(buf2); |
| 9824 | return out; |
| 9825 | } |
| 9826 | |
| 9827 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9828 | anylib_find(int kind, PyObject *str1, void *buf1, Py_ssize_t len1, |
| 9829 | 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] | 9830 | { |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 9831 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9832 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9833 | if (PyUnicode_IS_ASCII(str1) && PyUnicode_IS_ASCII(str2)) |
| 9834 | return asciilib_find(buf1, len1, buf2, len2, offset); |
| 9835 | else |
| 9836 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9837 | case PyUnicode_2BYTE_KIND: |
| 9838 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9839 | case PyUnicode_4BYTE_KIND: |
| 9840 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9841 | } |
| 9842 | assert(0); |
| 9843 | return -1; |
| 9844 | } |
| 9845 | |
| 9846 | static Py_ssize_t |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9847 | anylib_count(int kind, PyObject *sstr, void* sbuf, Py_ssize_t slen, |
| 9848 | 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] | 9849 | { |
Benjamin Peterson | c0b95d1 | 2011-12-20 17:24:05 -0600 | [diff] [blame] | 9850 | switch (kind) { |
| 9851 | case PyUnicode_1BYTE_KIND: |
| 9852 | if (PyUnicode_IS_ASCII(sstr) && PyUnicode_IS_ASCII(str1)) |
| 9853 | return asciilib_count(sbuf, slen, buf1, len1, maxcount); |
| 9854 | else |
| 9855 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9856 | case PyUnicode_2BYTE_KIND: |
| 9857 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9858 | case PyUnicode_4BYTE_KIND: |
| 9859 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9860 | } |
| 9861 | assert(0); |
| 9862 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9863 | } |
| 9864 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9865 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9866 | replace(PyObject *self, PyObject *str1, |
| 9867 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9868 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9869 | PyObject *u; |
| 9870 | char *sbuf = PyUnicode_DATA(self); |
| 9871 | char *buf1 = PyUnicode_DATA(str1); |
| 9872 | char *buf2 = PyUnicode_DATA(str2); |
| 9873 | int srelease = 0, release1 = 0, release2 = 0; |
| 9874 | int skind = PyUnicode_KIND(self); |
| 9875 | int kind1 = PyUnicode_KIND(str1); |
| 9876 | int kind2 = PyUnicode_KIND(str2); |
| 9877 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9878 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9879 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9880 | int mayshrink; |
| 9881 | Py_UCS4 maxchar, maxchar_str2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9882 | |
| 9883 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9884 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9885 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9886 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9887 | |
Victor Stinner | 59de0ee | 2011-10-07 10:01:28 +0200 | [diff] [blame] | 9888 | if (str1 == str2) |
| 9889 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9890 | if (skind < kind1) |
| 9891 | /* substring too wide to be present */ |
| 9892 | goto nothing; |
| 9893 | |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9894 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9895 | maxchar_str2 = PyUnicode_MAX_CHAR_VALUE(str2); |
| 9896 | /* Replacing str1 with str2 may cause a maxchar reduction in the |
| 9897 | result string. */ |
| 9898 | mayshrink = (maxchar_str2 < maxchar); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 9899 | maxchar = MAX_MAXCHAR(maxchar, maxchar_str2); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9900 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9901 | if (len1 == len2) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9902 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9903 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9904 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9905 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9906 | /* replace characters */ |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9907 | Py_UCS4 u1, u2; |
| 9908 | int rkind; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9909 | Py_ssize_t index, pos; |
| 9910 | char *src; |
| 9911 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9912 | u1 = PyUnicode_READ_CHAR(str1, 0); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9913 | pos = findchar(sbuf, PyUnicode_KIND(self), slen, u1, 1); |
| 9914 | if (pos < 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9915 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9916 | u2 = PyUnicode_READ_CHAR(str2, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9917 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9918 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9919 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 9920 | _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9921 | rkind = PyUnicode_KIND(u); |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9922 | |
| 9923 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); |
| 9924 | index = 0; |
| 9925 | src = sbuf; |
| 9926 | while (--maxcount) |
| 9927 | { |
| 9928 | pos++; |
| 9929 | src += pos * PyUnicode_KIND(self); |
| 9930 | slen -= pos; |
| 9931 | index += pos; |
| 9932 | pos = findchar(src, PyUnicode_KIND(self), slen, u1, 1); |
| 9933 | if (pos < 0) |
| 9934 | break; |
| 9935 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), index + pos, u2); |
| 9936 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9937 | } |
| 9938 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9939 | int rkind = skind; |
| 9940 | char *res; |
Victor Stinner | f644110 | 2011-12-18 02:43:08 +0100 | [diff] [blame] | 9941 | Py_ssize_t i; |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9942 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9943 | if (kind1 < rkind) { |
| 9944 | /* widen substring */ |
| 9945 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9946 | if (!buf1) goto error; |
| 9947 | release1 = 1; |
| 9948 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9949 | i = anylib_find(rkind, self, sbuf, slen, str1, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9950 | if (i < 0) |
| 9951 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9952 | if (rkind > kind2) { |
| 9953 | /* widen replacement */ |
| 9954 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9955 | if (!buf2) goto error; |
| 9956 | release2 = 1; |
| 9957 | } |
| 9958 | else if (rkind < kind2) { |
| 9959 | /* widen self and buf1 */ |
| 9960 | rkind = kind2; |
| 9961 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 9962 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9963 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9964 | if (!sbuf) goto error; |
| 9965 | srelease = 1; |
| 9966 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9967 | if (!buf1) goto error; |
| 9968 | release1 = 1; |
| 9969 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9970 | u = PyUnicode_New(slen, maxchar); |
| 9971 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9972 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9973 | assert(PyUnicode_KIND(u) == rkind); |
| 9974 | res = PyUnicode_DATA(u); |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 9975 | |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9976 | memcpy(res, sbuf, rkind * slen); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9977 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9978 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9979 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9980 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9981 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9982 | |
| 9983 | while ( --maxcount > 0) { |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9984 | i = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9985 | sbuf+rkind*i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 9986 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9987 | if (i == -1) |
| 9988 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9989 | memcpy(res + rkind * i, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9990 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 9991 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9992 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9993 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9994 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 9995 | } |
| 9996 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9997 | Py_ssize_t n, i, j, ires; |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 9998 | Py_ssize_t new_size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9999 | int rkind = skind; |
| 10000 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10001 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10002 | if (kind1 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10003 | /* widen substring */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10004 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10005 | if (!buf1) goto error; |
| 10006 | release1 = 1; |
| 10007 | } |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10008 | n = anylib_count(rkind, self, sbuf, slen, str1, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10009 | if (n == 0) |
| 10010 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10011 | if (kind2 < rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10012 | /* widen replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10013 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 10014 | if (!buf2) goto error; |
| 10015 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10016 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10017 | else if (kind2 > rkind) { |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10018 | /* widen self and buf1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10019 | rkind = kind2; |
| 10020 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 10021 | if (!sbuf) goto error; |
| 10022 | srelease = 1; |
| 10023 | if (release1) PyMem_Free(buf1); |
Antoine Pitrou | 6d5ad22 | 2012-11-17 23:28:17 +0100 | [diff] [blame] | 10024 | release1 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10025 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 10026 | if (!buf1) goto error; |
| 10027 | release1 = 1; |
| 10028 | } |
| 10029 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 10030 | PyUnicode_GET_LENGTH(str1))); */ |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10031 | 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] | 10032 | PyErr_SetString(PyExc_OverflowError, |
| 10033 | "replace string is too long"); |
| 10034 | goto error; |
| 10035 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10036 | new_size = slen + n * (len2 - len1); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10037 | if (new_size == 0) { |
| 10038 | Py_INCREF(unicode_empty); |
| 10039 | u = unicode_empty; |
| 10040 | goto done; |
| 10041 | } |
Mark Dickinson | c04ddff | 2012-10-06 18:04:49 +0100 | [diff] [blame] | 10042 | if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10043 | PyErr_SetString(PyExc_OverflowError, |
| 10044 | "replace string is too long"); |
| 10045 | goto error; |
| 10046 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10047 | u = PyUnicode_New(new_size, maxchar); |
| 10048 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10049 | goto error; |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10050 | assert(PyUnicode_KIND(u) == rkind); |
| 10051 | res = PyUnicode_DATA(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10052 | ires = i = 0; |
| 10053 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10054 | while (n-- > 0) { |
| 10055 | /* look for next match */ |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10056 | j = anylib_find(rkind, self, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10057 | sbuf + rkind * i, slen-i, |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 10058 | str1, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 10059 | if (j == -1) |
| 10060 | break; |
| 10061 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10062 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10063 | memcpy(res + rkind * ires, |
| 10064 | sbuf + rkind * i, |
| 10065 | rkind * (j-i)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10066 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10067 | } |
| 10068 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10069 | if (len2 > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10070 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10071 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10072 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10073 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10074 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10075 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10076 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10077 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10078 | /* copy tail [i:] */ |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10079 | memcpy(res + rkind * ires, |
| 10080 | sbuf + rkind * i, |
| 10081 | rkind * (slen-i)); |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10082 | } |
| 10083 | else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10084 | /* interleave */ |
| 10085 | while (n > 0) { |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10086 | memcpy(res + rkind * ires, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10087 | buf2, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10088 | rkind * len2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10089 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10090 | if (--n <= 0) |
| 10091 | break; |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10092 | memcpy(res + rkind * ires, |
| 10093 | sbuf + rkind * i, |
| 10094 | rkind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10095 | ires++; |
| 10096 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10097 | } |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 10098 | memcpy(res + rkind * ires, |
| 10099 | sbuf + rkind * i, |
| 10100 | rkind * (slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10101 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10102 | } |
| 10103 | |
| 10104 | if (mayshrink) { |
Victor Stinner | 25a4b29 | 2011-10-06 12:31:55 +0200 | [diff] [blame] | 10105 | unicode_adjust_maxchar(&u); |
| 10106 | if (u == NULL) |
| 10107 | goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10108 | } |
Victor Stinner | 49a0a21 | 2011-10-12 23:46:10 +0200 | [diff] [blame] | 10109 | |
| 10110 | done: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10111 | if (srelease) |
| 10112 | PyMem_FREE(sbuf); |
| 10113 | if (release1) |
| 10114 | PyMem_FREE(buf1); |
| 10115 | if (release2) |
| 10116 | PyMem_FREE(buf2); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10117 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10118 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10119 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10120 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10121 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10122 | if (srelease) |
| 10123 | PyMem_FREE(sbuf); |
| 10124 | if (release1) |
| 10125 | PyMem_FREE(buf1); |
| 10126 | if (release2) |
| 10127 | PyMem_FREE(buf2); |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10128 | return unicode_result_unchanged(self); |
| 10129 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10130 | error: |
| 10131 | if (srelease && sbuf) |
| 10132 | PyMem_FREE(sbuf); |
| 10133 | if (release1 && buf1) |
| 10134 | PyMem_FREE(buf1); |
| 10135 | if (release2 && buf2) |
| 10136 | PyMem_FREE(buf2); |
| 10137 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10138 | } |
| 10139 | |
| 10140 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 10141 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10142 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10143 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10144 | \n\ |
| 10145 | 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] | 10146 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10147 | |
| 10148 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10149 | unicode_title(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10150 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 10151 | if (PyUnicode_READY(self) == -1) |
| 10152 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10153 | return case_operation(self, do_title); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10154 | } |
| 10155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10156 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10157 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10158 | \n\ |
| 10159 | 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] | 10160 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10161 | |
| 10162 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10163 | unicode_capitalize(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10164 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 10165 | if (PyUnicode_READY(self) == -1) |
| 10166 | return NULL; |
| 10167 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10168 | return unicode_result_unchanged(self); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10169 | return case_operation(self, do_capitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10170 | } |
| 10171 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10172 | PyDoc_STRVAR(casefold__doc__, |
| 10173 | "S.casefold() -> str\n\ |
| 10174 | \n\ |
| 10175 | Return a version of S suitable for caseless comparisons."); |
| 10176 | |
| 10177 | static PyObject * |
| 10178 | unicode_casefold(PyObject *self) |
| 10179 | { |
| 10180 | if (PyUnicode_READY(self) == -1) |
| 10181 | return NULL; |
| 10182 | if (PyUnicode_IS_ASCII(self)) |
| 10183 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 10184 | return case_operation(self, do_casefold); |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 10185 | } |
| 10186 | |
| 10187 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10188 | /* Argument converter. Coerces to a single unicode character */ |
| 10189 | |
| 10190 | static int |
| 10191 | convert_uc(PyObject *obj, void *addr) |
| 10192 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10193 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10194 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10195 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10196 | uniobj = PyUnicode_FromObject(obj); |
| 10197 | if (uniobj == NULL) { |
| 10198 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10199 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10200 | return 0; |
| 10201 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10202 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10203 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10204 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10205 | Py_DECREF(uniobj); |
| 10206 | return 0; |
| 10207 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10208 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10209 | Py_DECREF(uniobj); |
| 10210 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10211 | } |
| 10212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10213 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10214 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10215 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10216 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10217 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10218 | |
| 10219 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10220 | unicode_center(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10221 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10222 | Py_ssize_t marg, left; |
| 10223 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10224 | Py_UCS4 fillchar = ' '; |
| 10225 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 10226 | 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] | 10227 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10228 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10229 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10230 | return NULL; |
| 10231 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10232 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 10233 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10234 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10235 | marg = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10236 | left = marg / 2 + (marg & width & 1); |
| 10237 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 10238 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10239 | } |
| 10240 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10241 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 10242 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10243 | static int |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10244 | unicode_compare(PyObject *str1, PyObject *str2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10245 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10246 | int kind1, kind2; |
| 10247 | void *data1, *data2; |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10248 | Py_ssize_t len1, len2; |
| 10249 | Py_ssize_t i, len; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10250 | |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10251 | /* a string is equal to itself */ |
| 10252 | if (str1 == str2) |
| 10253 | return 0; |
| 10254 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10255 | kind1 = PyUnicode_KIND(str1); |
| 10256 | kind2 = PyUnicode_KIND(str2); |
| 10257 | data1 = PyUnicode_DATA(str1); |
| 10258 | data2 = PyUnicode_DATA(str2); |
| 10259 | len1 = PyUnicode_GET_LENGTH(str1); |
| 10260 | len2 = PyUnicode_GET_LENGTH(str2); |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10261 | len = Py_MIN(len1, len2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10262 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10263 | if (kind1 == 1 && kind2 == 1) { |
| 10264 | int cmp = memcmp(data1, data2, len); |
| 10265 | /* normalize result of memcmp() into the range [-1; 1] */ |
| 10266 | if (cmp < 0) |
| 10267 | return -1; |
| 10268 | if (cmp > 0) |
| 10269 | return 1; |
| 10270 | } |
| 10271 | else { |
| 10272 | for (i = 0; i < len; ++i) { |
| 10273 | Py_UCS4 c1, c2; |
| 10274 | c1 = PyUnicode_READ(kind1, data1, i); |
| 10275 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 10276 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10277 | if (c1 != c2) |
| 10278 | return (c1 < c2) ? -1 : 1; |
| 10279 | } |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10280 | } |
| 10281 | |
Victor Stinner | 770e19e | 2012-10-04 22:59:45 +0200 | [diff] [blame] | 10282 | if (len1 == len2) |
| 10283 | return 0; |
| 10284 | if (len1 < len2) |
| 10285 | return -1; |
| 10286 | else |
| 10287 | return 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 10288 | } |
| 10289 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10290 | static int |
| 10291 | unicode_compare_eq(PyObject *str1, PyObject *str2) |
| 10292 | { |
| 10293 | int kind; |
| 10294 | void *data1, *data2; |
| 10295 | Py_ssize_t len; |
| 10296 | int cmp; |
| 10297 | |
| 10298 | /* a string is equal to itself */ |
| 10299 | if (str1 == str2) |
| 10300 | return 1; |
| 10301 | |
| 10302 | len = PyUnicode_GET_LENGTH(str1); |
| 10303 | if (PyUnicode_GET_LENGTH(str2) != len) |
| 10304 | return 0; |
| 10305 | kind = PyUnicode_KIND(str1); |
| 10306 | if (PyUnicode_KIND(str2) != kind) |
| 10307 | return 0; |
| 10308 | data1 = PyUnicode_DATA(str1); |
| 10309 | data2 = PyUnicode_DATA(str2); |
| 10310 | |
| 10311 | cmp = memcmp(data1, data2, len * kind); |
| 10312 | return (cmp == 0); |
| 10313 | } |
| 10314 | |
| 10315 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10316 | int |
| 10317 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10318 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10319 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 10320 | if (PyUnicode_READY(left) == -1 || |
| 10321 | PyUnicode_READY(right) == -1) |
| 10322 | return -1; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10323 | return unicode_compare(left, right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10324 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 10325 | PyErr_Format(PyExc_TypeError, |
| 10326 | "Can't compare %.100s and %.100s", |
| 10327 | left->ob_type->tp_name, |
| 10328 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10329 | return -1; |
| 10330 | } |
| 10331 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10332 | int |
| 10333 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 10334 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10335 | Py_ssize_t i; |
| 10336 | int kind; |
| 10337 | void *data; |
| 10338 | Py_UCS4 chr; |
| 10339 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 10340 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10341 | if (PyUnicode_READY(uni) == -1) |
| 10342 | return -1; |
| 10343 | kind = PyUnicode_KIND(uni); |
| 10344 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10345 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10346 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 10347 | if (chr != str[i]) |
| 10348 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 10349 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 10350 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10351 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10352 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10353 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10354 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 10355 | return 0; |
| 10356 | } |
| 10357 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10358 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10359 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10360 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10361 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10362 | PyObject * |
| 10363 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10364 | { |
| 10365 | int result; |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10366 | PyObject *v; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10367 | |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10368 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) |
| 10369 | Py_RETURN_NOTIMPLEMENTED; |
| 10370 | |
| 10371 | if (PyUnicode_READY(left) == -1 || |
| 10372 | PyUnicode_READY(right) == -1) |
| 10373 | return NULL; |
| 10374 | |
| 10375 | if (op == Py_EQ || op == Py_NE) { |
| 10376 | result = unicode_compare_eq(left, right); |
| 10377 | if (op == Py_EQ) |
| 10378 | v = TEST_COND(result); |
| 10379 | else |
| 10380 | v = TEST_COND(!result); |
| 10381 | } |
| 10382 | else { |
Victor Stinner | 90db9c4 | 2012-10-04 21:53:50 +0200 | [diff] [blame] | 10383 | result = unicode_compare(left, right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10384 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10385 | /* Convert the return value to a Boolean */ |
| 10386 | switch (op) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 10387 | case Py_LE: |
| 10388 | v = TEST_COND(result <= 0); |
| 10389 | break; |
| 10390 | case Py_GE: |
| 10391 | v = TEST_COND(result >= 0); |
| 10392 | break; |
| 10393 | case Py_LT: |
| 10394 | v = TEST_COND(result == -1); |
| 10395 | break; |
| 10396 | case Py_GT: |
| 10397 | v = TEST_COND(result == 1); |
| 10398 | break; |
| 10399 | default: |
| 10400 | PyErr_BadArgument(); |
| 10401 | return NULL; |
| 10402 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10403 | } |
Victor Stinner | e5567ad | 2012-10-23 02:48:49 +0200 | [diff] [blame] | 10404 | Py_INCREF(v); |
| 10405 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 10406 | } |
| 10407 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10408 | int |
| 10409 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10410 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10411 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10412 | int kind1, kind2, kind; |
| 10413 | void *buf1, *buf2; |
| 10414 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10415 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10416 | |
| 10417 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10418 | sub = PyUnicode_FromObject(element); |
| 10419 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10420 | PyErr_Format(PyExc_TypeError, |
| 10421 | "'in <string>' requires string as left operand, not %s", |
| 10422 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10423 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10424 | } |
| 10425 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10426 | str = PyUnicode_FromObject(container); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10427 | if (!str) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10428 | Py_DECREF(sub); |
| 10429 | return -1; |
| 10430 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 10431 | if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) { |
| 10432 | Py_DECREF(sub); |
| 10433 | Py_DECREF(str); |
| 10434 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10435 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10436 | kind1 = PyUnicode_KIND(str); |
| 10437 | kind2 = PyUnicode_KIND(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10438 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10439 | buf1 = PyUnicode_DATA(str); |
| 10440 | buf2 = PyUnicode_DATA(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10441 | if (kind2 != kind) { |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10442 | if (kind2 > kind) { |
| 10443 | Py_DECREF(sub); |
| 10444 | Py_DECREF(str); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10445 | return 0; |
Antoine Pitrou | 758153b | 2012-05-12 15:51:51 +0200 | [diff] [blame] | 10446 | } |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10447 | buf2 = _PyUnicode_AsKind(sub, kind); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10448 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10449 | if (!buf2) { |
| 10450 | Py_DECREF(sub); |
Benjamin Peterson | 1ff2e35 | 2012-05-11 17:41:20 -0500 | [diff] [blame] | 10451 | Py_DECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10452 | return -1; |
| 10453 | } |
| 10454 | len1 = PyUnicode_GET_LENGTH(str); |
| 10455 | len2 = PyUnicode_GET_LENGTH(sub); |
| 10456 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10457 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10458 | case PyUnicode_1BYTE_KIND: |
| 10459 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10460 | break; |
| 10461 | case PyUnicode_2BYTE_KIND: |
| 10462 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10463 | break; |
| 10464 | case PyUnicode_4BYTE_KIND: |
| 10465 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 10466 | break; |
| 10467 | default: |
| 10468 | result = -1; |
| 10469 | assert(0); |
| 10470 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10471 | |
| 10472 | Py_DECREF(str); |
| 10473 | Py_DECREF(sub); |
| 10474 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10475 | if (kind2 != kind) |
| 10476 | PyMem_Free(buf2); |
| 10477 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10478 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 10479 | } |
| 10480 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10481 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 10482 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 10483 | PyObject * |
| 10484 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10485 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10486 | PyObject *u = NULL, *v = NULL, *w; |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10487 | Py_UCS4 maxchar, maxchar2; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10488 | Py_ssize_t u_len, v_len, new_len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10489 | |
| 10490 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10491 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10492 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10493 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10494 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10495 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10496 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10497 | |
| 10498 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10499 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10500 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10501 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10502 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 10503 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10504 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10505 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10506 | } |
| 10507 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10508 | u_len = PyUnicode_GET_LENGTH(u); |
| 10509 | v_len = PyUnicode_GET_LENGTH(v); |
| 10510 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 10511 | PyErr_SetString(PyExc_OverflowError, |
| 10512 | "strings are too large to concat"); |
| 10513 | goto onError; |
| 10514 | } |
| 10515 | new_len = u_len + v_len; |
| 10516 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10517 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | 127226b | 2011-10-13 01:12:34 +0200 | [diff] [blame] | 10518 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(v); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10519 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10521 | /* Concat the two Unicode strings */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10522 | w = PyUnicode_New(new_len, maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10523 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10524 | goto onError; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10525 | _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); |
| 10526 | _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10527 | Py_DECREF(u); |
| 10528 | Py_DECREF(v); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 10529 | assert(_PyUnicode_CheckConsistency(w, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10530 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10531 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10532 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10533 | Py_XDECREF(u); |
| 10534 | Py_XDECREF(v); |
| 10535 | return NULL; |
| 10536 | } |
| 10537 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10538 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10539 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10540 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10541 | PyObject *left, *res; |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10542 | Py_UCS4 maxchar, maxchar2; |
| 10543 | Py_ssize_t left_len, right_len, new_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10544 | |
| 10545 | if (p_left == NULL) { |
| 10546 | if (!PyErr_Occurred()) |
| 10547 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10548 | return; |
| 10549 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10550 | left = *p_left; |
| 10551 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10552 | if (!PyErr_Occurred()) |
| 10553 | PyErr_BadInternalCall(); |
| 10554 | goto error; |
| 10555 | } |
| 10556 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10557 | if (PyUnicode_READY(left) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10558 | goto error; |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 10559 | if (PyUnicode_READY(right) == -1) |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 10560 | goto error; |
| 10561 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10562 | /* Shortcuts */ |
| 10563 | if (left == unicode_empty) { |
| 10564 | Py_DECREF(left); |
| 10565 | Py_INCREF(right); |
| 10566 | *p_left = right; |
| 10567 | return; |
| 10568 | } |
| 10569 | if (right == unicode_empty) |
| 10570 | return; |
| 10571 | |
| 10572 | left_len = PyUnicode_GET_LENGTH(left); |
| 10573 | right_len = PyUnicode_GET_LENGTH(right); |
| 10574 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 10575 | PyErr_SetString(PyExc_OverflowError, |
| 10576 | "strings are too large to concat"); |
| 10577 | goto error; |
| 10578 | } |
| 10579 | new_len = left_len + right_len; |
| 10580 | |
| 10581 | if (unicode_modifiable(left) |
| 10582 | && PyUnicode_CheckExact(right) |
| 10583 | && PyUnicode_KIND(right) <= PyUnicode_KIND(left) |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10584 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10585 | to change the structure size, but characters are stored just after |
Georg Brandl | 7597add | 2011-10-05 16:36:47 +0200 | [diff] [blame] | 10586 | the structure, and so it requires to move all characters which is |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame] | 10587 | not so different than duplicating the string. */ |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10588 | && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
| 10589 | { |
| 10590 | /* append inplace */ |
| 10591 | if (unicode_resize(p_left, new_len) != 0) { |
| 10592 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10593 | * deallocated so it cannot be put back into |
| 10594 | * 'variable'. The MemoryError is raised when there |
| 10595 | * is no value in 'variable', which might (very |
| 10596 | * remotely) be a cause of incompatibilities. |
| 10597 | */ |
| 10598 | goto error; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10599 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10600 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10601 | _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10602 | } |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10603 | else { |
| 10604 | maxchar = PyUnicode_MAX_CHAR_VALUE(left); |
| 10605 | maxchar2 = PyUnicode_MAX_CHAR_VALUE(right); |
Victor Stinner | e6abb48 | 2012-05-02 01:15:40 +0200 | [diff] [blame] | 10606 | maxchar = MAX_MAXCHAR(maxchar, maxchar2); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10607 | |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10608 | /* Concat the two Unicode strings */ |
| 10609 | res = PyUnicode_New(new_len, maxchar); |
| 10610 | if (res == NULL) |
| 10611 | goto error; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 10612 | _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); |
| 10613 | _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10614 | Py_DECREF(left); |
| 10615 | *p_left = res; |
| 10616 | } |
| 10617 | assert(_PyUnicode_CheckConsistency(*p_left, 1)); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10618 | return; |
| 10619 | |
| 10620 | error: |
Victor Stinner | 488fa49 | 2011-12-12 00:01:39 +0100 | [diff] [blame] | 10621 | Py_CLEAR(*p_left); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10622 | } |
| 10623 | |
| 10624 | void |
| 10625 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10626 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10627 | PyUnicode_Append(pleft, right); |
| 10628 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10629 | } |
| 10630 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10631 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10632 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10633 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10634 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10635 | 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] | 10636 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10637 | |
| 10638 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10639 | unicode_count(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10640 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10641 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10642 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10643 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10644 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10645 | int kind1, kind2, kind; |
| 10646 | void *buf1, *buf2; |
| 10647 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10648 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10649 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10650 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10651 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10652 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10653 | kind1 = PyUnicode_KIND(self); |
| 10654 | kind2 = PyUnicode_KIND(substring); |
Benjamin Peterson | b63f49f | 2012-05-03 18:31:07 -0400 | [diff] [blame] | 10655 | if (kind2 > kind1) |
| 10656 | return PyLong_FromLong(0); |
| 10657 | kind = kind1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10658 | buf1 = PyUnicode_DATA(self); |
| 10659 | buf2 = PyUnicode_DATA(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10660 | if (kind2 != kind) |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10661 | buf2 = _PyUnicode_AsKind(substring, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10662 | if (!buf2) { |
| 10663 | Py_DECREF(substring); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10664 | return NULL; |
| 10665 | } |
| 10666 | len1 = PyUnicode_GET_LENGTH(self); |
| 10667 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10668 | |
| 10669 | ADJUST_INDICES(start, end, len1); |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 10670 | switch (kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10671 | case PyUnicode_1BYTE_KIND: |
| 10672 | iresult = ucs1lib_count( |
| 10673 | ((Py_UCS1*)buf1) + start, end - start, |
| 10674 | buf2, len2, PY_SSIZE_T_MAX |
| 10675 | ); |
| 10676 | break; |
| 10677 | case PyUnicode_2BYTE_KIND: |
| 10678 | iresult = ucs2lib_count( |
| 10679 | ((Py_UCS2*)buf1) + start, end - start, |
| 10680 | buf2, len2, PY_SSIZE_T_MAX |
| 10681 | ); |
| 10682 | break; |
| 10683 | case PyUnicode_4BYTE_KIND: |
| 10684 | iresult = ucs4lib_count( |
| 10685 | ((Py_UCS4*)buf1) + start, end - start, |
| 10686 | buf2, len2, PY_SSIZE_T_MAX |
| 10687 | ); |
| 10688 | break; |
| 10689 | default: |
| 10690 | assert(0); iresult = 0; |
| 10691 | } |
| 10692 | |
| 10693 | result = PyLong_FromSsize_t(iresult); |
| 10694 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10695 | if (kind2 != kind) |
| 10696 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10697 | |
| 10698 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10699 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10700 | return result; |
| 10701 | } |
| 10702 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10703 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10704 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10705 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10706 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10707 | 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] | 10708 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10709 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10710 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10711 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10712 | |
| 10713 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10714 | unicode_encode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10715 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10716 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10717 | char *encoding = NULL; |
| 10718 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10719 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10720 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10721 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10722 | return NULL; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10723 | return PyUnicode_AsEncodedString(self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10724 | } |
| 10725 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10726 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10727 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10728 | \n\ |
| 10729 | 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] | 10730 | 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] | 10731 | |
| 10732 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10733 | unicode_expandtabs(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10734 | { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10735 | Py_ssize_t i, j, line_pos, src_len, incr; |
| 10736 | Py_UCS4 ch; |
| 10737 | PyObject *u; |
| 10738 | void *src_data, *dest_data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10739 | int tabsize = 8; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10740 | int kind; |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10741 | int found; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10742 | |
| 10743 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10744 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10745 | |
Antoine Pitrou | 2242522 | 2011-10-04 19:10:51 +0200 | [diff] [blame] | 10746 | if (PyUnicode_READY(self) == -1) |
| 10747 | return NULL; |
| 10748 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10749 | /* First pass: determine size of output string */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10750 | src_len = PyUnicode_GET_LENGTH(self); |
| 10751 | i = j = line_pos = 0; |
| 10752 | kind = PyUnicode_KIND(self); |
| 10753 | src_data = PyUnicode_DATA(self); |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10754 | found = 0; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10755 | for (; i < src_len; i++) { |
| 10756 | ch = PyUnicode_READ(kind, src_data, i); |
| 10757 | if (ch == '\t') { |
Antoine Pitrou | e19aa38 | 2011-10-04 16:04:01 +0200 | [diff] [blame] | 10758 | found = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10759 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10760 | incr = tabsize - (line_pos % tabsize); /* cannot overflow */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10761 | if (j > PY_SSIZE_T_MAX - incr) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10762 | goto overflow; |
| 10763 | line_pos += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10764 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10765 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10766 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10767 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10768 | if (j > PY_SSIZE_T_MAX - 1) |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10769 | goto overflow; |
| 10770 | line_pos++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10771 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10772 | if (ch == '\n' || ch == '\r') |
| 10773 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10774 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10775 | } |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 10776 | if (!found) |
| 10777 | return unicode_result_unchanged(self); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10778 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10779 | /* Second pass: create output string and fill it */ |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10780 | u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10781 | if (!u) |
| 10782 | return NULL; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10783 | dest_data = PyUnicode_DATA(u); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10784 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10785 | i = j = line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10786 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10787 | for (; i < src_len; i++) { |
| 10788 | ch = PyUnicode_READ(kind, src_data, i); |
| 10789 | if (ch == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10790 | if (tabsize > 0) { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10791 | incr = tabsize - (line_pos % tabsize); |
| 10792 | line_pos += incr; |
Victor Stinner | da79e63 | 2012-02-22 13:37:04 +0100 | [diff] [blame] | 10793 | FILL(kind, dest_data, ' ', j, incr); |
| 10794 | j += incr; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10795 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10796 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10797 | else { |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10798 | line_pos++; |
| 10799 | PyUnicode_WRITE(kind, dest_data, j, ch); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10800 | j++; |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10801 | if (ch == '\n' || ch == '\r') |
| 10802 | line_pos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10803 | } |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10804 | } |
| 10805 | assert (j == PyUnicode_GET_LENGTH(u)); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 10806 | return unicode_result(u); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10807 | |
Antoine Pitrou | e71d574 | 2011-10-04 15:55:09 +0200 | [diff] [blame] | 10808 | overflow: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10809 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10810 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10811 | } |
| 10812 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10813 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10814 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10815 | \n\ |
| 10816 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10817 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10818 | arguments start and end are interpreted as in slice notation.\n\ |
| 10819 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10820 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10821 | |
| 10822 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10823 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10824 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10825 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10826 | Py_ssize_t start; |
| 10827 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10828 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10829 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10830 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10831 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10832 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10833 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10834 | if (PyUnicode_READY(self) == -1) |
| 10835 | return NULL; |
| 10836 | if (PyUnicode_READY(substring) == -1) |
| 10837 | return NULL; |
| 10838 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10839 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10840 | |
| 10841 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10842 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10843 | if (result == -2) |
| 10844 | return NULL; |
| 10845 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10846 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10847 | } |
| 10848 | |
| 10849 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10850 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10851 | { |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10852 | void *data; |
| 10853 | enum PyUnicode_Kind kind; |
| 10854 | Py_UCS4 ch; |
| 10855 | PyObject *res; |
| 10856 | |
| 10857 | if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { |
| 10858 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10859 | return NULL; |
Victor Stinner | b6cd014 | 2012-05-03 02:17:04 +0200 | [diff] [blame] | 10860 | } |
| 10861 | if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { |
| 10862 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10863 | return NULL; |
| 10864 | } |
| 10865 | kind = PyUnicode_KIND(self); |
| 10866 | data = PyUnicode_DATA(self); |
| 10867 | ch = PyUnicode_READ(kind, data, index); |
| 10868 | if (ch < 256) |
| 10869 | return get_latin1_char(ch); |
| 10870 | |
| 10871 | res = PyUnicode_New(1, ch); |
| 10872 | if (res == NULL) |
| 10873 | return NULL; |
| 10874 | kind = PyUnicode_KIND(res); |
| 10875 | data = PyUnicode_DATA(res); |
| 10876 | PyUnicode_WRITE(kind, data, 0, ch); |
| 10877 | assert(_PyUnicode_CheckConsistency(res, 1)); |
| 10878 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10879 | } |
| 10880 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10881 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10882 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10883 | static Py_hash_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10884 | unicode_hash(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10885 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10886 | Py_ssize_t len; |
Gregory P. Smith | 27cbcd6 | 2012-12-10 18:15:46 -0800 | [diff] [blame] | 10887 | Py_uhash_t x; /* Unsigned for defined overflow behavior. */ |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10888 | |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 10889 | #ifdef Py_DEBUG |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 10890 | assert(_Py_HashSecret_Initialized); |
Benjamin Peterson | f6622c8 | 2012-04-09 14:53:07 -0400 | [diff] [blame] | 10891 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10892 | if (_PyUnicode_HASH(self) != -1) |
| 10893 | return _PyUnicode_HASH(self); |
| 10894 | if (PyUnicode_READY(self) == -1) |
| 10895 | return -1; |
| 10896 | len = PyUnicode_GET_LENGTH(self); |
Georg Brandl | 16fa2a1 | 2012-02-21 00:50:13 +0100 | [diff] [blame] | 10897 | /* |
| 10898 | We make the hash of the empty string be 0, rather than using |
| 10899 | (prefix ^ suffix), since this slightly obfuscates the hash secret |
| 10900 | */ |
| 10901 | if (len == 0) { |
| 10902 | _PyUnicode_HASH(self) = 0; |
| 10903 | return 0; |
| 10904 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10905 | |
| 10906 | /* The hash function as a macro, gets expanded three times below. */ |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10907 | #define HASH(P) \ |
| 10908 | x ^= (Py_uhash_t) *P << 7; \ |
| 10909 | while (--len >= 0) \ |
| 10910 | x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10911 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10912 | x = (Py_uhash_t) _Py_HashSecret.prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10913 | switch (PyUnicode_KIND(self)) { |
| 10914 | case PyUnicode_1BYTE_KIND: { |
| 10915 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10916 | HASH(c); |
| 10917 | break; |
| 10918 | } |
| 10919 | case PyUnicode_2BYTE_KIND: { |
| 10920 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10921 | HASH(s); |
| 10922 | break; |
| 10923 | } |
| 10924 | default: { |
| 10925 | Py_UCS4 *l; |
| 10926 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10927 | "Impossible switch case in unicode_hash"); |
| 10928 | l = PyUnicode_4BYTE_DATA(self); |
| 10929 | HASH(l); |
| 10930 | break; |
| 10931 | } |
| 10932 | } |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 10933 | x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self); |
| 10934 | x ^= (Py_uhash_t) _Py_HashSecret.suffix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10935 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10936 | if (x == -1) |
| 10937 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10938 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10939 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10940 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10941 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10943 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10944 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10945 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10946 | 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] | 10947 | |
| 10948 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10949 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10950 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10951 | Py_ssize_t result; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10952 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10953 | Py_ssize_t start; |
| 10954 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10955 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10956 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10957 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10958 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10959 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10960 | if (PyUnicode_READY(self) == -1) |
| 10961 | return NULL; |
| 10962 | if (PyUnicode_READY(substring) == -1) |
| 10963 | return NULL; |
| 10964 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 10965 | result = any_find_slice(1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10966 | |
| 10967 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10968 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10969 | if (result == -2) |
| 10970 | return NULL; |
| 10971 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10972 | if (result < 0) { |
| 10973 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10974 | return NULL; |
| 10975 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10976 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10977 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10978 | } |
| 10979 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10980 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10981 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10982 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10983 | 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] | 10984 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10985 | |
| 10986 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 10987 | unicode_islower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10988 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10989 | Py_ssize_t i, length; |
| 10990 | int kind; |
| 10991 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10992 | int cased; |
| 10993 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10994 | if (PyUnicode_READY(self) == -1) |
| 10995 | return NULL; |
| 10996 | length = PyUnicode_GET_LENGTH(self); |
| 10997 | kind = PyUnicode_KIND(self); |
| 10998 | data = PyUnicode_DATA(self); |
| 10999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11000 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11001 | if (length == 1) |
| 11002 | return PyBool_FromLong( |
| 11003 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11004 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11005 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11006 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11007 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11009 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11010 | for (i = 0; i < length; i++) { |
| 11011 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11012 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11013 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11014 | return PyBool_FromLong(0); |
| 11015 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 11016 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11017 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11018 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11019 | } |
| 11020 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11021 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11022 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11023 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11024 | 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] | 11025 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11026 | |
| 11027 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11028 | unicode_isupper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11029 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11030 | Py_ssize_t i, length; |
| 11031 | int kind; |
| 11032 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11033 | int cased; |
| 11034 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11035 | if (PyUnicode_READY(self) == -1) |
| 11036 | return NULL; |
| 11037 | length = PyUnicode_GET_LENGTH(self); |
| 11038 | kind = PyUnicode_KIND(self); |
| 11039 | data = PyUnicode_DATA(self); |
| 11040 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11041 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11042 | if (length == 1) |
| 11043 | return PyBool_FromLong( |
| 11044 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11045 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11046 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11047 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11048 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11049 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11050 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11051 | for (i = 0; i < length; i++) { |
| 11052 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11053 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11054 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 11055 | return PyBool_FromLong(0); |
| 11056 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 11057 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11058 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11059 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11060 | } |
| 11061 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11062 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11063 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11064 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11065 | Return True if S is a titlecased string and there is at least one\n\ |
| 11066 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 11067 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 11068 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11069 | |
| 11070 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11071 | unicode_istitle(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11072 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11073 | Py_ssize_t i, length; |
| 11074 | int kind; |
| 11075 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11076 | int cased, previous_is_cased; |
| 11077 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11078 | if (PyUnicode_READY(self) == -1) |
| 11079 | return NULL; |
| 11080 | length = PyUnicode_GET_LENGTH(self); |
| 11081 | kind = PyUnicode_KIND(self); |
| 11082 | data = PyUnicode_DATA(self); |
| 11083 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11084 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11085 | if (length == 1) { |
| 11086 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11087 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 11088 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 11089 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11090 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11091 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11092 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11093 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11094 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11095 | cased = 0; |
| 11096 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11097 | for (i = 0; i < length; i++) { |
| 11098 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11099 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11100 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 11101 | if (previous_is_cased) |
| 11102 | return PyBool_FromLong(0); |
| 11103 | previous_is_cased = 1; |
| 11104 | cased = 1; |
| 11105 | } |
| 11106 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 11107 | if (!previous_is_cased) |
| 11108 | return PyBool_FromLong(0); |
| 11109 | previous_is_cased = 1; |
| 11110 | cased = 1; |
| 11111 | } |
| 11112 | else |
| 11113 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11114 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11115 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11116 | } |
| 11117 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11118 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11119 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11120 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11121 | Return True if all characters in S are whitespace\n\ |
| 11122 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11123 | |
| 11124 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11125 | unicode_isspace(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11126 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11127 | Py_ssize_t i, length; |
| 11128 | int kind; |
| 11129 | void *data; |
| 11130 | |
| 11131 | if (PyUnicode_READY(self) == -1) |
| 11132 | return NULL; |
| 11133 | length = PyUnicode_GET_LENGTH(self); |
| 11134 | kind = PyUnicode_KIND(self); |
| 11135 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11137 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11138 | if (length == 1) |
| 11139 | return PyBool_FromLong( |
| 11140 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11141 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11142 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11143 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11144 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11145 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11146 | for (i = 0; i < length; i++) { |
| 11147 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11148 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11149 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11150 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11151 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11152 | } |
| 11153 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11154 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11155 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11156 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11157 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11158 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11159 | |
| 11160 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11161 | unicode_isalpha(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11162 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11163 | Py_ssize_t i, length; |
| 11164 | int kind; |
| 11165 | void *data; |
| 11166 | |
| 11167 | if (PyUnicode_READY(self) == -1) |
| 11168 | return NULL; |
| 11169 | length = PyUnicode_GET_LENGTH(self); |
| 11170 | kind = PyUnicode_KIND(self); |
| 11171 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11172 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11173 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11174 | if (length == 1) |
| 11175 | return PyBool_FromLong( |
| 11176 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11177 | |
| 11178 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11179 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11180 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11181 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11182 | for (i = 0; i < length; i++) { |
| 11183 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11184 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11185 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11186 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11187 | } |
| 11188 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11189 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11190 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11191 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11192 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11193 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11194 | |
| 11195 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11196 | unicode_isalnum(PyObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11197 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11198 | int kind; |
| 11199 | void *data; |
| 11200 | Py_ssize_t len, i; |
| 11201 | |
| 11202 | if (PyUnicode_READY(self) == -1) |
| 11203 | return NULL; |
| 11204 | |
| 11205 | kind = PyUnicode_KIND(self); |
| 11206 | data = PyUnicode_DATA(self); |
| 11207 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11208 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11209 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11210 | if (len == 1) { |
| 11211 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11212 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 11213 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11214 | |
| 11215 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11216 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11217 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11218 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11219 | for (i = 0; i < len; i++) { |
| 11220 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11221 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11222 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11223 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11224 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 11225 | } |
| 11226 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11227 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11228 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11229 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11230 | 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] | 11231 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11232 | |
| 11233 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11234 | unicode_isdecimal(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11235 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11236 | Py_ssize_t i, length; |
| 11237 | int kind; |
| 11238 | void *data; |
| 11239 | |
| 11240 | if (PyUnicode_READY(self) == -1) |
| 11241 | return NULL; |
| 11242 | length = PyUnicode_GET_LENGTH(self); |
| 11243 | kind = PyUnicode_KIND(self); |
| 11244 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11245 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11246 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11247 | if (length == 1) |
| 11248 | return PyBool_FromLong( |
| 11249 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11250 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11251 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11252 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11253 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11254 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11255 | for (i = 0; i < length; i++) { |
| 11256 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11257 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11258 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11259 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11260 | } |
| 11261 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11262 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11263 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11264 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 11265 | Return True if all characters in S are digits\n\ |
| 11266 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11267 | |
| 11268 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11269 | unicode_isdigit(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11270 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11271 | Py_ssize_t i, length; |
| 11272 | int kind; |
| 11273 | void *data; |
| 11274 | |
| 11275 | if (PyUnicode_READY(self) == -1) |
| 11276 | return NULL; |
| 11277 | length = PyUnicode_GET_LENGTH(self); |
| 11278 | kind = PyUnicode_KIND(self); |
| 11279 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11280 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11281 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11282 | if (length == 1) { |
| 11283 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 11284 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 11285 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11286 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11287 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11288 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11289 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11290 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11291 | for (i = 0; i < length; i++) { |
| 11292 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11293 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11294 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11295 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11296 | } |
| 11297 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11298 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11299 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11300 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11301 | 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] | 11302 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 | |
| 11304 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11305 | unicode_isnumeric(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11306 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11307 | Py_ssize_t i, length; |
| 11308 | int kind; |
| 11309 | void *data; |
| 11310 | |
| 11311 | if (PyUnicode_READY(self) == -1) |
| 11312 | return NULL; |
| 11313 | length = PyUnicode_GET_LENGTH(self); |
| 11314 | kind = PyUnicode_KIND(self); |
| 11315 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11316 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11317 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11318 | if (length == 1) |
| 11319 | return PyBool_FromLong( |
| 11320 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11321 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11322 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11323 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11324 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 11325 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11326 | for (i = 0; i < length; i++) { |
| 11327 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11328 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11329 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 11330 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11331 | } |
| 11332 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11333 | int |
| 11334 | PyUnicode_IsIdentifier(PyObject *self) |
| 11335 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11336 | int kind; |
| 11337 | void *data; |
| 11338 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 11339 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11340 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11341 | if (PyUnicode_READY(self) == -1) { |
| 11342 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11343 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11344 | } |
| 11345 | |
| 11346 | /* Special case for empty strings */ |
| 11347 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 11348 | return 0; |
| 11349 | kind = PyUnicode_KIND(self); |
| 11350 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11351 | |
| 11352 | /* PEP 3131 says that the first character must be in |
| 11353 | XID_Start and subsequent characters in XID_Continue, |
| 11354 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11355 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11356 | letters, digits, underscore). However, given the current |
| 11357 | definition of XID_Start and XID_Continue, it is sufficient |
| 11358 | to check just for these, except that _ must be allowed |
| 11359 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11360 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 11361 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11362 | return 0; |
| 11363 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 11364 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11365 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11366 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11367 | return 1; |
| 11368 | } |
| 11369 | |
| 11370 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11371 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 11372 | \n\ |
| 11373 | Return True if S is a valid identifier according\n\ |
| 11374 | to the language definition."); |
| 11375 | |
| 11376 | static PyObject* |
| 11377 | unicode_isidentifier(PyObject *self) |
| 11378 | { |
| 11379 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 11380 | } |
| 11381 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11382 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11383 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11384 | \n\ |
| 11385 | Return True if all characters in S are considered\n\ |
| 11386 | printable in repr() or S is empty, False otherwise."); |
| 11387 | |
| 11388 | static PyObject* |
| 11389 | unicode_isprintable(PyObject *self) |
| 11390 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11391 | Py_ssize_t i, length; |
| 11392 | int kind; |
| 11393 | void *data; |
| 11394 | |
| 11395 | if (PyUnicode_READY(self) == -1) |
| 11396 | return NULL; |
| 11397 | length = PyUnicode_GET_LENGTH(self); |
| 11398 | kind = PyUnicode_KIND(self); |
| 11399 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11400 | |
| 11401 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11402 | if (length == 1) |
| 11403 | return PyBool_FromLong( |
| 11404 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11405 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11406 | for (i = 0; i < length; i++) { |
| 11407 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11408 | Py_RETURN_FALSE; |
| 11409 | } |
| 11410 | } |
| 11411 | Py_RETURN_TRUE; |
| 11412 | } |
| 11413 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11414 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 11415 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11416 | \n\ |
| 11417 | 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] | 11418 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11419 | |
| 11420 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11421 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11422 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11423 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11424 | } |
| 11425 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11426 | static Py_ssize_t |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11427 | unicode_length(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11428 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11429 | if (PyUnicode_READY(self) == -1) |
| 11430 | return -1; |
| 11431 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11432 | } |
| 11433 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11434 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11435 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11436 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11437 | 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] | 11438 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | |
| 11440 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11441 | unicode_ljust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11442 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11443 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11444 | Py_UCS4 fillchar = ' '; |
| 11445 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11446 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11447 | return NULL; |
| 11448 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11449 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11450 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11451 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11452 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 11453 | return unicode_result_unchanged(self); |
| 11454 | |
| 11455 | return pad(self, 0, width - PyUnicode_GET_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11456 | } |
| 11457 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11458 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11459 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11460 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11461 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11462 | |
| 11463 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 11464 | unicode_lower(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11465 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 11466 | if (PyUnicode_READY(self) == -1) |
| 11467 | return NULL; |
| 11468 | if (PyUnicode_IS_ASCII(self)) |
| 11469 | return ascii_upper_or_lower(self, 1); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 11470 | return case_operation(self, do_lower); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11471 | } |
| 11472 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11473 | #define LEFTSTRIP 0 |
| 11474 | #define RIGHTSTRIP 1 |
| 11475 | #define BOTHSTRIP 2 |
| 11476 | |
| 11477 | /* Arrays indexed by above */ |
| 11478 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 11479 | |
| 11480 | #define STRIPNAME(i) (stripformat[i]+3) |
| 11481 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11482 | /* externally visible for str.strip(unicode) */ |
| 11483 | PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11484 | _PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11485 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11486 | void *data; |
| 11487 | int kind; |
| 11488 | Py_ssize_t i, j, len; |
| 11489 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11490 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11491 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 11492 | return NULL; |
| 11493 | |
| 11494 | kind = PyUnicode_KIND(self); |
| 11495 | data = PyUnicode_DATA(self); |
| 11496 | len = PyUnicode_GET_LENGTH(self); |
| 11497 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 11498 | PyUnicode_DATA(sepobj), |
| 11499 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11500 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11501 | i = 0; |
| 11502 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11503 | while (i < len && |
| 11504 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11505 | i++; |
| 11506 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11507 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11508 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11509 | j = len; |
| 11510 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11511 | do { |
| 11512 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11513 | } while (j >= i && |
| 11514 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11515 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11516 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11517 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11518 | return PyUnicode_Substring(self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11519 | } |
| 11520 | |
| 11521 | PyObject* |
| 11522 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 11523 | { |
| 11524 | unsigned char *data; |
| 11525 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11526 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11527 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11528 | if (PyUnicode_READY(self) == -1) |
| 11529 | return NULL; |
| 11530 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11531 | length = PyUnicode_GET_LENGTH(self); |
| 11532 | end = Py_MIN(end, length); |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11533 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11534 | if (start == 0 && end == length) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11535 | return unicode_result_unchanged(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11536 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 11537 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11538 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 11539 | return NULL; |
| 11540 | } |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11541 | if (start >= length || end < start) { |
Victor Stinner | 3a7f7977 | 2012-05-03 03:36:40 +0200 | [diff] [blame] | 11542 | Py_INCREF(unicode_empty); |
| 11543 | return unicode_empty; |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11544 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11545 | |
Victor Stinner | 684d5fd | 2012-05-03 02:32:34 +0200 | [diff] [blame] | 11546 | length = end - start; |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11547 | if (PyUnicode_IS_ASCII(self)) { |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11548 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 11549 | return _PyUnicode_FromASCII((char*)(data + start), length); |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11550 | } |
| 11551 | else { |
| 11552 | kind = PyUnicode_KIND(self); |
| 11553 | data = PyUnicode_1BYTE_DATA(self); |
| 11554 | return PyUnicode_FromKindAndData(kind, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11555 | data + kind * start, |
Victor Stinner | b9275c1 | 2011-10-05 14:01:42 +0200 | [diff] [blame] | 11556 | length); |
| 11557 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11558 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11559 | |
| 11560 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11561 | do_strip(PyObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11562 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11563 | int kind; |
| 11564 | void *data; |
| 11565 | Py_ssize_t len, i, j; |
| 11566 | |
| 11567 | if (PyUnicode_READY(self) == -1) |
| 11568 | return NULL; |
| 11569 | |
| 11570 | kind = PyUnicode_KIND(self); |
| 11571 | data = PyUnicode_DATA(self); |
| 11572 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11573 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11574 | i = 0; |
| 11575 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11576 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11577 | i++; |
| 11578 | } |
| 11579 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11580 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11581 | j = len; |
| 11582 | if (striptype != LEFTSTRIP) { |
| 11583 | do { |
| 11584 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11585 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11586 | j++; |
| 11587 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11588 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11589 | return PyUnicode_Substring(self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11590 | } |
| 11591 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11592 | |
| 11593 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11594 | do_argstrip(PyObject *self, int striptype, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11595 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11596 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11597 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11598 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11599 | return NULL; |
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 | if (sep != NULL && sep != Py_None) { |
| 11602 | if (PyUnicode_Check(sep)) |
| 11603 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11604 | else { |
| 11605 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11606 | "%s arg must be None or str", |
| 11607 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11608 | return NULL; |
| 11609 | } |
| 11610 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11611 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11612 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11613 | } |
| 11614 | |
| 11615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11616 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11617 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11618 | \n\ |
| 11619 | Return a copy of the string S with leading and trailing\n\ |
| 11620 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11621 | 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] | 11622 | |
| 11623 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11624 | unicode_strip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11625 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11626 | if (PyTuple_GET_SIZE(args) == 0) |
| 11627 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11628 | else |
| 11629 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11630 | } |
| 11631 | |
| 11632 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11633 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11634 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11635 | \n\ |
| 11636 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11637 | 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] | 11638 | |
| 11639 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11640 | unicode_lstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11641 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11642 | if (PyTuple_GET_SIZE(args) == 0) |
| 11643 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11644 | else |
| 11645 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11646 | } |
| 11647 | |
| 11648 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11649 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11650 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11651 | \n\ |
| 11652 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11653 | 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] | 11654 | |
| 11655 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11656 | unicode_rstrip(PyObject *self, PyObject *args) |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11657 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11658 | if (PyTuple_GET_SIZE(args) == 0) |
| 11659 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11660 | else |
| 11661 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11662 | } |
| 11663 | |
| 11664 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11665 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11666 | unicode_repeat(PyObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11667 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11668 | PyObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11669 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11670 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11671 | if (len < 1) { |
| 11672 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11673 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11674 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11675 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 11676 | /* no repeat, return original string */ |
| 11677 | if (len == 1) |
| 11678 | return unicode_result_unchanged(str); |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11679 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 11680 | if (PyUnicode_READY(str) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11681 | return NULL; |
| 11682 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11683 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11684 | PyErr_SetString(PyExc_OverflowError, |
| 11685 | "repeated string is too long"); |
| 11686 | return NULL; |
| 11687 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11688 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11689 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11690 | u = PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11691 | if (!u) |
| 11692 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11693 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11694 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11695 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11696 | const int kind = PyUnicode_KIND(str); |
| 11697 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11698 | if (kind == PyUnicode_1BYTE_KIND) { |
| 11699 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11700 | memset(to, (unsigned char)fill_char, len); |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11701 | } |
| 11702 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 11703 | Py_UCS2 *ucs2 = PyUnicode_2BYTE_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11704 | for (n = 0; n < len; ++n) |
Victor Stinner | 73f53b5 | 2011-12-18 03:26:31 +0100 | [diff] [blame] | 11705 | ucs2[n] = fill_char; |
| 11706 | } else { |
| 11707 | Py_UCS4 *ucs4 = PyUnicode_4BYTE_DATA(u); |
| 11708 | assert(kind == PyUnicode_4BYTE_KIND); |
| 11709 | for (n = 0; n < len; ++n) |
| 11710 | ucs4[n] = fill_char; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11711 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11712 | } |
| 11713 | else { |
| 11714 | /* number of characters copied this far */ |
| 11715 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 11716 | const Py_ssize_t char_size = PyUnicode_KIND(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11717 | char *to = (char *) PyUnicode_DATA(u); |
| 11718 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11719 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11720 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11721 | n = (done <= nchars-done) ? done : nchars-done; |
| 11722 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11723 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11724 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11725 | } |
| 11726 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 11727 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11728 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11729 | } |
| 11730 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11731 | PyObject * |
| 11732 | PyUnicode_Replace(PyObject *obj, |
| 11733 | PyObject *subobj, |
| 11734 | PyObject *replobj, |
| 11735 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11736 | { |
| 11737 | PyObject *self; |
| 11738 | PyObject *str1; |
| 11739 | PyObject *str2; |
| 11740 | PyObject *result; |
| 11741 | |
| 11742 | self = PyUnicode_FromObject(obj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11743 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11744 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11745 | str1 = PyUnicode_FromObject(subobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11746 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11747 | Py_DECREF(self); |
| 11748 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11749 | } |
| 11750 | str2 = PyUnicode_FromObject(replobj); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11751 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11752 | Py_DECREF(self); |
| 11753 | Py_DECREF(str1); |
| 11754 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11755 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11756 | if (PyUnicode_READY(self) == -1 || |
| 11757 | PyUnicode_READY(str1) == -1 || |
| 11758 | PyUnicode_READY(str2) == -1) |
| 11759 | result = NULL; |
| 11760 | else |
| 11761 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11762 | Py_DECREF(self); |
| 11763 | Py_DECREF(str1); |
| 11764 | Py_DECREF(str2); |
| 11765 | return result; |
| 11766 | } |
| 11767 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11768 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11769 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11770 | \n\ |
| 11771 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11772 | old replaced by new. If the optional argument count is\n\ |
| 11773 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11774 | |
| 11775 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11776 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11777 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11778 | PyObject *str1; |
| 11779 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11780 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11781 | PyObject *result; |
| 11782 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11783 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11784 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11785 | if (PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11786 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11787 | str1 = PyUnicode_FromObject(str1); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11788 | if (str1 == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11789 | return NULL; |
| 11790 | str2 = PyUnicode_FromObject(str2); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11791 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11792 | Py_DECREF(str1); |
| 11793 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11794 | } |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 11795 | if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1) |
| 11796 | result = NULL; |
| 11797 | else |
| 11798 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11799 | |
| 11800 | Py_DECREF(str1); |
| 11801 | Py_DECREF(str2); |
| 11802 | return result; |
| 11803 | } |
| 11804 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11805 | static PyObject * |
| 11806 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11807 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11808 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11809 | Py_ssize_t isize; |
| 11810 | Py_ssize_t osize, squote, dquote, i, o; |
| 11811 | Py_UCS4 max, quote; |
| 11812 | int ikind, okind; |
| 11813 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11814 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11815 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11816 | return NULL; |
| 11817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11818 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11819 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11820 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11821 | /* Compute length of output, quote characters, and |
| 11822 | maximum character */ |
| 11823 | osize = 2; /* quotes */ |
| 11824 | max = 127; |
| 11825 | squote = dquote = 0; |
| 11826 | ikind = PyUnicode_KIND(unicode); |
| 11827 | for (i = 0; i < isize; i++) { |
| 11828 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11829 | switch (ch) { |
| 11830 | case '\'': squote++; osize++; break; |
| 11831 | case '"': dquote++; osize++; break; |
| 11832 | case '\\': case '\t': case '\r': case '\n': |
| 11833 | osize += 2; break; |
| 11834 | default: |
| 11835 | /* Fast-path ASCII */ |
| 11836 | if (ch < ' ' || ch == 0x7f) |
| 11837 | osize += 4; /* \xHH */ |
| 11838 | else if (ch < 0x7f) |
| 11839 | osize++; |
| 11840 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11841 | osize++; |
| 11842 | max = ch > max ? ch : max; |
| 11843 | } |
| 11844 | else if (ch < 0x100) |
| 11845 | osize += 4; /* \xHH */ |
| 11846 | else if (ch < 0x10000) |
| 11847 | osize += 6; /* \uHHHH */ |
| 11848 | else |
| 11849 | osize += 10; /* \uHHHHHHHH */ |
| 11850 | } |
| 11851 | } |
| 11852 | |
| 11853 | quote = '\''; |
| 11854 | if (squote) { |
| 11855 | if (dquote) |
| 11856 | /* Both squote and dquote present. Use squote, |
| 11857 | and escape them */ |
| 11858 | osize += squote; |
| 11859 | else |
| 11860 | quote = '"'; |
| 11861 | } |
| 11862 | |
| 11863 | repr = PyUnicode_New(osize, max); |
| 11864 | if (repr == NULL) |
| 11865 | return NULL; |
| 11866 | okind = PyUnicode_KIND(repr); |
| 11867 | odata = PyUnicode_DATA(repr); |
| 11868 | |
| 11869 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11870 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11871 | |
| 11872 | for (i = 0, o = 1; i < isize; i++) { |
| 11873 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11874 | |
| 11875 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11876 | if ((ch == quote) || (ch == '\\')) { |
| 11877 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11878 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11879 | continue; |
| 11880 | } |
| 11881 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11882 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11883 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11884 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11885 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11886 | } |
| 11887 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11888 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11889 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11890 | } |
| 11891 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11892 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11893 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11894 | } |
| 11895 | |
| 11896 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11897 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11898 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11899 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11900 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 11901 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11902 | } |
| 11903 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11904 | /* Copy ASCII characters as-is */ |
| 11905 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11906 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11907 | } |
| 11908 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11909 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11910 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11911 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11912 | (categories Z* and C* except ASCII space) |
| 11913 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11914 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11915 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11916 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11917 | if (ch <= 0xff) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11918 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11919 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); |
| 11920 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11921 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11922 | /* Map 16-bit characters to '\uxxxx' */ |
| 11923 | else if (ch <= 0xffff) { |
| 11924 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11925 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 11926 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 11927 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 11928 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11929 | } |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11930 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11931 | else { |
Kristján Valur Jónsson | 55e5dc8 | 2012-06-06 21:58:08 +0000 | [diff] [blame] | 11932 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11933 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); |
| 11934 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); |
| 11935 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 20) & 0xF]); |
| 11936 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 16) & 0xF]); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 11937 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); |
| 11938 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); |
| 11939 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); |
| 11940 | PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11941 | } |
| 11942 | } |
| 11943 | /* Copy characters as-is */ |
| 11944 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11945 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11946 | } |
| 11947 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11948 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11949 | /* Closing quote already added at the beginning */ |
Victor Stinner | 05d1189 | 2011-10-06 01:13:58 +0200 | [diff] [blame] | 11950 | assert(_PyUnicode_CheckConsistency(repr, 1)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11951 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11952 | } |
| 11953 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11954 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11955 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11956 | \n\ |
| 11957 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11958 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11959 | arguments start and end are interpreted as in slice notation.\n\ |
| 11960 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11961 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11962 | |
| 11963 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11964 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11965 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11966 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11967 | Py_ssize_t start; |
| 11968 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11969 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11970 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11971 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11972 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11973 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11974 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11975 | if (PyUnicode_READY(self) == -1) |
| 11976 | return NULL; |
| 11977 | if (PyUnicode_READY(substring) == -1) |
| 11978 | return NULL; |
| 11979 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 11980 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11981 | |
| 11982 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11983 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11984 | if (result == -2) |
| 11985 | return NULL; |
| 11986 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11987 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11988 | } |
| 11989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11990 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11991 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11992 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11993 | 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] | 11994 | |
| 11995 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11996 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11997 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 11998 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11999 | Py_ssize_t start; |
| 12000 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12001 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12002 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12003 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 12004 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12005 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12006 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12007 | if (PyUnicode_READY(self) == -1) |
| 12008 | return NULL; |
| 12009 | if (PyUnicode_READY(substring) == -1) |
| 12010 | return NULL; |
| 12011 | |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12012 | result = any_find_slice(-1, self, substring, start, end); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12013 | |
| 12014 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12015 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12016 | if (result == -2) |
| 12017 | return NULL; |
| 12018 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12019 | if (result < 0) { |
| 12020 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 12021 | return NULL; |
| 12022 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12023 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12024 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12025 | } |
| 12026 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12027 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12028 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12029 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 12030 | 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] | 12031 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12032 | |
| 12033 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12034 | unicode_rjust(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12035 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12036 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12037 | Py_UCS4 fillchar = ' '; |
| 12038 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 12039 | 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] | 12040 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 12041 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12042 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12043 | return NULL; |
| 12044 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12045 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12046 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12047 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12048 | return pad(self, width - PyUnicode_GET_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12049 | } |
| 12050 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12051 | PyObject * |
| 12052 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12053 | { |
| 12054 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12055 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12056 | s = PyUnicode_FromObject(s); |
| 12057 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12058 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12059 | if (sep != NULL) { |
| 12060 | sep = PyUnicode_FromObject(sep); |
| 12061 | if (sep == NULL) { |
| 12062 | Py_DECREF(s); |
| 12063 | return NULL; |
| 12064 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12065 | } |
| 12066 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12067 | result = split(s, sep, maxsplit); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12068 | |
| 12069 | Py_DECREF(s); |
| 12070 | Py_XDECREF(sep); |
| 12071 | return result; |
| 12072 | } |
| 12073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12074 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12075 | "S.split(sep=None, maxsplit=-1) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12076 | \n\ |
| 12077 | Return a list of the words in S, using sep as the\n\ |
| 12078 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 12079 | 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] | 12080 | whitespace string is a separator and empty strings are\n\ |
| 12081 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12082 | |
| 12083 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12084 | unicode_split(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12085 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12086 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12087 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12088 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12089 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12090 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 12091 | kwlist, &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12092 | return NULL; |
| 12093 | |
| 12094 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12095 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12096 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12097 | return split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12098 | else |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12099 | return PyUnicode_Split(self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12100 | } |
| 12101 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12102 | PyObject * |
| 12103 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 12104 | { |
| 12105 | PyObject* str_obj; |
| 12106 | PyObject* sep_obj; |
| 12107 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12108 | int kind1, kind2, kind; |
| 12109 | void *buf1 = NULL, *buf2 = NULL; |
| 12110 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12111 | |
| 12112 | str_obj = PyUnicode_FromObject(str_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12113 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12114 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12115 | sep_obj = PyUnicode_FromObject(sep_in); |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 12116 | if (!sep_obj) { |
| 12117 | Py_DECREF(str_obj); |
| 12118 | return NULL; |
| 12119 | } |
| 12120 | if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) { |
| 12121 | Py_DECREF(sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12122 | Py_DECREF(str_obj); |
| 12123 | return NULL; |
| 12124 | } |
| 12125 | |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12126 | kind1 = PyUnicode_KIND(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12127 | kind2 = PyUnicode_KIND(sep_obj); |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12128 | kind = Py_MAX(kind1, kind2); |
| 12129 | buf1 = PyUnicode_DATA(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12130 | if (kind1 != kind) |
Victor Stinner | 14f8f02 | 2011-10-05 20:58:25 +0200 | [diff] [blame] | 12131 | buf1 = _PyUnicode_AsKind(str_obj, kind); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12132 | if (!buf1) |
| 12133 | goto onError; |
| 12134 | buf2 = PyUnicode_DATA(sep_obj); |
| 12135 | if (kind2 != kind) |
| 12136 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12137 | if (!buf2) |
| 12138 | goto onError; |
| 12139 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12140 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12141 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12142 | switch (PyUnicode_KIND(str_obj)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12143 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12144 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12145 | out = asciilib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12146 | else |
| 12147 | 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] | 12148 | break; |
| 12149 | case PyUnicode_2BYTE_KIND: |
| 12150 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12151 | break; |
| 12152 | case PyUnicode_4BYTE_KIND: |
| 12153 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12154 | break; |
| 12155 | default: |
| 12156 | assert(0); |
| 12157 | out = 0; |
| 12158 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12159 | |
| 12160 | Py_DECREF(sep_obj); |
| 12161 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12162 | if (kind1 != kind) |
| 12163 | PyMem_Free(buf1); |
| 12164 | if (kind2 != kind) |
| 12165 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12166 | |
| 12167 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12168 | onError: |
| 12169 | Py_DECREF(sep_obj); |
| 12170 | Py_DECREF(str_obj); |
| 12171 | if (kind1 != kind && buf1) |
| 12172 | PyMem_Free(buf1); |
| 12173 | if (kind2 != kind && buf2) |
| 12174 | PyMem_Free(buf2); |
| 12175 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12176 | } |
| 12177 | |
| 12178 | |
| 12179 | PyObject * |
| 12180 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 12181 | { |
| 12182 | PyObject* str_obj; |
| 12183 | PyObject* sep_obj; |
| 12184 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12185 | int kind1, kind2, kind; |
| 12186 | void *buf1 = NULL, *buf2 = NULL; |
| 12187 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12188 | |
| 12189 | str_obj = PyUnicode_FromObject(str_in); |
| 12190 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12191 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12192 | sep_obj = PyUnicode_FromObject(sep_in); |
| 12193 | if (!sep_obj) { |
| 12194 | Py_DECREF(str_obj); |
| 12195 | return NULL; |
| 12196 | } |
| 12197 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12198 | kind1 = PyUnicode_KIND(str_in); |
| 12199 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 12200 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12201 | buf1 = PyUnicode_DATA(str_in); |
| 12202 | if (kind1 != kind) |
| 12203 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 12204 | if (!buf1) |
| 12205 | goto onError; |
| 12206 | buf2 = PyUnicode_DATA(sep_obj); |
| 12207 | if (kind2 != kind) |
| 12208 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 12209 | if (!buf2) |
| 12210 | goto onError; |
| 12211 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 12212 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 12213 | |
Benjamin Peterson | ead6b53 | 2011-12-20 17:23:42 -0600 | [diff] [blame] | 12214 | switch (PyUnicode_KIND(str_in)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12215 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | c3cec78 | 2011-10-05 21:24:08 +0200 | [diff] [blame] | 12216 | if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sep_obj)) |
| 12217 | out = asciilib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12218 | else |
| 12219 | 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] | 12220 | break; |
| 12221 | case PyUnicode_2BYTE_KIND: |
| 12222 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12223 | break; |
| 12224 | case PyUnicode_4BYTE_KIND: |
| 12225 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 12226 | break; |
| 12227 | default: |
| 12228 | assert(0); |
| 12229 | out = 0; |
| 12230 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12231 | |
| 12232 | Py_DECREF(sep_obj); |
| 12233 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12234 | if (kind1 != kind) |
| 12235 | PyMem_Free(buf1); |
| 12236 | if (kind2 != kind) |
| 12237 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12238 | |
| 12239 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12240 | onError: |
| 12241 | Py_DECREF(sep_obj); |
| 12242 | Py_DECREF(str_obj); |
| 12243 | if (kind1 != kind && buf1) |
| 12244 | PyMem_Free(buf1); |
| 12245 | if (kind2 != kind && buf2) |
| 12246 | PyMem_Free(buf2); |
| 12247 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12248 | } |
| 12249 | |
| 12250 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12251 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12252 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12253 | 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] | 12254 | 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] | 12255 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12256 | |
| 12257 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12258 | unicode_partition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12259 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12260 | return PyUnicode_Partition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12261 | } |
| 12262 | |
| 12263 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 12264 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12265 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 12266 | 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] | 12267 | 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] | 12268 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12269 | |
| 12270 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12271 | unicode_rpartition(PyObject *self, PyObject *separator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12272 | { |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12273 | return PyUnicode_RPartition(self, separator); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12274 | } |
| 12275 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12276 | PyObject * |
| 12277 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12278 | { |
| 12279 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12280 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12281 | s = PyUnicode_FromObject(s); |
| 12282 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12283 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12284 | if (sep != NULL) { |
| 12285 | sep = PyUnicode_FromObject(sep); |
| 12286 | if (sep == NULL) { |
| 12287 | Py_DECREF(s); |
| 12288 | return NULL; |
| 12289 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12290 | } |
| 12291 | |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12292 | result = rsplit(s, sep, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12293 | |
| 12294 | Py_DECREF(s); |
| 12295 | Py_XDECREF(sep); |
| 12296 | return result; |
| 12297 | } |
| 12298 | |
| 12299 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12300 | "S.rsplit(sep=None, maxsplit=-1) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12301 | \n\ |
| 12302 | Return a list of the words in S, using sep as the\n\ |
| 12303 | delimiter string, starting at the end of the string and\n\ |
| 12304 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 12305 | splits are done. If sep is not specified, any whitespace string\n\ |
| 12306 | is a separator."); |
| 12307 | |
| 12308 | static PyObject* |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12309 | unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12310 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12311 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12312 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12313 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12314 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12315 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 12316 | kwlist, &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12317 | return NULL; |
| 12318 | |
| 12319 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12320 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12321 | else if (PyUnicode_Check(substring)) |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12322 | return rsplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12323 | else |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12324 | return PyUnicode_RSplit(self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12325 | } |
| 12326 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12327 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12328 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12329 | \n\ |
| 12330 | 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] | 12331 | 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] | 12332 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12333 | |
| 12334 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12335 | unicode_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12336 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12337 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 12338 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12339 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12340 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 12341 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12342 | return NULL; |
| 12343 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12344 | return PyUnicode_Splitlines(self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12345 | } |
| 12346 | |
| 12347 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 12348 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12349 | { |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12350 | return unicode_result_unchanged(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12351 | } |
| 12352 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12353 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12354 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12355 | \n\ |
| 12356 | 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] | 12357 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12358 | |
| 12359 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12360 | unicode_swapcase(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12361 | { |
Benjamin Peterson | eea4846 | 2012-01-16 14:28:50 -0500 | [diff] [blame] | 12362 | if (PyUnicode_READY(self) == -1) |
| 12363 | return NULL; |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12364 | return case_operation(self, do_swapcase); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12365 | } |
| 12366 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12367 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12368 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12369 | \n\ |
| 12370 | Return a translation table usable for str.translate().\n\ |
| 12371 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 12372 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12373 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12374 | If there are two arguments, they must be strings of equal length, and\n\ |
| 12375 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 12376 | character at the same position in y. If there is a third argument, it\n\ |
| 12377 | must be a string, whose characters will be mapped to None in the result."); |
| 12378 | |
| 12379 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12380 | unicode_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12381 | { |
| 12382 | PyObject *x, *y = NULL, *z = NULL; |
| 12383 | PyObject *new = NULL, *key, *value; |
| 12384 | Py_ssize_t i = 0; |
| 12385 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12386 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12387 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 12388 | return NULL; |
| 12389 | new = PyDict_New(); |
| 12390 | if (!new) |
| 12391 | return NULL; |
| 12392 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12393 | int x_kind, y_kind, z_kind; |
| 12394 | void *x_data, *y_data, *z_data; |
| 12395 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12396 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12397 | if (!PyUnicode_Check(x)) { |
| 12398 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 12399 | "be a string if there is a second argument"); |
| 12400 | goto err; |
| 12401 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12402 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12403 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 12404 | "arguments must have equal length"); |
| 12405 | goto err; |
| 12406 | } |
| 12407 | /* 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] | 12408 | x_kind = PyUnicode_KIND(x); |
| 12409 | y_kind = PyUnicode_KIND(y); |
| 12410 | x_data = PyUnicode_DATA(x); |
| 12411 | y_data = PyUnicode_DATA(y); |
| 12412 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 12413 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12414 | if (!key) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12415 | goto err; |
Benjamin Peterson | 822c790 | 2011-12-20 13:32:50 -0600 | [diff] [blame] | 12416 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Benjamin Peterson | 53aa1d7 | 2011-12-20 13:29:45 -0600 | [diff] [blame] | 12417 | if (!value) { |
| 12418 | Py_DECREF(key); |
| 12419 | goto err; |
| 12420 | } |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12421 | res = PyDict_SetItem(new, key, value); |
| 12422 | Py_DECREF(key); |
| 12423 | Py_DECREF(value); |
| 12424 | if (res < 0) |
| 12425 | goto err; |
| 12426 | } |
| 12427 | /* create entries for deleting chars in z */ |
| 12428 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12429 | z_kind = PyUnicode_KIND(z); |
| 12430 | z_data = PyUnicode_DATA(z); |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12431 | for (i = 0; i < PyUnicode_GET_LENGTH(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12432 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12433 | if (!key) |
| 12434 | goto err; |
| 12435 | res = PyDict_SetItem(new, key, Py_None); |
| 12436 | Py_DECREF(key); |
| 12437 | if (res < 0) |
| 12438 | goto err; |
| 12439 | } |
| 12440 | } |
| 12441 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12442 | int kind; |
| 12443 | void *data; |
| 12444 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12445 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 12446 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12447 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 12448 | "to maketrans it must be a dict"); |
| 12449 | goto err; |
| 12450 | } |
| 12451 | /* copy entries into the new dict, converting string keys to int keys */ |
| 12452 | while (PyDict_Next(x, &i, &key, &value)) { |
| 12453 | if (PyUnicode_Check(key)) { |
| 12454 | /* convert string keys to integer keys */ |
| 12455 | PyObject *newkey; |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 12456 | if (PyUnicode_GET_LENGTH(key) != 1) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12457 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 12458 | "table must be of length 1"); |
| 12459 | goto err; |
| 12460 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12461 | kind = PyUnicode_KIND(key); |
| 12462 | data = PyUnicode_DATA(key); |
| 12463 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12464 | if (!newkey) |
| 12465 | goto err; |
| 12466 | res = PyDict_SetItem(new, newkey, value); |
| 12467 | Py_DECREF(newkey); |
| 12468 | if (res < 0) |
| 12469 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 12470 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12471 | /* just keep integer keys */ |
| 12472 | if (PyDict_SetItem(new, key, value) < 0) |
| 12473 | goto err; |
| 12474 | } else { |
| 12475 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 12476 | "be strings or integers"); |
| 12477 | goto err; |
| 12478 | } |
| 12479 | } |
| 12480 | } |
| 12481 | return new; |
| 12482 | err: |
| 12483 | Py_DECREF(new); |
| 12484 | return NULL; |
| 12485 | } |
| 12486 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12487 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12488 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12489 | \n\ |
| 12490 | Return a copy of the string S, where all characters have been mapped\n\ |
| 12491 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 12492 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 12493 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 12494 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12495 | |
| 12496 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12497 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12498 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12499 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12500 | } |
| 12501 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12502 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12503 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12504 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12505 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12506 | |
| 12507 | static PyObject* |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12508 | unicode_upper(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12509 | { |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 12510 | if (PyUnicode_READY(self) == -1) |
| 12511 | return NULL; |
| 12512 | if (PyUnicode_IS_ASCII(self)) |
| 12513 | return ascii_upper_or_lower(self, 0); |
Victor Stinner | b0800dc | 2012-02-25 00:47:08 +0100 | [diff] [blame] | 12514 | return case_operation(self, do_upper); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12515 | } |
| 12516 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12517 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12518 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12519 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 12520 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 12521 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12522 | |
| 12523 | static PyObject * |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12524 | unicode_zfill(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12525 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12526 | Py_ssize_t fill; |
Victor Stinner | 9310abb | 2011-10-05 00:59:23 +0200 | [diff] [blame] | 12527 | PyObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12528 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12529 | int kind; |
| 12530 | void *data; |
| 12531 | Py_UCS4 chr; |
| 12532 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12533 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12534 | return NULL; |
| 12535 | |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 12536 | if (PyUnicode_READY(self) == -1) |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12537 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12538 | |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 12539 | if (PyUnicode_GET_LENGTH(self) >= width) |
| 12540 | return unicode_result_unchanged(self); |
| 12541 | |
| 12542 | fill = width - PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12543 | |
| 12544 | u = pad(self, fill, 0, '0'); |
| 12545 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12546 | if (u == NULL) |
| 12547 | return NULL; |
| 12548 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12549 | kind = PyUnicode_KIND(u); |
| 12550 | data = PyUnicode_DATA(u); |
| 12551 | chr = PyUnicode_READ(kind, data, fill); |
| 12552 | |
| 12553 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12554 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12555 | PyUnicode_WRITE(kind, data, 0, chr); |
| 12556 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12557 | } |
| 12558 | |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 12559 | assert(_PyUnicode_CheckConsistency(u, 1)); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 12560 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12561 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12562 | |
| 12563 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12564 | static PyObject * |
| 12565 | unicode__decimal2ascii(PyObject *self) |
| 12566 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12567 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12568 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12569 | #endif |
| 12570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12571 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12572 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12573 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12574 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 12575 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12576 | With optional end, stop comparing S at that position.\n\ |
| 12577 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12578 | |
| 12579 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12580 | unicode_startswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12581 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12582 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12583 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12584 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12585 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12586 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12587 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12588 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12589 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12590 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12591 | if (PyTuple_Check(subobj)) { |
| 12592 | Py_ssize_t i; |
| 12593 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12594 | substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12595 | if (substring == NULL) |
| 12596 | return NULL; |
| 12597 | result = tailmatch(self, substring, start, end, -1); |
| 12598 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame^] | 12599 | if (result == -1) |
| 12600 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12601 | if (result) { |
| 12602 | Py_RETURN_TRUE; |
| 12603 | } |
| 12604 | } |
| 12605 | /* nothing matched */ |
| 12606 | Py_RETURN_FALSE; |
| 12607 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12608 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12609 | if (substring == NULL) { |
| 12610 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12611 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12612 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12613 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12614 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12615 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12616 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame^] | 12617 | if (result == -1) |
| 12618 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12619 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12620 | } |
| 12621 | |
| 12622 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12623 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12624 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12625 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12626 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12627 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12628 | With optional end, stop comparing S at that position.\n\ |
| 12629 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12630 | |
| 12631 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12632 | unicode_endswith(PyObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12633 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12634 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12635 | PyObject *subobj; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12636 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12637 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12638 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12639 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12640 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12641 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12642 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12643 | if (PyTuple_Check(subobj)) { |
| 12644 | Py_ssize_t i; |
| 12645 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12646 | substring = PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12647 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12648 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12649 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12650 | result = tailmatch(self, substring, start, end, +1); |
| 12651 | Py_DECREF(substring); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame^] | 12652 | if (result == -1) |
| 12653 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12654 | if (result) { |
| 12655 | Py_RETURN_TRUE; |
| 12656 | } |
| 12657 | } |
| 12658 | Py_RETURN_FALSE; |
| 12659 | } |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12660 | substring = PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12661 | if (substring == NULL) { |
| 12662 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12663 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12664 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12665 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12666 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12667 | result = tailmatch(self, substring, start, end, +1); |
Victor Stinner | 18aa447 | 2013-01-03 03:18:09 +0100 | [diff] [blame^] | 12668 | if (result == -1) |
| 12669 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12670 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12671 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12672 | } |
| 12673 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12674 | Py_LOCAL_INLINE(void) |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12675 | _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12676 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12677 | writer->size = PyUnicode_GET_LENGTH(writer->buffer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12678 | writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); |
| 12679 | writer->data = PyUnicode_DATA(writer->buffer); |
| 12680 | writer->kind = PyUnicode_KIND(writer->buffer); |
| 12681 | } |
| 12682 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12683 | void |
| 12684 | _PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12685 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12686 | memset(writer, 0, sizeof(*writer)); |
| 12687 | #ifdef Py_DEBUG |
| 12688 | writer->kind = 5; /* invalid kind */ |
| 12689 | #endif |
| 12690 | writer->min_length = Py_MAX(min_length, 100); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12691 | writer->overallocate = (min_length > 0); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12692 | } |
| 12693 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12694 | int |
| 12695 | _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, |
| 12696 | Py_ssize_t length, Py_UCS4 maxchar) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12697 | { |
| 12698 | Py_ssize_t newlen; |
| 12699 | PyObject *newbuffer; |
| 12700 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12701 | assert(length > 0); |
| 12702 | |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12703 | if (length > PY_SSIZE_T_MAX - writer->pos) { |
| 12704 | PyErr_NoMemory(); |
| 12705 | return -1; |
| 12706 | } |
| 12707 | newlen = writer->pos + length; |
| 12708 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12709 | if (writer->buffer == NULL) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12710 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12711 | /* overallocate 25% to limit the number of resize */ |
| 12712 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12713 | newlen += newlen / 4; |
| 12714 | if (newlen < writer->min_length) |
| 12715 | newlen = writer->min_length; |
| 12716 | } |
| 12717 | writer->buffer = PyUnicode_New(newlen, maxchar); |
| 12718 | if (writer->buffer == NULL) |
| 12719 | return -1; |
| 12720 | _PyUnicodeWriter_Update(writer); |
| 12721 | return 0; |
| 12722 | } |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12723 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12724 | if (newlen > writer->size) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12725 | if (writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12726 | /* overallocate 25% to limit the number of resize */ |
| 12727 | if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) |
| 12728 | newlen += newlen / 4; |
| 12729 | if (newlen < writer->min_length) |
| 12730 | newlen = writer->min_length; |
| 12731 | } |
| 12732 | |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12733 | if (maxchar > writer->maxchar || writer->readonly) { |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12734 | /* resize + widen */ |
| 12735 | newbuffer = PyUnicode_New(newlen, maxchar); |
| 12736 | if (newbuffer == NULL) |
| 12737 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12738 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12739 | writer->buffer, 0, writer->pos); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12740 | Py_DECREF(writer->buffer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12741 | writer->readonly = 0; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12742 | } |
| 12743 | else { |
| 12744 | newbuffer = resize_compact(writer->buffer, newlen); |
| 12745 | if (newbuffer == NULL) |
| 12746 | return -1; |
| 12747 | } |
| 12748 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12749 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12750 | } |
| 12751 | else if (maxchar > writer->maxchar) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12752 | assert(!writer->readonly); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12753 | newbuffer = PyUnicode_New(writer->size, maxchar); |
| 12754 | if (newbuffer == NULL) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12755 | return -1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12756 | _PyUnicode_FastCopyCharacters(newbuffer, 0, |
| 12757 | writer->buffer, 0, writer->pos); |
| 12758 | Py_DECREF(writer->buffer); |
| 12759 | writer->buffer = newbuffer; |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12760 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12761 | } |
| 12762 | return 0; |
| 12763 | } |
| 12764 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12765 | int |
| 12766 | _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) |
| 12767 | { |
| 12768 | Py_UCS4 maxchar; |
| 12769 | Py_ssize_t len; |
| 12770 | |
| 12771 | if (PyUnicode_READY(str) == -1) |
| 12772 | return -1; |
| 12773 | len = PyUnicode_GET_LENGTH(str); |
| 12774 | if (len == 0) |
| 12775 | return 0; |
| 12776 | maxchar = PyUnicode_MAX_CHAR_VALUE(str); |
| 12777 | if (maxchar > writer->maxchar || len > writer->size - writer->pos) { |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12778 | if (writer->buffer == NULL && !writer->overallocate) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12779 | Py_INCREF(str); |
| 12780 | writer->buffer = str; |
| 12781 | _PyUnicodeWriter_Update(writer); |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12782 | writer->readonly = 1; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12783 | writer->size = 0; |
| 12784 | writer->pos += len; |
| 12785 | return 0; |
| 12786 | } |
| 12787 | if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) |
| 12788 | return -1; |
| 12789 | } |
| 12790 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 12791 | str, 0, len); |
| 12792 | writer->pos += len; |
| 12793 | return 0; |
| 12794 | } |
| 12795 | |
Victor Stinner | e215d96 | 2012-10-06 23:03:36 +0200 | [diff] [blame] | 12796 | int |
| 12797 | _PyUnicodeWriter_WriteCstr(_PyUnicodeWriter *writer, const char *str, Py_ssize_t len) |
| 12798 | { |
| 12799 | Py_UCS4 maxchar; |
| 12800 | |
| 12801 | maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len); |
| 12802 | if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1) |
| 12803 | return -1; |
| 12804 | unicode_write_cstr(writer->buffer, writer->pos, str, len); |
| 12805 | writer->pos += len; |
| 12806 | return 0; |
| 12807 | } |
| 12808 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12809 | PyObject * |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12810 | _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12811 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12812 | if (writer->pos == 0) { |
| 12813 | Py_XDECREF(writer->buffer); |
| 12814 | Py_INCREF(unicode_empty); |
| 12815 | return unicode_empty; |
| 12816 | } |
Victor Stinner | d7b7c74 | 2012-06-04 22:52:12 +0200 | [diff] [blame] | 12817 | if (writer->readonly) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12818 | assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); |
| 12819 | return writer->buffer; |
| 12820 | } |
| 12821 | if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { |
| 12822 | PyObject *newbuffer; |
| 12823 | newbuffer = resize_compact(writer->buffer, writer->pos); |
| 12824 | if (newbuffer == NULL) { |
| 12825 | Py_DECREF(writer->buffer); |
| 12826 | return NULL; |
| 12827 | } |
| 12828 | writer->buffer = newbuffer; |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12829 | } |
Victor Stinner | f59c28c | 2012-05-09 03:24:14 +0200 | [diff] [blame] | 12830 | assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12831 | return writer->buffer; |
| 12832 | } |
| 12833 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12834 | void |
Victor Stinner | 3b1a74a | 2012-05-09 22:25:00 +0200 | [diff] [blame] | 12835 | _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) |
Victor Stinner | 202fdca | 2012-05-07 12:47:02 +0200 | [diff] [blame] | 12836 | { |
| 12837 | Py_CLEAR(writer->buffer); |
| 12838 | } |
| 12839 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12840 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12841 | |
| 12842 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12843 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12844 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12845 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12846 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12847 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12848 | PyDoc_STRVAR(format_map__doc__, |
| 12849 | "S.format_map(mapping) -> str\n\ |
| 12850 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12851 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12852 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12853 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12854 | static PyObject * |
| 12855 | unicode__format__(PyObject* self, PyObject* args) |
| 12856 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12857 | PyObject *format_spec; |
| 12858 | _PyUnicodeWriter writer; |
| 12859 | int ret; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12860 | |
| 12861 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12862 | return NULL; |
| 12863 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 12864 | if (PyUnicode_READY(self) == -1) |
| 12865 | return NULL; |
| 12866 | _PyUnicodeWriter_Init(&writer, 0); |
| 12867 | ret = _PyUnicode_FormatAdvancedWriter(&writer, |
| 12868 | self, format_spec, 0, |
| 12869 | PyUnicode_GET_LENGTH(format_spec)); |
| 12870 | if (ret == -1) { |
| 12871 | _PyUnicodeWriter_Dealloc(&writer); |
| 12872 | return NULL; |
| 12873 | } |
| 12874 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12875 | } |
| 12876 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12877 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12878 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12879 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12880 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12881 | |
| 12882 | static PyObject * |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 12883 | unicode__sizeof__(PyObject *v) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12884 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12885 | Py_ssize_t size; |
| 12886 | |
| 12887 | /* If it's a compact object, account for base structure + |
| 12888 | character data. */ |
| 12889 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12890 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12891 | else if (PyUnicode_IS_COMPACT(v)) |
| 12892 | size = sizeof(PyCompactUnicodeObject) + |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12893 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12894 | else { |
| 12895 | /* If it is a two-block object, account for base object, and |
| 12896 | for character block if present. */ |
| 12897 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12898 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12899 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 12900 | PyUnicode_KIND(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12901 | } |
| 12902 | /* 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] | 12903 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12904 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12905 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12906 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12907 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12908 | |
| 12909 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12910 | } |
| 12911 | |
| 12912 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12913 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12914 | |
| 12915 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12916 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12917 | { |
Victor Stinner | bf6e560 | 2011-12-12 01:53:47 +0100 | [diff] [blame] | 12918 | PyObject *copy = _PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12919 | if (!copy) |
| 12920 | return NULL; |
| 12921 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12922 | } |
| 12923 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12924 | static PyMethodDef unicode_methods[] = { |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12925 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12926 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 12927 | {"split", (PyCFunction) unicode_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
| 12928 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12929 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12930 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 12931 | {"casefold", (PyCFunction) unicode_casefold, METH_NOARGS, casefold__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12932 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12933 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12934 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12935 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12936 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12937 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12938 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12939 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12940 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12941 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12942 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12943 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12944 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12945 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12946 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12947 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12948 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12949 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12950 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12951 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12952 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12953 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12954 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12955 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12956 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12957 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12958 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12959 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12960 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12961 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12962 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12963 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12964 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12965 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12966 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12967 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12968 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12969 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12970 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12971 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12972 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12973 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12974 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12975 | #endif |
| 12976 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12977 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12978 | {NULL, NULL} |
| 12979 | }; |
| 12980 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12981 | static PyObject * |
| 12982 | unicode_mod(PyObject *v, PyObject *w) |
| 12983 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12984 | if (!PyUnicode_Check(v)) |
| 12985 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12986 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12987 | } |
| 12988 | |
| 12989 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12990 | 0, /*nb_add*/ |
| 12991 | 0, /*nb_subtract*/ |
| 12992 | 0, /*nb_multiply*/ |
| 12993 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12994 | }; |
| 12995 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12996 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12997 | (lenfunc) unicode_length, /* sq_length */ |
| 12998 | PyUnicode_Concat, /* sq_concat */ |
| 12999 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 13000 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 13001 | 0, /* sq_slice */ |
| 13002 | 0, /* sq_ass_item */ |
| 13003 | 0, /* sq_ass_slice */ |
| 13004 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13005 | }; |
| 13006 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13007 | static PyObject* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13008 | unicode_subscript(PyObject* self, PyObject* item) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13009 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13010 | if (PyUnicode_READY(self) == -1) |
| 13011 | return NULL; |
| 13012 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 13013 | if (PyIndex_Check(item)) { |
| 13014 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13015 | if (i == -1 && PyErr_Occurred()) |
| 13016 | return NULL; |
| 13017 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13018 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13019 | return unicode_getitem(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13020 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 13021 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13022 | PyObject *result; |
| 13023 | void *src_data, *dest_data; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13024 | int src_kind, dest_kind; |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13025 | Py_UCS4 ch, max_char, kind_limit; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13027 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13028 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13029 | return NULL; |
| 13030 | } |
| 13031 | |
| 13032 | if (slicelength <= 0) { |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 13033 | Py_INCREF(unicode_empty); |
| 13034 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13035 | } else if (start == 0 && step == 1 && |
Victor Stinner | c4b4954 | 2011-12-11 22:44:26 +0100 | [diff] [blame] | 13036 | slicelength == PyUnicode_GET_LENGTH(self)) { |
| 13037 | return unicode_result_unchanged(self); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 13038 | } else if (step == 1) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 13039 | return PyUnicode_Substring(self, |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 13040 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13041 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13042 | /* General case */ |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13043 | src_kind = PyUnicode_KIND(self); |
| 13044 | src_data = PyUnicode_DATA(self); |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13045 | if (!PyUnicode_IS_ASCII(self)) { |
| 13046 | kind_limit = kind_maxchar_limit(src_kind); |
| 13047 | max_char = 0; |
| 13048 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 13049 | ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13050 | if (ch > max_char) { |
| 13051 | max_char = ch; |
| 13052 | if (max_char >= kind_limit) |
| 13053 | break; |
| 13054 | } |
Victor Stinner | c80d6d2 | 2011-10-05 14:13:28 +0200 | [diff] [blame] | 13055 | } |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13056 | } |
Victor Stinner | 55c9911 | 2011-10-13 01:17:06 +0200 | [diff] [blame] | 13057 | else |
| 13058 | max_char = 127; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13059 | result = PyUnicode_New(slicelength, max_char); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13060 | if (result == NULL) |
| 13061 | return NULL; |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13062 | dest_kind = PyUnicode_KIND(result); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13063 | dest_data = PyUnicode_DATA(result); |
| 13064 | |
| 13065 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
Antoine Pitrou | 875f29b | 2011-10-04 20:00:49 +0200 | [diff] [blame] | 13066 | Py_UCS4 ch = PyUnicode_READ(src_kind, src_data, cur); |
| 13067 | PyUnicode_WRITE(dest_kind, dest_data, i, ch); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13068 | } |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 13069 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Antoine Pitrou | 7aec401 | 2011-10-04 19:08:01 +0200 | [diff] [blame] | 13070 | return result; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13071 | } else { |
| 13072 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 13073 | return NULL; |
| 13074 | } |
| 13075 | } |
| 13076 | |
| 13077 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13078 | (lenfunc)unicode_length, /* mp_length */ |
| 13079 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 13080 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 13081 | }; |
| 13082 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13083 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13084 | /* Helpers for PyUnicode_Format() */ |
| 13085 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13086 | struct unicode_formatter_t { |
| 13087 | PyObject *args; |
| 13088 | int args_owned; |
| 13089 | Py_ssize_t arglen, argidx; |
| 13090 | PyObject *dict; |
| 13091 | |
| 13092 | enum PyUnicode_Kind fmtkind; |
| 13093 | Py_ssize_t fmtcnt, fmtpos; |
| 13094 | void *fmtdata; |
| 13095 | PyObject *fmtstr; |
| 13096 | |
| 13097 | _PyUnicodeWriter writer; |
| 13098 | }; |
| 13099 | |
| 13100 | struct unicode_format_arg_t { |
| 13101 | Py_UCS4 ch; |
| 13102 | int flags; |
| 13103 | Py_ssize_t width; |
| 13104 | int prec; |
| 13105 | int sign; |
| 13106 | }; |
| 13107 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13108 | static PyObject * |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13109 | unicode_format_getnextarg(struct unicode_formatter_t *ctx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13110 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13111 | Py_ssize_t argidx = ctx->argidx; |
| 13112 | |
| 13113 | if (argidx < ctx->arglen) { |
| 13114 | ctx->argidx++; |
| 13115 | if (ctx->arglen < 0) |
| 13116 | return ctx->args; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13117 | else |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13118 | return PyTuple_GetItem(ctx->args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13119 | } |
| 13120 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13121 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13122 | return NULL; |
| 13123 | } |
| 13124 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13125 | /* 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] | 13126 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13127 | /* Format a float into the writer if the writer is not NULL, or into *p_output |
| 13128 | otherwise. |
| 13129 | |
| 13130 | Return 0 on success, raise an exception and return -1 on error. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13131 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13132 | formatfloat(PyObject *v, struct unicode_format_arg_t *arg, |
| 13133 | PyObject **p_output, |
| 13134 | _PyUnicodeWriter *writer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13135 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13136 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13137 | double x; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13138 | Py_ssize_t len; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13139 | int prec; |
| 13140 | int dtoa_flags; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13142 | x = PyFloat_AsDouble(v); |
| 13143 | if (x == -1.0 && PyErr_Occurred()) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13144 | return -1; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13145 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13146 | prec = arg->prec; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13147 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13148 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13149 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13150 | if (arg->flags & F_ALT) |
| 13151 | dtoa_flags = Py_DTSF_ALT; |
| 13152 | else |
| 13153 | dtoa_flags = 0; |
| 13154 | p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 13155 | if (p == NULL) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13156 | return -1; |
| 13157 | len = strlen(p); |
| 13158 | if (writer) { |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13159 | if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) { |
| 13160 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13161 | return -1; |
Christian Heimes | f4f9939 | 2012-09-10 11:48:41 +0200 | [diff] [blame] | 13162 | } |
Victor Stinner | 184252a | 2012-06-16 02:57:41 +0200 | [diff] [blame] | 13163 | unicode_write_cstr(writer->buffer, writer->pos, p, len); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13164 | writer->pos += len; |
| 13165 | } |
| 13166 | else |
| 13167 | *p_output = _PyUnicode_FromASCII(p, len); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 13168 | PyMem_Free(p); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13169 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13170 | } |
| 13171 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13172 | /* formatlong() emulates the format codes d, u, o, x and X, and |
| 13173 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 13174 | * Python's regular ints. |
| 13175 | * Return value: a new PyUnicodeObject*, or NULL if error. |
| 13176 | * The output string is of the form |
| 13177 | * "-"? ("0x" | "0X")? digit+ |
| 13178 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
| 13179 | * set in flags. The case of hex digits will be correct, |
| 13180 | * There will be at least prec digits, zero-filled on the left if |
| 13181 | * necessary to get that many. |
| 13182 | * val object to be converted |
| 13183 | * flags bitmask of format flags; only F_ALT is looked at |
| 13184 | * prec minimum number of digits; 0-fill on left if needed |
| 13185 | * type a character in [duoxX]; u acts the same as d |
| 13186 | * |
| 13187 | * CAUTION: o, x and X conversions on regular ints can never |
| 13188 | * produce a '-' sign, but can for Python's unbounded ints. |
| 13189 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13190 | static PyObject* |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13191 | formatlong(PyObject *val, struct unicode_format_arg_t *arg) |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13192 | { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13193 | PyObject *result = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13194 | char *buf; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13195 | Py_ssize_t i; |
| 13196 | int sign; /* 1 if '-', else 0 */ |
| 13197 | int len; /* number of characters */ |
| 13198 | Py_ssize_t llen; |
| 13199 | int numdigits; /* len == numnondigits + numdigits */ |
| 13200 | int numnondigits = 0; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13201 | int prec = arg->prec; |
| 13202 | int type = arg->ch; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13203 | |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13204 | /* Avoid exceeding SSIZE_T_MAX */ |
| 13205 | if (prec > INT_MAX-3) { |
| 13206 | PyErr_SetString(PyExc_OverflowError, |
| 13207 | "precision too large"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13208 | return NULL; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13209 | } |
| 13210 | |
| 13211 | assert(PyLong_Check(val)); |
| 13212 | |
| 13213 | switch (type) { |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13214 | default: |
| 13215 | assert(!"'type' not in [diuoxX]"); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13216 | case 'd': |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13217 | case 'i': |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13218 | case 'u': |
| 13219 | /* Special-case boolean: we want 0/1 */ |
Victor Stinner | b11d91d | 2012-04-28 00:25:34 +0200 | [diff] [blame] | 13220 | if (PyBool_Check(val)) |
| 13221 | result = PyNumber_ToBase(val, 10); |
| 13222 | else |
| 13223 | result = Py_TYPE(val)->tp_str(val); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13224 | break; |
| 13225 | case 'o': |
| 13226 | numnondigits = 2; |
| 13227 | result = PyNumber_ToBase(val, 8); |
| 13228 | break; |
| 13229 | case 'x': |
| 13230 | case 'X': |
| 13231 | numnondigits = 2; |
| 13232 | result = PyNumber_ToBase(val, 16); |
| 13233 | break; |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13234 | } |
| 13235 | if (!result) |
| 13236 | return NULL; |
| 13237 | |
| 13238 | assert(unicode_modifiable(result)); |
| 13239 | assert(PyUnicode_IS_READY(result)); |
| 13240 | assert(PyUnicode_IS_ASCII(result)); |
| 13241 | |
| 13242 | /* To modify the string in-place, there can only be one reference. */ |
| 13243 | if (Py_REFCNT(result) != 1) { |
| 13244 | PyErr_BadInternalCall(); |
| 13245 | return NULL; |
| 13246 | } |
| 13247 | buf = PyUnicode_DATA(result); |
| 13248 | llen = PyUnicode_GET_LENGTH(result); |
| 13249 | if (llen > INT_MAX) { |
| 13250 | PyErr_SetString(PyExc_ValueError, |
| 13251 | "string too large in _PyBytes_FormatLong"); |
| 13252 | return NULL; |
| 13253 | } |
| 13254 | len = (int)llen; |
| 13255 | sign = buf[0] == '-'; |
| 13256 | numnondigits += sign; |
| 13257 | numdigits = len - numnondigits; |
| 13258 | assert(numdigits > 0); |
| 13259 | |
| 13260 | /* Get rid of base marker unless F_ALT */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13261 | if (((arg->flags & F_ALT) == 0 && |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13262 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 13263 | assert(buf[sign] == '0'); |
| 13264 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
| 13265 | buf[sign+1] == 'o'); |
| 13266 | numnondigits -= 2; |
| 13267 | buf += 2; |
| 13268 | len -= 2; |
| 13269 | if (sign) |
| 13270 | buf[0] = '-'; |
| 13271 | assert(len == numnondigits + numdigits); |
| 13272 | assert(numdigits > 0); |
| 13273 | } |
| 13274 | |
| 13275 | /* Fill with leading zeroes to meet minimum width. */ |
| 13276 | if (prec > numdigits) { |
| 13277 | PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
| 13278 | numnondigits + prec); |
| 13279 | char *b1; |
| 13280 | if (!r1) { |
| 13281 | Py_DECREF(result); |
| 13282 | return NULL; |
| 13283 | } |
| 13284 | b1 = PyBytes_AS_STRING(r1); |
| 13285 | for (i = 0; i < numnondigits; ++i) |
| 13286 | *b1++ = *buf++; |
| 13287 | for (i = 0; i < prec - numdigits; i++) |
| 13288 | *b1++ = '0'; |
| 13289 | for (i = 0; i < numdigits; i++) |
| 13290 | *b1++ = *buf++; |
| 13291 | *b1 = '\0'; |
| 13292 | Py_DECREF(result); |
| 13293 | result = r1; |
| 13294 | buf = PyBytes_AS_STRING(result); |
| 13295 | len = numnondigits + prec; |
| 13296 | } |
| 13297 | |
| 13298 | /* Fix up case for hex conversions. */ |
| 13299 | if (type == 'X') { |
| 13300 | /* Need to convert all lower case letters to upper case. |
| 13301 | and need to convert 0x to 0X (and -0x to -0X). */ |
| 13302 | for (i = 0; i < len; i++) |
| 13303 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 13304 | buf[i] -= 'a'-'A'; |
| 13305 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13306 | if (!PyUnicode_Check(result) |
| 13307 | || buf != PyUnicode_DATA(result)) { |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13308 | PyObject *unicode; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 13309 | unicode = _PyUnicode_FromASCII(buf, len); |
Victor Stinner | d0880d5 | 2012-04-27 23:40:13 +0200 | [diff] [blame] | 13310 | Py_DECREF(result); |
| 13311 | result = unicode; |
| 13312 | } |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13313 | else if (len != PyUnicode_GET_LENGTH(result)) { |
| 13314 | if (PyUnicode_Resize(&result, len) < 0) |
| 13315 | Py_CLEAR(result); |
| 13316 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13317 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 13318 | } |
| 13319 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13320 | /* Format an integer. |
| 13321 | * Return 1 if the number has been formatted into the writer, |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13322 | * 0 if the number has been formatted into *p_output |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13323 | * -1 and raise an exception on error */ |
| 13324 | static int |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13325 | mainformatlong(PyObject *v, |
| 13326 | struct unicode_format_arg_t *arg, |
| 13327 | PyObject **p_output, |
| 13328 | _PyUnicodeWriter *writer) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13329 | { |
| 13330 | PyObject *iobj, *res; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13331 | char type = (char)arg->ch; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13332 | |
| 13333 | if (!PyNumber_Check(v)) |
| 13334 | goto wrongtype; |
| 13335 | |
| 13336 | if (!PyLong_Check(v)) { |
| 13337 | iobj = PyNumber_Long(v); |
| 13338 | if (iobj == NULL) { |
| 13339 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 13340 | goto wrongtype; |
| 13341 | return -1; |
| 13342 | } |
| 13343 | assert(PyLong_Check(iobj)); |
| 13344 | } |
| 13345 | else { |
| 13346 | iobj = v; |
| 13347 | Py_INCREF(iobj); |
| 13348 | } |
| 13349 | |
| 13350 | if (PyLong_CheckExact(v) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13351 | && arg->width == -1 && arg->prec == -1 |
| 13352 | && !(arg->flags & (F_SIGN | F_BLANK)) |
| 13353 | && type != 'X') |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13354 | { |
| 13355 | /* Fast path */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13356 | int alternate = arg->flags & F_ALT; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13357 | int base; |
| 13358 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13359 | switch(type) |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13360 | { |
| 13361 | default: |
| 13362 | assert(0 && "'type' not in [diuoxX]"); |
| 13363 | case 'd': |
| 13364 | case 'i': |
| 13365 | case 'u': |
| 13366 | base = 10; |
| 13367 | break; |
| 13368 | case 'o': |
| 13369 | base = 8; |
| 13370 | break; |
| 13371 | case 'x': |
| 13372 | case 'X': |
| 13373 | base = 16; |
| 13374 | break; |
| 13375 | } |
| 13376 | |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13377 | if (_PyLong_FormatWriter(writer, v, base, alternate) == -1) { |
| 13378 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13379 | return -1; |
Victor Stinner | c89d28f | 2012-10-02 12:54:07 +0200 | [diff] [blame] | 13380 | } |
| 13381 | Py_DECREF(iobj); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13382 | return 1; |
| 13383 | } |
| 13384 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13385 | res = formatlong(iobj, arg); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13386 | Py_DECREF(iobj); |
| 13387 | if (res == NULL) |
| 13388 | return -1; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13389 | *p_output = res; |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13390 | return 0; |
| 13391 | |
| 13392 | wrongtype: |
| 13393 | PyErr_Format(PyExc_TypeError, |
| 13394 | "%%%c format: a number is required, " |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13395 | "not %.200s", |
| 13396 | type, Py_TYPE(v)->tp_name); |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 13397 | return -1; |
| 13398 | } |
| 13399 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13400 | static Py_UCS4 |
| 13401 | formatchar(PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13402 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13403 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13404 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13405 | if (PyUnicode_GET_LENGTH(v) == 1) { |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13406 | return PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13407 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13408 | goto onError; |
| 13409 | } |
| 13410 | else { |
| 13411 | /* Integer input truncated to a character */ |
| 13412 | long x; |
| 13413 | x = PyLong_AsLong(v); |
| 13414 | if (x == -1 && PyErr_Occurred()) |
| 13415 | goto onError; |
| 13416 | |
Victor Stinner | 8faf821 | 2011-12-08 22:14:11 +0100 | [diff] [blame] | 13417 | if (x < 0 || x > MAX_UNICODE) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13418 | PyErr_SetString(PyExc_OverflowError, |
| 13419 | "%c arg not in range(0x110000)"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13420 | return (Py_UCS4) -1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13421 | } |
| 13422 | |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13423 | return (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13424 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 13425 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13426 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 13427 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13428 | "%c requires int or char"); |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13429 | return (Py_UCS4) -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13430 | } |
| 13431 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13432 | /* Parse options of an argument: flags, width, precision. |
| 13433 | Handle also "%(name)" syntax. |
| 13434 | |
| 13435 | Return 0 if the argument has been formatted into arg->str. |
| 13436 | Return 1 if the argument has been written into ctx->writer, |
| 13437 | Raise an exception and return -1 on error. */ |
| 13438 | static int |
| 13439 | unicode_format_arg_parse(struct unicode_formatter_t *ctx, |
| 13440 | struct unicode_format_arg_t *arg) |
| 13441 | { |
| 13442 | #define FORMAT_READ(ctx) \ |
| 13443 | PyUnicode_READ((ctx)->fmtkind, (ctx)->fmtdata, (ctx)->fmtpos) |
| 13444 | |
| 13445 | PyObject *v; |
| 13446 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13447 | if (arg->ch == '(') { |
| 13448 | /* Get argument value from a dictionary. Example: "%(name)s". */ |
| 13449 | Py_ssize_t keystart; |
| 13450 | Py_ssize_t keylen; |
| 13451 | PyObject *key; |
| 13452 | int pcount = 1; |
| 13453 | |
| 13454 | if (ctx->dict == NULL) { |
| 13455 | PyErr_SetString(PyExc_TypeError, |
| 13456 | "format requires a mapping"); |
| 13457 | return -1; |
| 13458 | } |
| 13459 | ++ctx->fmtpos; |
| 13460 | --ctx->fmtcnt; |
| 13461 | keystart = ctx->fmtpos; |
| 13462 | /* Skip over balanced parentheses */ |
| 13463 | while (pcount > 0 && --ctx->fmtcnt >= 0) { |
| 13464 | arg->ch = FORMAT_READ(ctx); |
| 13465 | if (arg->ch == ')') |
| 13466 | --pcount; |
| 13467 | else if (arg->ch == '(') |
| 13468 | ++pcount; |
| 13469 | ctx->fmtpos++; |
| 13470 | } |
| 13471 | keylen = ctx->fmtpos - keystart - 1; |
| 13472 | if (ctx->fmtcnt < 0 || pcount > 0) { |
| 13473 | PyErr_SetString(PyExc_ValueError, |
| 13474 | "incomplete format key"); |
| 13475 | return -1; |
| 13476 | } |
| 13477 | key = PyUnicode_Substring(ctx->fmtstr, |
| 13478 | keystart, keystart + keylen); |
| 13479 | if (key == NULL) |
| 13480 | return -1; |
| 13481 | if (ctx->args_owned) { |
| 13482 | Py_DECREF(ctx->args); |
| 13483 | ctx->args_owned = 0; |
| 13484 | } |
| 13485 | ctx->args = PyObject_GetItem(ctx->dict, key); |
| 13486 | Py_DECREF(key); |
| 13487 | if (ctx->args == NULL) |
| 13488 | return -1; |
| 13489 | ctx->args_owned = 1; |
| 13490 | ctx->arglen = -1; |
| 13491 | ctx->argidx = -2; |
| 13492 | } |
| 13493 | |
| 13494 | /* Parse flags. Example: "%+i" => flags=F_SIGN. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13495 | while (--ctx->fmtcnt >= 0) { |
| 13496 | arg->ch = FORMAT_READ(ctx); |
| 13497 | ctx->fmtpos++; |
| 13498 | switch (arg->ch) { |
| 13499 | case '-': arg->flags |= F_LJUST; continue; |
| 13500 | case '+': arg->flags |= F_SIGN; continue; |
| 13501 | case ' ': arg->flags |= F_BLANK; continue; |
| 13502 | case '#': arg->flags |= F_ALT; continue; |
| 13503 | case '0': arg->flags |= F_ZERO; continue; |
| 13504 | } |
| 13505 | break; |
| 13506 | } |
| 13507 | |
| 13508 | /* Parse width. Example: "%10s" => width=10 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13509 | if (arg->ch == '*') { |
| 13510 | v = unicode_format_getnextarg(ctx); |
| 13511 | if (v == NULL) |
| 13512 | return -1; |
| 13513 | if (!PyLong_Check(v)) { |
| 13514 | PyErr_SetString(PyExc_TypeError, |
| 13515 | "* wants int"); |
| 13516 | return -1; |
| 13517 | } |
| 13518 | arg->width = PyLong_AsLong(v); |
| 13519 | if (arg->width == -1 && PyErr_Occurred()) |
| 13520 | return -1; |
| 13521 | if (arg->width < 0) { |
| 13522 | arg->flags |= F_LJUST; |
| 13523 | arg->width = -arg->width; |
| 13524 | } |
| 13525 | if (--ctx->fmtcnt >= 0) { |
| 13526 | arg->ch = FORMAT_READ(ctx); |
| 13527 | ctx->fmtpos++; |
| 13528 | } |
| 13529 | } |
| 13530 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13531 | arg->width = arg->ch - '0'; |
| 13532 | while (--ctx->fmtcnt >= 0) { |
| 13533 | arg->ch = FORMAT_READ(ctx); |
| 13534 | ctx->fmtpos++; |
| 13535 | if (arg->ch < '0' || arg->ch > '9') |
| 13536 | break; |
| 13537 | /* Since arg->ch is unsigned, the RHS would end up as unsigned, |
| 13538 | mixing signed and unsigned comparison. Since arg->ch is between |
| 13539 | '0' and '9', casting to int is safe. */ |
| 13540 | if (arg->width > (PY_SSIZE_T_MAX - ((int)arg->ch - '0')) / 10) { |
| 13541 | PyErr_SetString(PyExc_ValueError, |
| 13542 | "width too big"); |
| 13543 | return -1; |
| 13544 | } |
| 13545 | arg->width = arg->width*10 + (arg->ch - '0'); |
| 13546 | } |
| 13547 | } |
| 13548 | |
| 13549 | /* Parse precision. Example: "%.3f" => prec=3 */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13550 | if (arg->ch == '.') { |
| 13551 | arg->prec = 0; |
| 13552 | if (--ctx->fmtcnt >= 0) { |
| 13553 | arg->ch = FORMAT_READ(ctx); |
| 13554 | ctx->fmtpos++; |
| 13555 | } |
| 13556 | if (arg->ch == '*') { |
| 13557 | v = unicode_format_getnextarg(ctx); |
| 13558 | if (v == NULL) |
| 13559 | return -1; |
| 13560 | if (!PyLong_Check(v)) { |
| 13561 | PyErr_SetString(PyExc_TypeError, |
| 13562 | "* wants int"); |
| 13563 | return -1; |
| 13564 | } |
| 13565 | arg->prec = PyLong_AsLong(v); |
| 13566 | if (arg->prec == -1 && PyErr_Occurred()) |
| 13567 | return -1; |
| 13568 | if (arg->prec < 0) |
| 13569 | arg->prec = 0; |
| 13570 | if (--ctx->fmtcnt >= 0) { |
| 13571 | arg->ch = FORMAT_READ(ctx); |
| 13572 | ctx->fmtpos++; |
| 13573 | } |
| 13574 | } |
| 13575 | else if (arg->ch >= '0' && arg->ch <= '9') { |
| 13576 | arg->prec = arg->ch - '0'; |
| 13577 | while (--ctx->fmtcnt >= 0) { |
| 13578 | arg->ch = FORMAT_READ(ctx); |
| 13579 | ctx->fmtpos++; |
| 13580 | if (arg->ch < '0' || arg->ch > '9') |
| 13581 | break; |
| 13582 | if (arg->prec > (INT_MAX - ((int)arg->ch - '0')) / 10) { |
| 13583 | PyErr_SetString(PyExc_ValueError, |
Victor Stinner | 3921e90 | 2012-10-06 23:05:00 +0200 | [diff] [blame] | 13584 | "precision too big"); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13585 | return -1; |
| 13586 | } |
| 13587 | arg->prec = arg->prec*10 + (arg->ch - '0'); |
| 13588 | } |
| 13589 | } |
| 13590 | } |
| 13591 | |
| 13592 | /* Ignore "h", "l" and "L" format prefix (ex: "%hi" or "%ls") */ |
| 13593 | if (ctx->fmtcnt >= 0) { |
| 13594 | if (arg->ch == 'h' || arg->ch == 'l' || arg->ch == 'L') { |
| 13595 | if (--ctx->fmtcnt >= 0) { |
| 13596 | arg->ch = FORMAT_READ(ctx); |
| 13597 | ctx->fmtpos++; |
| 13598 | } |
| 13599 | } |
| 13600 | } |
| 13601 | if (ctx->fmtcnt < 0) { |
| 13602 | PyErr_SetString(PyExc_ValueError, |
| 13603 | "incomplete format"); |
| 13604 | return -1; |
| 13605 | } |
| 13606 | return 0; |
| 13607 | |
| 13608 | #undef FORMAT_READ |
| 13609 | } |
| 13610 | |
| 13611 | /* Format one argument. Supported conversion specifiers: |
| 13612 | |
| 13613 | - "s", "r", "a": any type |
| 13614 | - "i", "d", "u", "o", "x", "X": int |
| 13615 | - "e", "E", "f", "F", "g", "G": float |
| 13616 | - "c": int or str (1 character) |
| 13617 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13618 | When possible, the output is written directly into the Unicode writer |
| 13619 | (ctx->writer). A string is created when padding is required. |
| 13620 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13621 | Return 0 if the argument has been formatted into *p_str, |
| 13622 | 1 if the argument has been written into ctx->writer, |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13623 | -1 on error. */ |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13624 | static int |
| 13625 | unicode_format_arg_format(struct unicode_formatter_t *ctx, |
| 13626 | struct unicode_format_arg_t *arg, |
| 13627 | PyObject **p_str) |
| 13628 | { |
| 13629 | PyObject *v; |
| 13630 | _PyUnicodeWriter *writer = &ctx->writer; |
| 13631 | |
| 13632 | if (ctx->fmtcnt == 0) |
| 13633 | ctx->writer.overallocate = 0; |
| 13634 | |
| 13635 | if (arg->ch == '%') { |
| 13636 | if (_PyUnicodeWriter_Prepare(writer, 1, '%') == -1) |
| 13637 | return -1; |
| 13638 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '%'); |
| 13639 | writer->pos += 1; |
| 13640 | return 1; |
| 13641 | } |
| 13642 | |
| 13643 | v = unicode_format_getnextarg(ctx); |
| 13644 | if (v == NULL) |
| 13645 | return -1; |
| 13646 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13647 | |
| 13648 | switch (arg->ch) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13649 | case 's': |
| 13650 | case 'r': |
| 13651 | case 'a': |
| 13652 | if (PyLong_CheckExact(v) && arg->width == -1 && arg->prec == -1) { |
| 13653 | /* Fast path */ |
| 13654 | if (_PyLong_FormatWriter(writer, v, 10, arg->flags & F_ALT) == -1) |
| 13655 | return -1; |
| 13656 | return 1; |
| 13657 | } |
| 13658 | |
| 13659 | if (PyUnicode_CheckExact(v) && arg->ch == 's') { |
| 13660 | *p_str = v; |
| 13661 | Py_INCREF(*p_str); |
| 13662 | } |
| 13663 | else { |
| 13664 | if (arg->ch == 's') |
| 13665 | *p_str = PyObject_Str(v); |
| 13666 | else if (arg->ch == 'r') |
| 13667 | *p_str = PyObject_Repr(v); |
| 13668 | else |
| 13669 | *p_str = PyObject_ASCII(v); |
| 13670 | } |
| 13671 | break; |
| 13672 | |
| 13673 | case 'i': |
| 13674 | case 'd': |
| 13675 | case 'u': |
| 13676 | case 'o': |
| 13677 | case 'x': |
| 13678 | case 'X': |
| 13679 | { |
| 13680 | int ret = mainformatlong(v, arg, p_str, writer); |
| 13681 | if (ret != 0) |
| 13682 | return ret; |
| 13683 | arg->sign = 1; |
| 13684 | break; |
| 13685 | } |
| 13686 | |
| 13687 | case 'e': |
| 13688 | case 'E': |
| 13689 | case 'f': |
| 13690 | case 'F': |
| 13691 | case 'g': |
| 13692 | case 'G': |
| 13693 | if (arg->width == -1 && arg->prec == -1 |
| 13694 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 13695 | { |
| 13696 | /* Fast path */ |
| 13697 | if (formatfloat(v, arg, NULL, writer) == -1) |
| 13698 | return -1; |
| 13699 | return 1; |
| 13700 | } |
| 13701 | |
| 13702 | arg->sign = 1; |
| 13703 | if (formatfloat(v, arg, p_str, NULL) == -1) |
| 13704 | return -1; |
| 13705 | break; |
| 13706 | |
| 13707 | case 'c': |
| 13708 | { |
| 13709 | Py_UCS4 ch = formatchar(v); |
| 13710 | if (ch == (Py_UCS4) -1) |
| 13711 | return -1; |
| 13712 | if (arg->width == -1 && arg->prec == -1) { |
| 13713 | /* Fast path */ |
| 13714 | if (_PyUnicodeWriter_Prepare(writer, 1, ch) == -1) |
| 13715 | return -1; |
| 13716 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch); |
| 13717 | writer->pos += 1; |
| 13718 | return 1; |
| 13719 | } |
| 13720 | *p_str = PyUnicode_FromOrdinal(ch); |
| 13721 | break; |
| 13722 | } |
| 13723 | |
| 13724 | default: |
| 13725 | PyErr_Format(PyExc_ValueError, |
| 13726 | "unsupported format character '%c' (0x%x) " |
| 13727 | "at index %zd", |
| 13728 | (31<=arg->ch && arg->ch<=126) ? (char)arg->ch : '?', |
| 13729 | (int)arg->ch, |
| 13730 | ctx->fmtpos - 1); |
| 13731 | return -1; |
| 13732 | } |
| 13733 | if (*p_str == NULL) |
| 13734 | return -1; |
| 13735 | assert (PyUnicode_Check(*p_str)); |
| 13736 | return 0; |
| 13737 | } |
| 13738 | |
| 13739 | static int |
| 13740 | unicode_format_arg_output(struct unicode_formatter_t *ctx, |
| 13741 | struct unicode_format_arg_t *arg, |
| 13742 | PyObject *str) |
| 13743 | { |
| 13744 | Py_ssize_t len; |
| 13745 | enum PyUnicode_Kind kind; |
| 13746 | void *pbuf; |
| 13747 | Py_ssize_t pindex; |
| 13748 | Py_UCS4 signchar; |
| 13749 | Py_ssize_t buflen; |
| 13750 | Py_UCS4 maxchar, bufmaxchar; |
| 13751 | Py_ssize_t sublen; |
| 13752 | _PyUnicodeWriter *writer = &ctx->writer; |
| 13753 | Py_UCS4 fill; |
| 13754 | |
| 13755 | fill = ' '; |
| 13756 | if (arg->sign && arg->flags & F_ZERO) |
| 13757 | fill = '0'; |
| 13758 | |
| 13759 | if (PyUnicode_READY(str) == -1) |
| 13760 | return -1; |
| 13761 | |
| 13762 | len = PyUnicode_GET_LENGTH(str); |
| 13763 | if ((arg->width == -1 || arg->width <= len) |
| 13764 | && (arg->prec == -1 || arg->prec >= len) |
| 13765 | && !(arg->flags & (F_SIGN | F_BLANK))) |
| 13766 | { |
| 13767 | /* Fast path */ |
| 13768 | if (_PyUnicodeWriter_WriteStr(writer, str) == -1) |
| 13769 | return -1; |
| 13770 | return 0; |
| 13771 | } |
| 13772 | |
| 13773 | /* Truncate the string for "s", "r" and "a" formats |
| 13774 | if the precision is set */ |
| 13775 | if (arg->ch == 's' || arg->ch == 'r' || arg->ch == 'a') { |
| 13776 | if (arg->prec >= 0 && len > arg->prec) |
| 13777 | len = arg->prec; |
| 13778 | } |
| 13779 | |
| 13780 | /* Adjust sign and width */ |
| 13781 | kind = PyUnicode_KIND(str); |
| 13782 | pbuf = PyUnicode_DATA(str); |
| 13783 | pindex = 0; |
| 13784 | signchar = '\0'; |
| 13785 | if (arg->sign) { |
| 13786 | Py_UCS4 ch = PyUnicode_READ(kind, pbuf, pindex); |
| 13787 | if (ch == '-' || ch == '+') { |
| 13788 | signchar = ch; |
| 13789 | len--; |
| 13790 | pindex++; |
| 13791 | } |
| 13792 | else if (arg->flags & F_SIGN) |
| 13793 | signchar = '+'; |
| 13794 | else if (arg->flags & F_BLANK) |
| 13795 | signchar = ' '; |
| 13796 | else |
| 13797 | arg->sign = 0; |
| 13798 | } |
| 13799 | if (arg->width < len) |
| 13800 | arg->width = len; |
| 13801 | |
| 13802 | /* Prepare the writer */ |
| 13803 | bufmaxchar = 127; |
| 13804 | if (!(arg->flags & F_LJUST)) { |
| 13805 | if (arg->sign) { |
| 13806 | if ((arg->width-1) > len) |
| 13807 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); |
| 13808 | } |
| 13809 | else { |
| 13810 | if (arg->width > len) |
| 13811 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); |
| 13812 | } |
| 13813 | } |
| 13814 | maxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); |
| 13815 | bufmaxchar = MAX_MAXCHAR(bufmaxchar, maxchar); |
| 13816 | buflen = arg->width; |
| 13817 | if (arg->sign && len == arg->width) |
| 13818 | buflen++; |
| 13819 | if (_PyUnicodeWriter_Prepare(writer, buflen, bufmaxchar) == -1) |
| 13820 | return -1; |
| 13821 | |
| 13822 | /* Write the sign if needed */ |
| 13823 | if (arg->sign) { |
| 13824 | if (fill != ' ') { |
| 13825 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 13826 | writer->pos += 1; |
| 13827 | } |
| 13828 | if (arg->width > len) |
| 13829 | arg->width--; |
| 13830 | } |
| 13831 | |
| 13832 | /* Write the numeric prefix for "x", "X" and "o" formats |
| 13833 | if the alternate form is used. |
| 13834 | For example, write "0x" for the "%#x" format. */ |
| 13835 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 13836 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13837 | assert(PyUnicode_READ(kind, pbuf, pindex + 1) == arg->ch); |
| 13838 | if (fill != ' ') { |
| 13839 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 13840 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 13841 | writer->pos += 2; |
| 13842 | pindex += 2; |
| 13843 | } |
| 13844 | arg->width -= 2; |
| 13845 | if (arg->width < 0) |
| 13846 | arg->width = 0; |
| 13847 | len -= 2; |
| 13848 | } |
| 13849 | |
| 13850 | /* Pad left with the fill character if needed */ |
| 13851 | if (arg->width > len && !(arg->flags & F_LJUST)) { |
| 13852 | sublen = arg->width - len; |
| 13853 | FILL(writer->kind, writer->data, fill, writer->pos, sublen); |
| 13854 | writer->pos += sublen; |
| 13855 | arg->width = len; |
| 13856 | } |
| 13857 | |
| 13858 | /* If padding with spaces: write sign if needed and/or numeric prefix if |
| 13859 | the alternate form is used */ |
| 13860 | if (fill == ' ') { |
| 13861 | if (arg->sign) { |
| 13862 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, signchar); |
| 13863 | writer->pos += 1; |
| 13864 | } |
| 13865 | if ((arg->flags & F_ALT) && (arg->ch == 'x' || arg->ch == 'X' || arg->ch == 'o')) { |
| 13866 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 13867 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == arg->ch); |
| 13868 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '0'); |
| 13869 | PyUnicode_WRITE(writer->kind, writer->data, writer->pos+1, arg->ch); |
| 13870 | writer->pos += 2; |
| 13871 | pindex += 2; |
| 13872 | } |
| 13873 | } |
| 13874 | |
| 13875 | /* Write characters */ |
| 13876 | if (len) { |
| 13877 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 13878 | str, pindex, len); |
| 13879 | writer->pos += len; |
| 13880 | } |
| 13881 | |
| 13882 | /* Pad right with the fill character if needed */ |
| 13883 | if (arg->width > len) { |
| 13884 | sublen = arg->width - len; |
| 13885 | FILL(writer->kind, writer->data, ' ', writer->pos, sublen); |
| 13886 | writer->pos += sublen; |
| 13887 | } |
| 13888 | return 0; |
| 13889 | } |
| 13890 | |
| 13891 | /* Helper of PyUnicode_Format(): format one arg. |
| 13892 | Return 0 on success, raise an exception and return -1 on error. */ |
| 13893 | static int |
| 13894 | unicode_format_arg(struct unicode_formatter_t *ctx) |
| 13895 | { |
| 13896 | struct unicode_format_arg_t arg; |
| 13897 | PyObject *str; |
| 13898 | int ret; |
| 13899 | |
Victor Stinner | 8dbd421 | 2012-12-04 09:30:24 +0100 | [diff] [blame] | 13900 | arg.ch = PyUnicode_READ(ctx->fmtkind, ctx->fmtdata, ctx->fmtpos); |
| 13901 | arg.flags = 0; |
| 13902 | arg.width = -1; |
| 13903 | arg.prec = -1; |
| 13904 | arg.sign = 0; |
| 13905 | str = NULL; |
| 13906 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13907 | ret = unicode_format_arg_parse(ctx, &arg); |
| 13908 | if (ret == -1) |
| 13909 | return -1; |
| 13910 | |
| 13911 | ret = unicode_format_arg_format(ctx, &arg, &str); |
| 13912 | if (ret == -1) |
| 13913 | return -1; |
| 13914 | |
| 13915 | if (ret != 1) { |
| 13916 | ret = unicode_format_arg_output(ctx, &arg, str); |
| 13917 | Py_DECREF(str); |
| 13918 | if (ret == -1) |
| 13919 | return -1; |
| 13920 | } |
| 13921 | |
| 13922 | if (ctx->dict && (ctx->argidx < ctx->arglen) && arg.ch != '%') { |
| 13923 | PyErr_SetString(PyExc_TypeError, |
| 13924 | "not all arguments converted during string formatting"); |
| 13925 | return -1; |
| 13926 | } |
| 13927 | return 0; |
| 13928 | } |
| 13929 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13930 | PyObject * |
| 13931 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13932 | { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13933 | struct unicode_formatter_t ctx; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 13934 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13935 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13936 | PyErr_BadInternalCall(); |
| 13937 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13938 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13939 | |
| 13940 | ctx.fmtstr = PyUnicode_FromObject(format); |
| 13941 | if (ctx.fmtstr == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13942 | return NULL; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13943 | if (PyUnicode_READY(ctx.fmtstr) == -1) { |
| 13944 | Py_DECREF(ctx.fmtstr); |
| 13945 | return NULL; |
| 13946 | } |
| 13947 | ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr); |
| 13948 | ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr); |
| 13949 | ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); |
| 13950 | ctx.fmtpos = 0; |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13951 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13952 | _PyUnicodeWriter_Init(&ctx.writer, ctx.fmtcnt + 100); |
Victor Stinner | f2c76aa | 2012-05-03 13:10:40 +0200 | [diff] [blame] | 13953 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13954 | if (PyTuple_Check(args)) { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13955 | ctx.arglen = PyTuple_Size(args); |
| 13956 | ctx.argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13957 | } |
| 13958 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13959 | ctx.arglen = -1; |
| 13960 | ctx.argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13961 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13962 | ctx.args_owned = 0; |
Benjamin Peterson | 28a6cfa | 2012-08-28 17:55:35 -0400 | [diff] [blame] | 13963 | if (PyMapping_Check(args) && !PyTuple_Check(args) && !PyUnicode_Check(args)) |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13964 | ctx.dict = args; |
| 13965 | else |
| 13966 | ctx.dict = NULL; |
| 13967 | ctx.args = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13968 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13969 | while (--ctx.fmtcnt >= 0) { |
| 13970 | if (PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 13971 | Py_ssize_t nonfmtpos, sublen; |
| 13972 | Py_UCS4 maxchar; |
| 13973 | |
| 13974 | nonfmtpos = ctx.fmtpos++; |
| 13975 | while (ctx.fmtcnt >= 0 && |
| 13976 | PyUnicode_READ(ctx.fmtkind, ctx.fmtdata, ctx.fmtpos) != '%') { |
| 13977 | ctx.fmtpos++; |
| 13978 | ctx.fmtcnt--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13979 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13980 | if (ctx.fmtcnt < 0) { |
| 13981 | ctx.fmtpos--; |
| 13982 | ctx.writer.overallocate = 0; |
Victor Stinner | a049443 | 2012-10-03 23:03:46 +0200 | [diff] [blame] | 13983 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13984 | sublen = ctx.fmtpos - nonfmtpos; |
| 13985 | maxchar = _PyUnicode_FindMaxChar(ctx.fmtstr, |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13986 | nonfmtpos, nonfmtpos + sublen); |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13987 | if (_PyUnicodeWriter_Prepare(&ctx.writer, sublen, maxchar) == -1) |
Antoine Pitrou | 5c0ba36 | 2011-10-07 01:54:09 +0200 | [diff] [blame] | 13988 | goto onError; |
Victor Stinner | ee4544c | 2012-05-09 22:24:08 +0200 | [diff] [blame] | 13989 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13990 | _PyUnicode_FastCopyCharacters(ctx.writer.buffer, ctx.writer.pos, |
| 13991 | ctx.fmtstr, nonfmtpos, sublen); |
| 13992 | ctx.writer.pos += sublen; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13993 | } |
| 13994 | else { |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13995 | ctx.fmtpos++; |
| 13996 | if (unicode_format_arg(&ctx) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13997 | goto onError; |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 13998 | } |
| 13999 | } |
Victor Stinner | aff3cc6 | 2012-04-30 05:19:21 +0200 | [diff] [blame] | 14000 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14001 | if (ctx.argidx < ctx.arglen && !ctx.dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14002 | PyErr_SetString(PyExc_TypeError, |
| 14003 | "not all arguments converted during string formatting"); |
| 14004 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14005 | } |
| 14006 | |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14007 | if (ctx.args_owned) { |
| 14008 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14009 | } |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14010 | Py_DECREF(ctx.fmtstr); |
| 14011 | return _PyUnicodeWriter_Finish(&ctx.writer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14012 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14013 | onError: |
Victor Stinner | a4708231 | 2012-10-04 02:19:54 +0200 | [diff] [blame] | 14014 | Py_DECREF(ctx.fmtstr); |
| 14015 | _PyUnicodeWriter_Dealloc(&ctx.writer); |
| 14016 | if (ctx.args_owned) { |
| 14017 | Py_DECREF(ctx.args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14018 | } |
| 14019 | return NULL; |
| 14020 | } |
| 14021 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 14022 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14023 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 14024 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14025 | static PyObject * |
| 14026 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14027 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14028 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14029 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 14030 | char *encoding = NULL; |
| 14031 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14032 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14033 | if (type != &PyUnicode_Type) |
| 14034 | return unicode_subtype_new(type, args, kwds); |
| 14035 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14036 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14037 | return NULL; |
Victor Stinner | 382955f | 2011-12-11 21:44:00 +0100 | [diff] [blame] | 14038 | if (x == NULL) { |
| 14039 | Py_INCREF(unicode_empty); |
| 14040 | return unicode_empty; |
| 14041 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14042 | if (encoding == NULL && errors == NULL) |
| 14043 | return PyObject_Str(x); |
| 14044 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14045 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14046 | } |
| 14047 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14048 | static PyObject * |
| 14049 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14050 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14051 | PyObject *unicode, *self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14052 | Py_ssize_t length, char_size; |
| 14053 | int share_wstr, share_utf8; |
| 14054 | unsigned int kind; |
| 14055 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14056 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14057 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14058 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14059 | unicode = unicode_new(&PyUnicode_Type, args, kwds); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14060 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14061 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14062 | assert(_PyUnicode_CHECK(unicode)); |
Benjamin Peterson | bac7949 | 2012-01-14 13:34:47 -0500 | [diff] [blame] | 14063 | if (PyUnicode_READY(unicode) == -1) { |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14064 | Py_DECREF(unicode); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14065 | return NULL; |
Benjamin Peterson | 22a2970 | 2012-01-02 09:00:30 -0600 | [diff] [blame] | 14066 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14067 | |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14068 | self = type->tp_alloc(type, 0); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14069 | if (self == NULL) { |
| 14070 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14071 | return NULL; |
| 14072 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14073 | kind = PyUnicode_KIND(unicode); |
| 14074 | length = PyUnicode_GET_LENGTH(unicode); |
| 14075 | |
| 14076 | _PyUnicode_LENGTH(self) = length; |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14077 | #ifdef Py_DEBUG |
| 14078 | _PyUnicode_HASH(self) = -1; |
| 14079 | #else |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14080 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14081 | #endif |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14082 | _PyUnicode_STATE(self).interned = 0; |
| 14083 | _PyUnicode_STATE(self).kind = kind; |
| 14084 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 14085 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14086 | _PyUnicode_STATE(self).ready = 1; |
| 14087 | _PyUnicode_WSTR(self) = NULL; |
| 14088 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 14089 | _PyUnicode_UTF8(self) = NULL; |
| 14090 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14091 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14092 | |
| 14093 | share_utf8 = 0; |
| 14094 | share_wstr = 0; |
| 14095 | if (kind == PyUnicode_1BYTE_KIND) { |
| 14096 | char_size = 1; |
| 14097 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 14098 | share_utf8 = 1; |
| 14099 | } |
| 14100 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 14101 | char_size = 2; |
| 14102 | if (sizeof(wchar_t) == 2) |
| 14103 | share_wstr = 1; |
| 14104 | } |
| 14105 | else { |
| 14106 | assert(kind == PyUnicode_4BYTE_KIND); |
| 14107 | char_size = 4; |
| 14108 | if (sizeof(wchar_t) == 4) |
| 14109 | share_wstr = 1; |
| 14110 | } |
| 14111 | |
| 14112 | /* Ensure we won't overflow the length. */ |
| 14113 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 14114 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14115 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14116 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14117 | data = PyObject_MALLOC((length + 1) * char_size); |
| 14118 | if (data == NULL) { |
| 14119 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14120 | goto onError; |
| 14121 | } |
| 14122 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 14123 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14124 | if (share_utf8) { |
| 14125 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 14126 | _PyUnicode_UTF8(self) = data; |
| 14127 | } |
| 14128 | if (share_wstr) { |
| 14129 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 14130 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 14131 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14132 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14133 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 14134 | kind * (length + 1)); |
Victor Stinner | bb10a1f | 2011-10-05 01:34:17 +0200 | [diff] [blame] | 14135 | assert(_PyUnicode_CheckConsistency(self, 1)); |
Victor Stinner | fb9ea8c | 2011-10-06 01:45:57 +0200 | [diff] [blame] | 14136 | #ifdef Py_DEBUG |
| 14137 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 14138 | #endif |
Victor Stinner | dd18d3a | 2011-10-22 11:08:10 +0200 | [diff] [blame] | 14139 | Py_DECREF(unicode); |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14140 | return self; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 14141 | |
| 14142 | onError: |
| 14143 | Py_DECREF(unicode); |
| 14144 | Py_DECREF(self); |
| 14145 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 14146 | } |
| 14147 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 14148 | PyDoc_STRVAR(unicode_doc, |
Chris Jerdonek | 83fe2e1 | 2012-10-07 14:48:36 -0700 | [diff] [blame] | 14149 | "str(object='') -> str\n\ |
| 14150 | str(bytes_or_buffer[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14151 | \n\ |
Nick Coghlan | 573b1fd | 2012-08-16 14:13:07 +1000 | [diff] [blame] | 14152 | Create a new string object from the given object. If encoding or\n\ |
| 14153 | errors is specified, then the object must expose a data buffer\n\ |
| 14154 | that will be decoded using the given encoding and error handler.\n\ |
| 14155 | Otherwise, returns the result of object.__str__() (if defined)\n\ |
| 14156 | or repr(object).\n\ |
| 14157 | encoding defaults to sys.getdefaultencoding().\n\ |
| 14158 | errors defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14159 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14160 | static PyObject *unicode_iter(PyObject *seq); |
| 14161 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14162 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 14163 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14164 | "str", /* tp_name */ |
| 14165 | sizeof(PyUnicodeObject), /* tp_size */ |
| 14166 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14167 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14168 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 14169 | 0, /* tp_print */ |
| 14170 | 0, /* tp_getattr */ |
| 14171 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14172 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14173 | unicode_repr, /* tp_repr */ |
| 14174 | &unicode_as_number, /* tp_as_number */ |
| 14175 | &unicode_as_sequence, /* tp_as_sequence */ |
| 14176 | &unicode_as_mapping, /* tp_as_mapping */ |
| 14177 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 14178 | 0, /* tp_call*/ |
| 14179 | (reprfunc) unicode_str, /* tp_str */ |
| 14180 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14181 | 0, /* tp_setattro */ |
| 14182 | 0, /* tp_as_buffer */ |
| 14183 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14184 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14185 | unicode_doc, /* tp_doc */ |
| 14186 | 0, /* tp_traverse */ |
| 14187 | 0, /* tp_clear */ |
| 14188 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 14189 | 0, /* tp_weaklistoffset */ |
| 14190 | unicode_iter, /* tp_iter */ |
| 14191 | 0, /* tp_iternext */ |
| 14192 | unicode_methods, /* tp_methods */ |
| 14193 | 0, /* tp_members */ |
| 14194 | 0, /* tp_getset */ |
| 14195 | &PyBaseObject_Type, /* tp_base */ |
| 14196 | 0, /* tp_dict */ |
| 14197 | 0, /* tp_descr_get */ |
| 14198 | 0, /* tp_descr_set */ |
| 14199 | 0, /* tp_dictoffset */ |
| 14200 | 0, /* tp_init */ |
| 14201 | 0, /* tp_alloc */ |
| 14202 | unicode_new, /* tp_new */ |
| 14203 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14204 | }; |
| 14205 | |
| 14206 | /* Initialize the Unicode implementation */ |
| 14207 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14208 | int _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14209 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14210 | int i; |
| 14211 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14212 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14213 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14214 | 0x000A, /* LINE FEED */ |
| 14215 | 0x000D, /* CARRIAGE RETURN */ |
| 14216 | 0x001C, /* FILE SEPARATOR */ |
| 14217 | 0x001D, /* GROUP SEPARATOR */ |
| 14218 | 0x001E, /* RECORD SEPARATOR */ |
| 14219 | 0x0085, /* NEXT LINE */ |
| 14220 | 0x2028, /* LINE SEPARATOR */ |
| 14221 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 14222 | }; |
| 14223 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 14224 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 14225 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14226 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14227 | Py_FatalError("Can't create empty string"); |
Victor Stinner | d3df8ab | 2011-11-22 01:22:34 +0100 | [diff] [blame] | 14228 | assert(_PyUnicode_CheckConsistency(unicode_empty, 1)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14229 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14230 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14231 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 14232 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14233 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14234 | |
| 14235 | /* initialize the linebreak bloom filter */ |
| 14236 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14237 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 14238 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 14239 | |
| 14240 | PyType_Ready(&EncodingMapType); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14241 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 14242 | if (PyType_Ready(&PyFieldNameIter_Type) < 0) |
| 14243 | Py_FatalError("Can't initialize field name iterator type"); |
| 14244 | |
| 14245 | if (PyType_Ready(&PyFormatterIter_Type) < 0) |
| 14246 | Py_FatalError("Can't initialize formatter iter type"); |
Benjamin Peterson | e8ea97f | 2012-10-30 23:27:52 -0400 | [diff] [blame] | 14247 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 14248 | #ifdef HAVE_MBCS |
| 14249 | winver.dwOSVersionInfoSize = sizeof(winver); |
| 14250 | if (!GetVersionEx((OSVERSIONINFO*)&winver)) { |
| 14251 | PyErr_SetFromWindowsErr(0); |
| 14252 | return -1; |
| 14253 | } |
| 14254 | #endif |
| 14255 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14256 | } |
| 14257 | |
| 14258 | /* Finalize the Unicode implementation */ |
| 14259 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14260 | int |
| 14261 | PyUnicode_ClearFreeList(void) |
| 14262 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14263 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14264 | } |
| 14265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14266 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 14267 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14268 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14269 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14270 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 14271 | Py_XDECREF(unicode_empty); |
| 14272 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 14273 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14274 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14275 | if (unicode_latin1[i]) { |
| 14276 | Py_DECREF(unicode_latin1[i]); |
| 14277 | unicode_latin1[i] = NULL; |
| 14278 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 14279 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 14280 | _PyUnicode_ClearStaticStrings(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 14281 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14282 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 14283 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14284 | void |
| 14285 | PyUnicode_InternInPlace(PyObject **p) |
| 14286 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14287 | register PyObject *s = *p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14288 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14289 | #ifdef Py_DEBUG |
| 14290 | assert(s != NULL); |
| 14291 | assert(_PyUnicode_CHECK(s)); |
| 14292 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14293 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 14294 | return; |
| 14295 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14296 | /* If it's a subclass, we don't really know what putting |
| 14297 | it in the interned dict might do. */ |
| 14298 | if (!PyUnicode_CheckExact(s)) |
| 14299 | return; |
| 14300 | if (PyUnicode_CHECK_INTERNED(s)) |
| 14301 | return; |
| 14302 | if (interned == NULL) { |
| 14303 | interned = PyDict_New(); |
| 14304 | if (interned == NULL) { |
| 14305 | PyErr_Clear(); /* Don't leave an exception */ |
| 14306 | return; |
| 14307 | } |
| 14308 | } |
| 14309 | /* It might be that the GetItem call fails even |
| 14310 | though the key is present in the dictionary, |
| 14311 | namely when this happens during a stack overflow. */ |
| 14312 | Py_ALLOW_RECURSION |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14313 | t = PyDict_GetItem(interned, s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14314 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14315 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14316 | if (t) { |
| 14317 | Py_INCREF(t); |
| 14318 | Py_DECREF(*p); |
| 14319 | *p = t; |
| 14320 | return; |
| 14321 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14322 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14323 | PyThreadState_GET()->recursion_critical = 1; |
Victor Stinner | 7931d9a | 2011-11-04 00:22:48 +0100 | [diff] [blame] | 14324 | if (PyDict_SetItem(interned, s, s) < 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14325 | PyErr_Clear(); |
| 14326 | PyThreadState_GET()->recursion_critical = 0; |
| 14327 | return; |
| 14328 | } |
| 14329 | PyThreadState_GET()->recursion_critical = 0; |
| 14330 | /* The two references in interned are not counted by refcnt. |
| 14331 | The deallocator will take care of this */ |
| 14332 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14333 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14334 | } |
| 14335 | |
| 14336 | void |
| 14337 | PyUnicode_InternImmortal(PyObject **p) |
| 14338 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14339 | PyUnicode_InternInPlace(p); |
| 14340 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Victor Stinner | af9e4b8 | 2011-10-23 20:07:00 +0200 | [diff] [blame] | 14341 | _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14342 | Py_INCREF(*p); |
| 14343 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14344 | } |
| 14345 | |
| 14346 | PyObject * |
| 14347 | PyUnicode_InternFromString(const char *cp) |
| 14348 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14349 | PyObject *s = PyUnicode_FromString(cp); |
| 14350 | if (s == NULL) |
| 14351 | return NULL; |
| 14352 | PyUnicode_InternInPlace(&s); |
| 14353 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14354 | } |
| 14355 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 14356 | void |
| 14357 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14358 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14359 | PyObject *keys; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14360 | PyObject *s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14361 | Py_ssize_t i, n; |
| 14362 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14363 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14364 | if (interned == NULL || !PyDict_Check(interned)) |
| 14365 | return; |
| 14366 | keys = PyDict_Keys(interned); |
| 14367 | if (keys == NULL || !PyList_Check(keys)) { |
| 14368 | PyErr_Clear(); |
| 14369 | return; |
| 14370 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14371 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14372 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 14373 | detector, interned unicode strings are not forcibly deallocated; |
| 14374 | rather, we give them their stolen references back, and then clear |
| 14375 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14376 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14377 | n = PyList_GET_SIZE(keys); |
| 14378 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14379 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14380 | for (i = 0; i < n; i++) { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14381 | s = PyList_GET_ITEM(keys, i); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14382 | if (PyUnicode_READY(s) == -1) { |
| 14383 | assert(0 && "could not ready string"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14384 | fprintf(stderr, "could not ready string\n"); |
Victor Stinner | 6b56a7f | 2011-10-04 20:04:52 +0200 | [diff] [blame] | 14385 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14386 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14387 | case SSTATE_NOT_INTERNED: |
| 14388 | /* XXX Shouldn't happen */ |
| 14389 | break; |
| 14390 | case SSTATE_INTERNED_IMMORTAL: |
| 14391 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14392 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14393 | break; |
| 14394 | case SSTATE_INTERNED_MORTAL: |
| 14395 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14396 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14397 | break; |
| 14398 | default: |
| 14399 | Py_FatalError("Inconsistent interned string state."); |
| 14400 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14401 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14402 | } |
| 14403 | fprintf(stderr, "total size of all interned strings: " |
| 14404 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 14405 | "mortal/immortal\n", mortal_size, immortal_size); |
| 14406 | Py_DECREF(keys); |
| 14407 | PyDict_Clear(interned); |
| 14408 | Py_DECREF(interned); |
| 14409 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 14410 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14411 | |
| 14412 | |
| 14413 | /********************* Unicode Iterator **************************/ |
| 14414 | |
| 14415 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14416 | PyObject_HEAD |
| 14417 | Py_ssize_t it_index; |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14418 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14419 | } unicodeiterobject; |
| 14420 | |
| 14421 | static void |
| 14422 | unicodeiter_dealloc(unicodeiterobject *it) |
| 14423 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14424 | _PyObject_GC_UNTRACK(it); |
| 14425 | Py_XDECREF(it->it_seq); |
| 14426 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14427 | } |
| 14428 | |
| 14429 | static int |
| 14430 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 14431 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14432 | Py_VISIT(it->it_seq); |
| 14433 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14434 | } |
| 14435 | |
| 14436 | static PyObject * |
| 14437 | unicodeiter_next(unicodeiterobject *it) |
| 14438 | { |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14439 | PyObject *seq, *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14440 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14441 | assert(it != NULL); |
| 14442 | seq = it->it_seq; |
| 14443 | if (seq == NULL) |
| 14444 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 14445 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14446 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14447 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 14448 | int kind = PyUnicode_KIND(seq); |
| 14449 | void *data = PyUnicode_DATA(seq); |
| 14450 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 14451 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14452 | if (item != NULL) |
| 14453 | ++it->it_index; |
| 14454 | return item; |
| 14455 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14456 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14457 | Py_DECREF(seq); |
| 14458 | it->it_seq = NULL; |
| 14459 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14460 | } |
| 14461 | |
| 14462 | static PyObject * |
| 14463 | unicodeiter_len(unicodeiterobject *it) |
| 14464 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14465 | Py_ssize_t len = 0; |
| 14466 | if (it->it_seq) |
Victor Stinner | c4f281e | 2011-10-11 22:11:42 +0200 | [diff] [blame] | 14467 | len = PyUnicode_GET_LENGTH(it->it_seq) - it->it_index; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14468 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14469 | } |
| 14470 | |
| 14471 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 14472 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14473 | static PyObject * |
| 14474 | unicodeiter_reduce(unicodeiterobject *it) |
| 14475 | { |
| 14476 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14477 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14478 | it->it_seq, it->it_index); |
| 14479 | } else { |
| 14480 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 14481 | if (u == NULL) |
| 14482 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 14483 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14484 | } |
| 14485 | } |
| 14486 | |
| 14487 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 14488 | |
| 14489 | static PyObject * |
| 14490 | unicodeiter_setstate(unicodeiterobject *it, PyObject *state) |
| 14491 | { |
| 14492 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 14493 | if (index == -1 && PyErr_Occurred()) |
| 14494 | return NULL; |
| 14495 | if (index < 0) |
| 14496 | index = 0; |
| 14497 | it->it_index = index; |
| 14498 | Py_RETURN_NONE; |
| 14499 | } |
| 14500 | |
| 14501 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 14502 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14503 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14504 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 14505 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 14506 | {"__reduce__", (PyCFunction)unicodeiter_reduce, METH_NOARGS, |
| 14507 | reduce_doc}, |
| 14508 | {"__setstate__", (PyCFunction)unicodeiter_setstate, METH_O, |
| 14509 | setstate_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14510 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14511 | }; |
| 14512 | |
| 14513 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14514 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 14515 | "str_iterator", /* tp_name */ |
| 14516 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 14517 | 0, /* tp_itemsize */ |
| 14518 | /* methods */ |
| 14519 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 14520 | 0, /* tp_print */ |
| 14521 | 0, /* tp_getattr */ |
| 14522 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 14523 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14524 | 0, /* tp_repr */ |
| 14525 | 0, /* tp_as_number */ |
| 14526 | 0, /* tp_as_sequence */ |
| 14527 | 0, /* tp_as_mapping */ |
| 14528 | 0, /* tp_hash */ |
| 14529 | 0, /* tp_call */ |
| 14530 | 0, /* tp_str */ |
| 14531 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 14532 | 0, /* tp_setattro */ |
| 14533 | 0, /* tp_as_buffer */ |
| 14534 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 14535 | 0, /* tp_doc */ |
| 14536 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 14537 | 0, /* tp_clear */ |
| 14538 | 0, /* tp_richcompare */ |
| 14539 | 0, /* tp_weaklistoffset */ |
| 14540 | PyObject_SelfIter, /* tp_iter */ |
| 14541 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 14542 | unicodeiter_methods, /* tp_methods */ |
| 14543 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14544 | }; |
| 14545 | |
| 14546 | static PyObject * |
| 14547 | unicode_iter(PyObject *seq) |
| 14548 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14549 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14550 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14551 | if (!PyUnicode_Check(seq)) { |
| 14552 | PyErr_BadInternalCall(); |
| 14553 | return NULL; |
| 14554 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14555 | if (PyUnicode_READY(seq) == -1) |
| 14556 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14557 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 14558 | if (it == NULL) |
| 14559 | return NULL; |
| 14560 | it->it_index = 0; |
| 14561 | Py_INCREF(seq); |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14562 | it->it_seq = seq; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 14563 | _PyObject_GC_TRACK(it); |
| 14564 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 14565 | } |
| 14566 | |
Martin v. Löwis | 0d3072e | 2011-10-31 08:40:56 +0100 | [diff] [blame] | 14567 | |
| 14568 | size_t |
| 14569 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 14570 | { |
| 14571 | int res = 0; |
| 14572 | while(*u++) |
| 14573 | res++; |
| 14574 | return res; |
| 14575 | } |
| 14576 | |
| 14577 | Py_UNICODE* |
| 14578 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14579 | { |
| 14580 | Py_UNICODE *u = s1; |
| 14581 | while ((*u++ = *s2++)); |
| 14582 | return s1; |
| 14583 | } |
| 14584 | |
| 14585 | Py_UNICODE* |
| 14586 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14587 | { |
| 14588 | Py_UNICODE *u = s1; |
| 14589 | while ((*u++ = *s2++)) |
| 14590 | if (n-- == 0) |
| 14591 | break; |
| 14592 | return s1; |
| 14593 | } |
| 14594 | |
| 14595 | Py_UNICODE* |
| 14596 | Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14597 | { |
| 14598 | Py_UNICODE *u1 = s1; |
| 14599 | u1 += Py_UNICODE_strlen(u1); |
| 14600 | Py_UNICODE_strcpy(u1, s2); |
| 14601 | return s1; |
| 14602 | } |
| 14603 | |
| 14604 | int |
| 14605 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 14606 | { |
| 14607 | while (*s1 && *s2 && *s1 == *s2) |
| 14608 | s1++, s2++; |
| 14609 | if (*s1 && *s2) |
| 14610 | return (*s1 < *s2) ? -1 : +1; |
| 14611 | if (*s1) |
| 14612 | return 1; |
| 14613 | if (*s2) |
| 14614 | return -1; |
| 14615 | return 0; |
| 14616 | } |
| 14617 | |
| 14618 | int |
| 14619 | Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 14620 | { |
| 14621 | register Py_UNICODE u1, u2; |
| 14622 | for (; n != 0; n--) { |
| 14623 | u1 = *s1; |
| 14624 | u2 = *s2; |
| 14625 | if (u1 != u2) |
| 14626 | return (u1 < u2) ? -1 : +1; |
| 14627 | if (u1 == '\0') |
| 14628 | return 0; |
| 14629 | s1++; |
| 14630 | s2++; |
| 14631 | } |
| 14632 | return 0; |
| 14633 | } |
| 14634 | |
| 14635 | Py_UNICODE* |
| 14636 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14637 | { |
| 14638 | const Py_UNICODE *p; |
| 14639 | for (p = s; *p; p++) |
| 14640 | if (*p == c) |
| 14641 | return (Py_UNICODE*)p; |
| 14642 | return NULL; |
| 14643 | } |
| 14644 | |
| 14645 | Py_UNICODE* |
| 14646 | Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) |
| 14647 | { |
| 14648 | const Py_UNICODE *p; |
| 14649 | p = s + Py_UNICODE_strlen(s); |
| 14650 | while (p != s) { |
| 14651 | p--; |
| 14652 | if (*p == c) |
| 14653 | return (Py_UNICODE*)p; |
| 14654 | } |
| 14655 | return NULL; |
| 14656 | } |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 14657 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14658 | Py_UNICODE* |
Victor Stinner | 9db1a8b | 2011-10-23 20:04:37 +0200 | [diff] [blame] | 14659 | PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14660 | { |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14661 | Py_UNICODE *u, *copy; |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14662 | Py_ssize_t len, size; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14663 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 14664 | if (!PyUnicode_Check(unicode)) { |
| 14665 | PyErr_BadArgument(); |
| 14666 | return NULL; |
| 14667 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14668 | u = PyUnicode_AsUnicodeAndSize(unicode, &len); |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14669 | if (u == NULL) |
| 14670 | return NULL; |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14671 | /* Ensure we won't overflow the size. */ |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14672 | if (len > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14673 | PyErr_NoMemory(); |
| 14674 | return NULL; |
| 14675 | } |
Victor Stinner | 57ffa9d | 2011-10-23 20:10:08 +0200 | [diff] [blame] | 14676 | size = len + 1; /* copy the null character */ |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14677 | size *= sizeof(Py_UNICODE); |
| 14678 | copy = PyMem_Malloc(size); |
| 14679 | if (copy == NULL) { |
| 14680 | PyErr_NoMemory(); |
| 14681 | return NULL; |
| 14682 | } |
Victor Stinner | 577db2c | 2011-10-11 22:12:48 +0200 | [diff] [blame] | 14683 | memcpy(copy, u, size); |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 14684 | return copy; |
| 14685 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 14686 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 14687 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 14688 | to the string.Formatter class implemented in Python. */ |
| 14689 | |
| 14690 | static PyMethodDef _string_methods[] = { |
| 14691 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 14692 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 14693 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 14694 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 14695 | {NULL, NULL} |
| 14696 | }; |
| 14697 | |
| 14698 | static struct PyModuleDef _string_module = { |
| 14699 | PyModuleDef_HEAD_INIT, |
| 14700 | "_string", |
| 14701 | PyDoc_STR("string helper module"), |
| 14702 | 0, |
| 14703 | _string_methods, |
| 14704 | NULL, |
| 14705 | NULL, |
| 14706 | NULL, |
| 14707 | NULL |
| 14708 | }; |
| 14709 | |
| 14710 | PyMODINIT_FUNC |
| 14711 | PyInit__string(void) |
| 14712 | { |
| 14713 | return PyModule_Create(&_string_module); |
| 14714 | } |
| 14715 | |
| 14716 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14717 | #ifdef __cplusplus |
| 14718 | } |
| 14719 | #endif |