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" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 45 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 46 | #include <windows.h> |
| 47 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 49 | /* Limit for the Unicode object free list */ |
| 50 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 51 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | |
| 53 | /* Limit for the Unicode object free list stay alive optimization. |
| 54 | |
| 55 | The implementation will keep allocated Unicode memory intact for |
| 56 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 57 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 58 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 59 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 60 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | malloc()-overhead) bytes of unused garbage. |
| 62 | |
| 63 | Setting the limit to 0 effectively turns the feature off. |
| 64 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 65 | Note: This is an experimental feature ! If you get core dumps when |
| 66 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 67 | |
| 68 | */ |
| 69 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 70 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 | |
| 72 | /* Endianness switches; defaults to little endian */ |
| 73 | |
| 74 | #ifdef WORDS_BIGENDIAN |
| 75 | # define BYTEORDER_IS_BIG_ENDIAN |
| 76 | #else |
| 77 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 78 | #endif |
| 79 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 80 | /* --- Globals ------------------------------------------------------------ |
| 81 | |
| 82 | The globals are initialized by the _PyUnicode_Init() API and should |
| 83 | not be used before calling that API. |
| 84 | |
| 85 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 86 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 87 | |
| 88 | #ifdef __cplusplus |
| 89 | extern "C" { |
| 90 | #endif |
| 91 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 92 | #ifdef Py_DEBUG |
| 93 | # define _PyUnicode_CHECK(op) _PyUnicode_CheckConsistency(op) |
| 94 | #else |
| 95 | # define _PyUnicode_CHECK(op) PyUnicode_Check(op) |
| 96 | #endif |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 97 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 98 | #define _PyUnicode_UTF8(op) \ |
| 99 | (((PyCompactUnicodeObject*)(op))->utf8) |
| 100 | #define PyUnicode_UTF8(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 101 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 102 | assert(PyUnicode_IS_READY(op)), \ |
| 103 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 104 | ((char*)((PyASCIIObject*)(op) + 1)) : \ |
| 105 | _PyUnicode_UTF8(op)) |
Victor Stinner | bc8b81b | 2011-09-29 19:31:34 +0200 | [diff] [blame] | 106 | #define _PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 107 | (((PyCompactUnicodeObject*)(op))->utf8_length) |
| 108 | #define PyUnicode_UTF8_LENGTH(op) \ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 109 | (assert(_PyUnicode_CHECK(op)), \ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 110 | assert(PyUnicode_IS_READY(op)), \ |
| 111 | PyUnicode_IS_COMPACT_ASCII(op) ? \ |
| 112 | ((PyASCIIObject*)(op))->length : \ |
| 113 | _PyUnicode_UTF8_LENGTH(op)) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 114 | #define _PyUnicode_WSTR(op) \ |
| 115 | (((PyASCIIObject*)(op))->wstr) |
| 116 | #define _PyUnicode_WSTR_LENGTH(op) \ |
| 117 | (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 118 | #define _PyUnicode_LENGTH(op) \ |
| 119 | (((PyASCIIObject *)(op))->length) |
| 120 | #define _PyUnicode_STATE(op) \ |
| 121 | (((PyASCIIObject *)(op))->state) |
| 122 | #define _PyUnicode_HASH(op) \ |
| 123 | (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 124 | #define _PyUnicode_KIND(op) \ |
| 125 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 126 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 127 | #define _PyUnicode_GET_LENGTH(op) \ |
| 128 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 129 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 130 | #define _PyUnicode_DATA_ANY(op) \ |
| 131 | (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 132 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 133 | #undef PyUnicode_READY |
| 134 | #define PyUnicode_READY(op) \ |
| 135 | (assert(_PyUnicode_CHECK(op)), \ |
| 136 | (PyUnicode_IS_READY(op) ? \ |
Victor Stinner | a5f9163 | 2011-10-04 01:07:11 +0200 | [diff] [blame] | 137 | 0 : \ |
| 138 | _PyUnicode_Ready((PyObject *)(op)))) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 139 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 140 | #define _PyUnicode_READY_REPLACE(p_obj) \ |
| 141 | (assert(_PyUnicode_CHECK(*p_obj)), \ |
| 142 | (PyUnicode_IS_READY(*p_obj) ? \ |
| 143 | 0 : _PyUnicode_ReadyReplace((PyObject **)(p_obj)))) |
| 144 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 145 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 146 | (assert(_PyUnicode_CHECK(op)), \ |
| 147 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 148 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 149 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 150 | (assert(_PyUnicode_CHECK(op)), \ |
| 151 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 152 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 153 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 154 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 155 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 156 | (assert(_PyUnicode_CHECK(op)), \ |
| 157 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 158 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 159 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 160 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 161 | /* true if the Unicode object has an allocated wstr memory block |
| 162 | (not shared with other data) */ |
| 163 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 164 | (assert(_PyUnicode_CHECK(op)), \ |
| 165 | (_PyUnicode_WSTR(op) && \ |
| 166 | (!PyUnicode_IS_READY(op) || \ |
| 167 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 168 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 169 | /* Generic helper macro to convert characters of different types. |
| 170 | from_type and to_type have to be valid type names, begin and end |
| 171 | are pointers to the source characters which should be of type |
| 172 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 173 | buffer where the result characters are written to. */ |
| 174 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 175 | do { \ |
| 176 | const from_type *iter_; to_type *to_; \ |
| 177 | for (iter_ = (begin), to_ = (to_type *)(to); \ |
| 178 | iter_ < (end); \ |
| 179 | ++iter_, ++to_) { \ |
| 180 | *to_ = (to_type)*iter_; \ |
| 181 | } \ |
| 182 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 183 | |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 184 | /* The Unicode string has been modified: reset the hash */ |
| 185 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) |
| 186 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 187 | /* This dictionary holds all interned unicode strings. Note that references |
| 188 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 189 | When the interned string reaches a refcnt of 0 the string deallocation |
| 190 | function will delete the reference from this dictionary. |
| 191 | |
| 192 | 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] | 193 | 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] | 194 | */ |
| 195 | static PyObject *interned; |
| 196 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 197 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 198 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 199 | |
| 200 | /* Single character Unicode strings in the Latin-1 range are being |
| 201 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 202 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 203 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 204 | /* Fast detection of the most frequent whitespace characters */ |
| 205 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 206 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 207 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 208 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 209 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 210 | /* case 0x000C: * FORM FEED */ |
| 211 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 212 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 213 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 214 | /* case 0x001C: * FILE SEPARATOR */ |
| 215 | /* case 0x001D: * GROUP SEPARATOR */ |
| 216 | /* case 0x001E: * RECORD SEPARATOR */ |
| 217 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 218 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 219 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 220 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 221 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 222 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 223 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 224 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 225 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 226 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 227 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 228 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 229 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 230 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 231 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 232 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 235 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 236 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 237 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 238 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 239 | static PyObject * |
| 240 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 241 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 242 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 243 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 244 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 245 | static void |
| 246 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 247 | const char *encoding, |
| 248 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 249 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 250 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 251 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 252 | /* Same for linebreaks */ |
| 253 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 254 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 255 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 256 | /* 0x000B, * LINE TABULATION */ |
| 257 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 258 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 259 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 260 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 261 | /* 0x001C, * FILE SEPARATOR */ |
| 262 | /* 0x001D, * GROUP SEPARATOR */ |
| 263 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 264 | 0, 0, 0, 0, 1, 1, 1, 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 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 270 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 271 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 272 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 273 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 274 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 275 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 276 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 277 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 280 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 281 | 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] | 282 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 283 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 284 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 285 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 286 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 287 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 288 | /* This is actually an illegal character, so it should |
| 289 | not be passed to unichr. */ |
| 290 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 291 | #endif |
| 292 | } |
| 293 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 294 | #ifdef Py_DEBUG |
| 295 | static int |
| 296 | _PyUnicode_CheckConsistency(void *op) |
| 297 | { |
| 298 | PyASCIIObject *ascii; |
| 299 | unsigned int kind; |
| 300 | |
| 301 | assert(PyUnicode_Check(op)); |
| 302 | |
| 303 | ascii = (PyASCIIObject *)op; |
| 304 | kind = ascii->state.kind; |
| 305 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 306 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 307 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 308 | assert(ascii->state.ready == 1); |
| 309 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 310 | else { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 311 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 312 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 313 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 314 | if (ascii->state.compact == 1) { |
| 315 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 316 | assert(kind == PyUnicode_1BYTE_KIND |
| 317 | || kind == PyUnicode_2BYTE_KIND |
| 318 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 319 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 320 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 321 | assert (compact->utf8 != data); |
| 322 | } else { |
| 323 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 324 | |
| 325 | data = unicode->data.any; |
| 326 | if (kind == PyUnicode_WCHAR_KIND) { |
| 327 | assert(ascii->state.compact == 0); |
| 328 | assert(ascii->state.ascii == 0); |
| 329 | assert(ascii->state.ready == 0); |
| 330 | assert(ascii->wstr != NULL); |
| 331 | assert(data == NULL); |
| 332 | assert(compact->utf8 == NULL); |
| 333 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
| 334 | } |
| 335 | else { |
| 336 | assert(kind == PyUnicode_1BYTE_KIND |
| 337 | || kind == PyUnicode_2BYTE_KIND |
| 338 | || kind == PyUnicode_4BYTE_KIND); |
| 339 | assert(ascii->state.compact == 0); |
| 340 | assert(ascii->state.ready == 1); |
| 341 | assert(data != NULL); |
| 342 | if (ascii->state.ascii) { |
| 343 | assert (compact->utf8 == data); |
| 344 | assert (compact->utf8_length == ascii->length); |
| 345 | } |
| 346 | else |
| 347 | assert (compact->utf8 != data); |
| 348 | } |
| 349 | } |
| 350 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 351 | if ( |
| 352 | #if SIZEOF_WCHAR_T == 2 |
| 353 | kind == PyUnicode_2BYTE_KIND |
| 354 | #else |
| 355 | kind == PyUnicode_4BYTE_KIND |
| 356 | #endif |
| 357 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 358 | { |
| 359 | assert(ascii->wstr == data); |
| 360 | assert(compact->wstr_length == ascii->length); |
| 361 | } else |
| 362 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 363 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame] | 364 | |
| 365 | if (compact->utf8 == NULL) |
| 366 | assert(compact->utf8_length == 0); |
| 367 | if (ascii->wstr == NULL) |
| 368 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 369 | } |
| 370 | return 1; |
| 371 | } |
| 372 | #endif |
| 373 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 374 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 375 | |
| 376 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 377 | to keep things simple, we use a single bitmask, using the least 5 |
| 378 | bits from each unicode characters as the bit index. */ |
| 379 | |
| 380 | /* the linebreak mask is set up by Unicode_Init below */ |
| 381 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 382 | #if LONG_BIT >= 128 |
| 383 | #define BLOOM_WIDTH 128 |
| 384 | #elif LONG_BIT >= 64 |
| 385 | #define BLOOM_WIDTH 64 |
| 386 | #elif LONG_BIT >= 32 |
| 387 | #define BLOOM_WIDTH 32 |
| 388 | #else |
| 389 | #error "LONG_BIT is smaller than 32" |
| 390 | #endif |
| 391 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 392 | #define BLOOM_MASK unsigned long |
| 393 | |
| 394 | static BLOOM_MASK bloom_linebreak; |
| 395 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 396 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 397 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 398 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 399 | #define BLOOM_LINEBREAK(ch) \ |
| 400 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 401 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 402 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 403 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 404 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 405 | { |
| 406 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 407 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 408 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 409 | Py_ssize_t i; |
| 410 | |
| 411 | mask = 0; |
| 412 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 413 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 414 | |
| 415 | return mask; |
| 416 | } |
| 417 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 418 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 419 | (BLOOM(mask, chr) \ |
| 420 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 421 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 422 | /* --- Unicode Object ----------------------------------------------------- */ |
| 423 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 424 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 425 | fixup(PyUnicodeObject *self, Py_UCS4 (*fixfct)(PyUnicodeObject *s)); |
| 426 | |
| 427 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 428 | Py_ssize_t size, Py_UCS4 ch, |
| 429 | int direction) |
| 430 | { |
| 431 | /* like wcschr, but doesn't stop at NULL characters */ |
| 432 | Py_ssize_t i; |
| 433 | if (direction == 1) { |
| 434 | for(i = 0; i < size; i++) |
| 435 | if (PyUnicode_READ(kind, s, i) == ch) |
| 436 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 437 | } |
| 438 | else { |
| 439 | for(i = size-1; i >= 0; i--) |
| 440 | if (PyUnicode_READ(kind, s, i) == ch) |
| 441 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 442 | } |
| 443 | return NULL; |
| 444 | } |
| 445 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 446 | static PyObject* |
| 447 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 448 | { |
| 449 | Py_ssize_t char_size; |
| 450 | Py_ssize_t struct_size; |
| 451 | Py_ssize_t new_size; |
| 452 | int share_wstr; |
| 453 | |
| 454 | assert(PyUnicode_IS_READY(unicode)); |
| 455 | char_size = PyUnicode_CHARACTER_SIZE(unicode); |
| 456 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 457 | struct_size = sizeof(PyASCIIObject); |
| 458 | else |
| 459 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 460 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 461 | |
| 462 | _Py_DEC_REFTOTAL; |
| 463 | _Py_ForgetReference(unicode); |
| 464 | |
| 465 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 466 | PyErr_NoMemory(); |
| 467 | return NULL; |
| 468 | } |
| 469 | new_size = (struct_size + (length + 1) * char_size); |
| 470 | |
| 471 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 472 | if (unicode == NULL) { |
| 473 | PyObject_Del(unicode); |
| 474 | PyErr_NoMemory(); |
| 475 | return NULL; |
| 476 | } |
| 477 | _Py_NewReference(unicode); |
| 478 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 479 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 480 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 481 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 482 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 483 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 484 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 485 | length, 0); |
| 486 | return unicode; |
| 487 | } |
| 488 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 489 | static int |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 490 | resize_inplace(PyUnicodeObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 491 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 492 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 493 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 494 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 495 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 496 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 497 | |
| 498 | if (PyUnicode_IS_READY(unicode)) { |
| 499 | Py_ssize_t char_size; |
| 500 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 501 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 502 | void *data; |
| 503 | |
| 504 | data = _PyUnicode_DATA_ANY(unicode); |
| 505 | assert(data != NULL); |
| 506 | char_size = PyUnicode_CHARACTER_SIZE(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 507 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 508 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 509 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 510 | { |
| 511 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 512 | _PyUnicode_UTF8(unicode) = NULL; |
| 513 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 514 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 515 | |
| 516 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 517 | PyErr_NoMemory(); |
| 518 | return -1; |
| 519 | } |
| 520 | new_size = (length + 1) * char_size; |
| 521 | |
| 522 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 523 | if (data == NULL) { |
| 524 | PyErr_NoMemory(); |
| 525 | return -1; |
| 526 | } |
| 527 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 528 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 529 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 530 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 531 | } |
| 532 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 533 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 534 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 535 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 536 | _PyUnicode_LENGTH(unicode) = length; |
| 537 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 538 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
| 539 | _PyUnicode_CHECK(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 540 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 541 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 542 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 543 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 544 | |
| 545 | /* check for integer overflow */ |
| 546 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 547 | PyErr_NoMemory(); |
| 548 | return -1; |
| 549 | } |
| 550 | wstr = _PyUnicode_WSTR(unicode); |
| 551 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 552 | if (!wstr) { |
| 553 | PyErr_NoMemory(); |
| 554 | return -1; |
| 555 | } |
| 556 | _PyUnicode_WSTR(unicode) = wstr; |
| 557 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 558 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 559 | _PyUnicode_CHECK(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 563 | static PyObject* |
| 564 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 565 | { |
| 566 | Py_ssize_t copy_length; |
| 567 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 568 | PyObject *copy; |
| 569 | assert(PyUnicode_IS_READY(unicode)); |
| 570 | |
| 571 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 572 | if (copy == NULL) |
| 573 | return NULL; |
| 574 | |
| 575 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
| 576 | if (PyUnicode_CopyCharacters(copy, 0, |
| 577 | unicode, 0, |
| 578 | copy_length) < 0) |
| 579 | { |
| 580 | Py_DECREF(copy); |
| 581 | return NULL; |
| 582 | } |
| 583 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 584 | } |
| 585 | else { |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 586 | PyUnicodeObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 587 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 588 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 589 | w = _PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 590 | if (w == NULL) |
| 591 | return NULL; |
| 592 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 593 | copy_length = Py_MIN(copy_length, length); |
| 594 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 595 | copy_length); |
| 596 | return (PyObject*)w; |
| 597 | } |
| 598 | } |
| 599 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 600 | /* 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] | 601 | Ux0000 terminated; some code (e.g. new_identifier) |
| 602 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 603 | |
| 604 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 605 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 606 | |
| 607 | */ |
| 608 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 609 | #ifdef Py_DEBUG |
| 610 | int unicode_old_new_calls = 0; |
| 611 | #endif |
| 612 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 613 | static PyUnicodeObject * |
| 614 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 615 | { |
| 616 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 617 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 618 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 619 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 620 | if (length == 0 && unicode_empty != NULL) { |
| 621 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 622 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 625 | /* Ensure we won't overflow the size. */ |
| 626 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 627 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 628 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 629 | if (length < 0) { |
| 630 | PyErr_SetString(PyExc_SystemError, |
| 631 | "Negative size passed to _PyUnicode_New"); |
| 632 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 635 | #ifdef Py_DEBUG |
| 636 | ++unicode_old_new_calls; |
| 637 | #endif |
| 638 | |
| 639 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 640 | if (unicode == NULL) |
| 641 | return NULL; |
| 642 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 643 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 644 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 645 | PyErr_NoMemory(); |
| 646 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 647 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 648 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 649 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 650 | * the caller fails before initializing str -- unicode_resize() |
| 651 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 652 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 653 | * We don't want unicode_resize to read uninitialized memory in |
| 654 | * that case. |
| 655 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 656 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 657 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 658 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 659 | _PyUnicode_HASH(unicode) = -1; |
| 660 | _PyUnicode_STATE(unicode).interned = 0; |
| 661 | _PyUnicode_STATE(unicode).kind = 0; |
| 662 | _PyUnicode_STATE(unicode).compact = 0; |
| 663 | _PyUnicode_STATE(unicode).ready = 0; |
| 664 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 665 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 666 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 667 | _PyUnicode_UTF8(unicode) = NULL; |
| 668 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 669 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 670 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 671 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 672 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 673 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 674 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 675 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 676 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 679 | static const char* |
| 680 | unicode_kind_name(PyObject *unicode) |
| 681 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 682 | /* don't check consistency: unicode_kind_name() is called from |
| 683 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 684 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 685 | { |
| 686 | if (!PyUnicode_IS_READY(unicode)) |
| 687 | return "wstr"; |
| 688 | switch(PyUnicode_KIND(unicode)) |
| 689 | { |
| 690 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 691 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 692 | return "legacy ascii"; |
| 693 | else |
| 694 | return "legacy latin1"; |
| 695 | case PyUnicode_2BYTE_KIND: |
| 696 | return "legacy UCS2"; |
| 697 | case PyUnicode_4BYTE_KIND: |
| 698 | return "legacy UCS4"; |
| 699 | default: |
| 700 | return "<legacy invalid kind>"; |
| 701 | } |
| 702 | } |
| 703 | assert(PyUnicode_IS_READY(unicode)); |
| 704 | switch(PyUnicode_KIND(unicode)) |
| 705 | { |
| 706 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 707 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 708 | return "ascii"; |
| 709 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 710 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 711 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 712 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 713 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 714 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 715 | default: |
| 716 | return "<invalid compact kind>"; |
| 717 | } |
| 718 | } |
| 719 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 720 | #ifdef Py_DEBUG |
| 721 | int unicode_new_new_calls = 0; |
| 722 | |
| 723 | /* Functions wrapping macros for use in debugger */ |
| 724 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 725 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | void *_PyUnicode_compact_data(void *unicode) { |
| 729 | return _PyUnicode_COMPACT_DATA(unicode); |
| 730 | } |
| 731 | void *_PyUnicode_data(void *unicode){ |
| 732 | printf("obj %p\n", unicode); |
| 733 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 734 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 735 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 736 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 737 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 738 | return PyUnicode_DATA(unicode); |
| 739 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 740 | |
| 741 | void |
| 742 | _PyUnicode_Dump(PyObject *op) |
| 743 | { |
| 744 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 745 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 746 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 747 | void *data; |
| 748 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 749 | if (ascii->state.compact) |
| 750 | data = (compact + 1); |
| 751 | else |
| 752 | data = unicode->data.any; |
| 753 | if (ascii->wstr == data) |
| 754 | printf("shared "); |
| 755 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 756 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 757 | printf(" (%zu), ", compact->wstr_length); |
| 758 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 759 | printf("shared "); |
| 760 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 761 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 762 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 763 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 764 | #endif |
| 765 | |
| 766 | PyObject * |
| 767 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 768 | { |
| 769 | PyObject *obj; |
| 770 | PyCompactUnicodeObject *unicode; |
| 771 | void *data; |
| 772 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 773 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 774 | Py_ssize_t char_size; |
| 775 | Py_ssize_t struct_size; |
| 776 | |
| 777 | /* Optimization for empty strings */ |
| 778 | if (size == 0 && unicode_empty != NULL) { |
| 779 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 780 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | #ifdef Py_DEBUG |
| 784 | ++unicode_new_new_calls; |
| 785 | #endif |
| 786 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 787 | is_ascii = 0; |
| 788 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 789 | struct_size = sizeof(PyCompactUnicodeObject); |
| 790 | if (maxchar < 128) { |
| 791 | kind_state = PyUnicode_1BYTE_KIND; |
| 792 | char_size = 1; |
| 793 | is_ascii = 1; |
| 794 | struct_size = sizeof(PyASCIIObject); |
| 795 | } |
| 796 | else if (maxchar < 256) { |
| 797 | kind_state = PyUnicode_1BYTE_KIND; |
| 798 | char_size = 1; |
| 799 | } |
| 800 | else if (maxchar < 65536) { |
| 801 | kind_state = PyUnicode_2BYTE_KIND; |
| 802 | char_size = 2; |
| 803 | if (sizeof(wchar_t) == 2) |
| 804 | is_sharing = 1; |
| 805 | } |
| 806 | else { |
| 807 | kind_state = PyUnicode_4BYTE_KIND; |
| 808 | char_size = 4; |
| 809 | if (sizeof(wchar_t) == 4) |
| 810 | is_sharing = 1; |
| 811 | } |
| 812 | |
| 813 | /* Ensure we won't overflow the size. */ |
| 814 | if (size < 0) { |
| 815 | PyErr_SetString(PyExc_SystemError, |
| 816 | "Negative size passed to PyUnicode_New"); |
| 817 | return NULL; |
| 818 | } |
| 819 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 820 | return PyErr_NoMemory(); |
| 821 | |
| 822 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 823 | * PyObject_New() so we are able to allocate space for the object and |
| 824 | * it's data buffer. |
| 825 | */ |
| 826 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 827 | if (obj == NULL) |
| 828 | return PyErr_NoMemory(); |
| 829 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 830 | if (obj == NULL) |
| 831 | return NULL; |
| 832 | |
| 833 | unicode = (PyCompactUnicodeObject *)obj; |
| 834 | if (is_ascii) |
| 835 | data = ((PyASCIIObject*)obj) + 1; |
| 836 | else |
| 837 | data = unicode + 1; |
| 838 | _PyUnicode_LENGTH(unicode) = size; |
| 839 | _PyUnicode_HASH(unicode) = -1; |
| 840 | _PyUnicode_STATE(unicode).interned = 0; |
| 841 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 842 | _PyUnicode_STATE(unicode).compact = 1; |
| 843 | _PyUnicode_STATE(unicode).ready = 1; |
| 844 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 845 | if (is_ascii) { |
| 846 | ((char*)data)[size] = 0; |
| 847 | _PyUnicode_WSTR(unicode) = NULL; |
| 848 | } |
| 849 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 850 | ((char*)data)[size] = 0; |
| 851 | _PyUnicode_WSTR(unicode) = NULL; |
| 852 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 853 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 854 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 855 | } |
| 856 | else { |
| 857 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 858 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 859 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 860 | ((Py_UCS2*)data)[size] = 0; |
| 861 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 862 | ((Py_UCS4*)data)[size] = 0; |
| 863 | if (is_sharing) { |
| 864 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 865 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 866 | } |
| 867 | else { |
| 868 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 869 | _PyUnicode_WSTR(unicode) = NULL; |
| 870 | } |
| 871 | } |
| 872 | return obj; |
| 873 | } |
| 874 | |
| 875 | #if SIZEOF_WCHAR_T == 2 |
| 876 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 877 | will decode surrogate pairs, the other conversions are implemented as macros |
| 878 | for efficency. |
| 879 | |
| 880 | This function assumes that unicode can hold one more code point than wstr |
| 881 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 882 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 883 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 884 | PyUnicodeObject *unicode) |
| 885 | { |
| 886 | const wchar_t *iter; |
| 887 | Py_UCS4 *ucs4_out; |
| 888 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 889 | assert(unicode != NULL); |
| 890 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 891 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 892 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 893 | |
| 894 | for (iter = begin; iter < end; ) { |
| 895 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 896 | _PyUnicode_GET_LENGTH(unicode))); |
| 897 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 898 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 899 | { |
| 900 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 901 | iter += 2; |
| 902 | } |
| 903 | else { |
| 904 | *ucs4_out++ = *iter; |
| 905 | iter++; |
| 906 | } |
| 907 | } |
| 908 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 909 | _PyUnicode_GET_LENGTH(unicode))); |
| 910 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 911 | } |
| 912 | #endif |
| 913 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 914 | static int |
| 915 | _PyUnicode_Dirty(PyObject *unicode) |
| 916 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 917 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 918 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 919 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 920 | "Cannot modify a string having more than 1 reference"); |
| 921 | return -1; |
| 922 | } |
| 923 | _PyUnicode_DIRTY(unicode); |
| 924 | return 0; |
| 925 | } |
| 926 | |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 927 | Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 928 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 929 | PyObject *from, Py_ssize_t from_start, |
| 930 | Py_ssize_t how_many) |
| 931 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 932 | unsigned int from_kind, to_kind; |
| 933 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 934 | |
Victor Stinner | b153615 | 2011-09-30 02:26:10 +0200 | [diff] [blame] | 935 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 936 | PyErr_BadInternalCall(); |
| 937 | return -1; |
| 938 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 939 | |
| 940 | if (PyUnicode_READY(from)) |
| 941 | return -1; |
| 942 | if (PyUnicode_READY(to)) |
| 943 | return -1; |
| 944 | |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 945 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 946 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 947 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 948 | "Cannot write %zi characters at %zi " |
| 949 | "in a string of %zi characters", |
| 950 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 951 | return -1; |
| 952 | } |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 953 | if (how_many == 0) |
| 954 | return 0; |
| 955 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 956 | if (_PyUnicode_Dirty(to)) |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 957 | return -1; |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 958 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 959 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 960 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 961 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 962 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 963 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 964 | if (from_kind == to_kind |
| 965 | /* deny latin1 => ascii */ |
| 966 | && PyUnicode_MAX_CHAR_VALUE(to) >= PyUnicode_MAX_CHAR_VALUE(from)) |
| 967 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 968 | Py_MEMCPY((char*)to_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 969 | + PyUnicode_KIND_SIZE(to_kind, to_start), |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 970 | (char*)from_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 971 | + PyUnicode_KIND_SIZE(from_kind, from_start), |
| 972 | PyUnicode_KIND_SIZE(to_kind, how_many)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 973 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 974 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 975 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 976 | { |
| 977 | _PyUnicode_CONVERT_BYTES( |
| 978 | Py_UCS1, Py_UCS2, |
| 979 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 980 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 981 | PyUnicode_2BYTE_DATA(to) + to_start |
| 982 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 983 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 984 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 985 | && to_kind == PyUnicode_4BYTE_KIND) |
| 986 | { |
| 987 | _PyUnicode_CONVERT_BYTES( |
| 988 | Py_UCS1, Py_UCS4, |
| 989 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 990 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 991 | PyUnicode_4BYTE_DATA(to) + to_start |
| 992 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 993 | } |
| 994 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 995 | && to_kind == PyUnicode_4BYTE_KIND) |
| 996 | { |
| 997 | _PyUnicode_CONVERT_BYTES( |
| 998 | Py_UCS2, Py_UCS4, |
| 999 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 1000 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 1001 | PyUnicode_4BYTE_DATA(to) + to_start |
| 1002 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 1003 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1004 | else { |
| 1005 | int invalid_kinds; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1006 | |
| 1007 | /* check if max_char(from substring) <= max_char(to) */ |
| 1008 | if (from_kind > to_kind |
| 1009 | /* latin1 => ascii */ |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1010 | || (PyUnicode_IS_ASCII(to) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1011 | && to_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1012 | && !PyUnicode_IS_ASCII(from))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1013 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1014 | /* slow path to check for character overflow */ |
| 1015 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1016 | Py_UCS4 ch, maxchar; |
| 1017 | Py_ssize_t i; |
| 1018 | |
| 1019 | maxchar = 0; |
| 1020 | invalid_kinds = 0; |
| 1021 | for (i=0; i < how_many; i++) { |
| 1022 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1023 | if (ch > maxchar) { |
| 1024 | maxchar = ch; |
| 1025 | if (maxchar > to_maxchar) { |
| 1026 | invalid_kinds = 1; |
| 1027 | break; |
| 1028 | } |
| 1029 | } |
| 1030 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1031 | } |
| 1032 | } |
| 1033 | else |
| 1034 | invalid_kinds = 1; |
| 1035 | if (invalid_kinds) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1036 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1037 | "Cannot copy %s characters " |
| 1038 | "into a string of %s characters", |
| 1039 | unicode_kind_name(from), |
| 1040 | unicode_kind_name(to)); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1041 | return -1; |
| 1042 | } |
| 1043 | } |
| 1044 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1047 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1048 | correct string length can be computed before converting a string to UCS4. |
| 1049 | This function counts single surrogates as a character and not as a pair. |
| 1050 | |
| 1051 | Return 0 on success, or -1 on error. */ |
| 1052 | static int |
| 1053 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1054 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1055 | { |
| 1056 | const wchar_t *iter; |
| 1057 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1058 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1059 | if (num_surrogates == NULL || maxchar == NULL) { |
| 1060 | PyErr_SetString(PyExc_SystemError, |
| 1061 | "unexpected NULL arguments to " |
| 1062 | "PyUnicode_FindMaxCharAndNumSurrogatePairs"); |
| 1063 | return -1; |
| 1064 | } |
| 1065 | |
| 1066 | *num_surrogates = 0; |
| 1067 | *maxchar = 0; |
| 1068 | |
| 1069 | for (iter = begin; iter < end; ) { |
| 1070 | if (*iter > *maxchar) |
| 1071 | *maxchar = *iter; |
| 1072 | #if SIZEOF_WCHAR_T == 2 |
| 1073 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1074 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1075 | { |
| 1076 | Py_UCS4 surrogate_val; |
| 1077 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1078 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1079 | ++(*num_surrogates); |
| 1080 | if (surrogate_val > *maxchar) |
| 1081 | *maxchar = surrogate_val; |
| 1082 | iter += 2; |
| 1083 | } |
| 1084 | else |
| 1085 | iter++; |
| 1086 | #else |
| 1087 | iter++; |
| 1088 | #endif |
| 1089 | } |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | #ifdef Py_DEBUG |
| 1094 | int unicode_ready_calls = 0; |
| 1095 | #endif |
| 1096 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1097 | static int |
| 1098 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1099 | { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1100 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1101 | wchar_t *end; |
| 1102 | Py_UCS4 maxchar = 0; |
| 1103 | Py_ssize_t num_surrogates; |
| 1104 | #if SIZEOF_WCHAR_T == 2 |
| 1105 | Py_ssize_t length_wo_surrogates; |
| 1106 | #endif |
| 1107 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1108 | assert(p_obj != NULL); |
| 1109 | unicode = (PyUnicodeObject *)*p_obj; |
| 1110 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1111 | /* _PyUnicode_Ready() is only intented for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1112 | strings were created using _PyObject_New() and where no canonical |
| 1113 | representation (the str field) has been set yet aka strings |
| 1114 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1115 | assert(_PyUnicode_CHECK(unicode)); |
| 1116 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1117 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1118 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1119 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1120 | /* Actually, it should neither be interned nor be anything else: */ |
| 1121 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1122 | |
| 1123 | #ifdef Py_DEBUG |
| 1124 | ++unicode_ready_calls; |
| 1125 | #endif |
| 1126 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1127 | #ifdef Py_DEBUG |
| 1128 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1129 | #else |
| 1130 | if (replace && Py_REFCNT(unicode) != 1) |
| 1131 | replace = 0; |
| 1132 | #endif |
| 1133 | if (replace) { |
| 1134 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1135 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1136 | /* Optimization for empty strings */ |
| 1137 | if (len == 0) { |
| 1138 | Py_INCREF(unicode_empty); |
| 1139 | Py_DECREF(*p_obj); |
| 1140 | *p_obj = unicode_empty; |
| 1141 | return 0; |
| 1142 | } |
| 1143 | if (len == 1 && wstr[0] < 256) { |
| 1144 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1145 | if (latin1_char == NULL) |
| 1146 | return -1; |
| 1147 | Py_DECREF(*p_obj); |
| 1148 | *p_obj = latin1_char; |
| 1149 | return 0; |
| 1150 | } |
| 1151 | } |
| 1152 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1153 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1154 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1155 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1156 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1157 | |
| 1158 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1159 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1160 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1161 | PyErr_NoMemory(); |
| 1162 | return -1; |
| 1163 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1164 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1165 | _PyUnicode_WSTR(unicode), end, |
| 1166 | PyUnicode_1BYTE_DATA(unicode)); |
| 1167 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1168 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1169 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1170 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1171 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1172 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1173 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1174 | } |
| 1175 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1176 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1177 | _PyUnicode_UTF8(unicode) = NULL; |
| 1178 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1179 | } |
| 1180 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1181 | _PyUnicode_WSTR(unicode) = NULL; |
| 1182 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1183 | } |
| 1184 | /* In this case we might have to convert down from 4-byte native |
| 1185 | wchar_t to 2-byte unicode. */ |
| 1186 | else if (maxchar < 65536) { |
| 1187 | assert(num_surrogates == 0 && |
| 1188 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1189 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1190 | #if SIZEOF_WCHAR_T == 2 |
| 1191 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1192 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1193 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1194 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1195 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1196 | _PyUnicode_UTF8(unicode) = NULL; |
| 1197 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1198 | #else |
| 1199 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1200 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1201 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1202 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1203 | PyErr_NoMemory(); |
| 1204 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1205 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1206 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1207 | _PyUnicode_WSTR(unicode), end, |
| 1208 | PyUnicode_2BYTE_DATA(unicode)); |
| 1209 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1210 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1211 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1212 | _PyUnicode_UTF8(unicode) = NULL; |
| 1213 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1214 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1215 | _PyUnicode_WSTR(unicode) = NULL; |
| 1216 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1217 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1218 | } |
| 1219 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1220 | else { |
| 1221 | #if SIZEOF_WCHAR_T == 2 |
| 1222 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1223 | new normalized 4-byte version. */ |
| 1224 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1225 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1226 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1227 | PyErr_NoMemory(); |
| 1228 | return -1; |
| 1229 | } |
| 1230 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1231 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1232 | _PyUnicode_UTF8(unicode) = NULL; |
| 1233 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1234 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1235 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1236 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1237 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1238 | _PyUnicode_WSTR(unicode) = NULL; |
| 1239 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1240 | #else |
| 1241 | assert(num_surrogates == 0); |
| 1242 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1243 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1244 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1245 | _PyUnicode_UTF8(unicode) = NULL; |
| 1246 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1247 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1248 | #endif |
| 1249 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1250 | } |
| 1251 | _PyUnicode_STATE(unicode).ready = 1; |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1255 | int |
| 1256 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1257 | { |
| 1258 | return unicode_ready(op, 1); |
| 1259 | } |
| 1260 | |
| 1261 | int |
| 1262 | _PyUnicode_Ready(PyObject *op) |
| 1263 | { |
| 1264 | return unicode_ready(&op, 0); |
| 1265 | } |
| 1266 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1267 | static void |
| 1268 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1269 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1270 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1271 | case SSTATE_NOT_INTERNED: |
| 1272 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1273 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1274 | case SSTATE_INTERNED_MORTAL: |
| 1275 | /* revive dead object temporarily for DelItem */ |
| 1276 | Py_REFCNT(unicode) = 3; |
| 1277 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 1278 | Py_FatalError( |
| 1279 | "deletion of interned string failed"); |
| 1280 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1281 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1282 | case SSTATE_INTERNED_IMMORTAL: |
| 1283 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1284 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1285 | default: |
| 1286 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1289 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1290 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1291 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1292 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1293 | |
| 1294 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1295 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1296 | } |
| 1297 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1298 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1299 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1300 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1304 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1305 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1306 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1307 | if (Py_REFCNT(unicode) != 1) |
| 1308 | return 0; |
| 1309 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1310 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1311 | assert (unicode != unicode_empty); |
| 1312 | #ifdef Py_DEBUG |
| 1313 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND |
| 1314 | && PyUnicode_GET_LENGTH(unicode) == 1) |
| 1315 | { |
| 1316 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1317 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1318 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1319 | } |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1320 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1321 | return 1; |
| 1322 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1323 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1324 | static int |
| 1325 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1326 | { |
| 1327 | PyObject *unicode; |
| 1328 | Py_ssize_t old_length; |
| 1329 | |
| 1330 | assert(p_unicode != NULL); |
| 1331 | unicode = *p_unicode; |
| 1332 | |
| 1333 | assert(unicode != NULL); |
| 1334 | assert(PyUnicode_Check(unicode)); |
| 1335 | assert(0 <= length); |
| 1336 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1337 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1338 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1339 | else |
| 1340 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1341 | if (old_length == length) |
| 1342 | return 0; |
| 1343 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1344 | if (!unicode_resizable(unicode)) { |
| 1345 | PyObject *copy = resize_copy(unicode, length); |
| 1346 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1347 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1348 | Py_DECREF(*p_unicode); |
| 1349 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1350 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1353 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1354 | *p_unicode = resize_compact(unicode, length); |
| 1355 | if (*p_unicode == NULL) |
| 1356 | return -1; |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 1357 | _PyUnicode_CHECK(*p_unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1358 | return 0; |
| 1359 | } else |
| 1360 | return resize_inplace((PyUnicodeObject*)unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1363 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1364 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1365 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1366 | PyObject *unicode; |
| 1367 | if (p_unicode == NULL) { |
| 1368 | PyErr_BadInternalCall(); |
| 1369 | return -1; |
| 1370 | } |
| 1371 | unicode = *p_unicode; |
| 1372 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 |
| 1373 | || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) |
| 1374 | { |
| 1375 | PyErr_BadInternalCall(); |
| 1376 | return -1; |
| 1377 | } |
| 1378 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1379 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1380 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1381 | static PyObject* |
| 1382 | get_latin1_char(unsigned char ch) |
| 1383 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1384 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1385 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1386 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1387 | if (!unicode) |
| 1388 | return NULL; |
| 1389 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
| 1390 | unicode_latin1[ch] = unicode; |
| 1391 | } |
| 1392 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1393 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1394 | } |
| 1395 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1396 | PyObject * |
| 1397 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1398 | { |
| 1399 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1400 | Py_UCS4 maxchar = 0; |
| 1401 | Py_ssize_t num_surrogates; |
| 1402 | |
| 1403 | if (u == NULL) |
| 1404 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1405 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1406 | /* If the Unicode data is known at construction time, we can apply |
| 1407 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1408 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1409 | /* Optimization for empty strings */ |
| 1410 | if (size == 0 && unicode_empty != NULL) { |
| 1411 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1412 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1413 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1414 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1415 | /* Single character Unicode objects in the Latin-1 range are |
| 1416 | shared when using this constructor */ |
| 1417 | if (size == 1 && *u < 256) |
| 1418 | return get_latin1_char((unsigned char)*u); |
| 1419 | |
| 1420 | /* If not empty and not single character, copy the Unicode data |
| 1421 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1422 | if (find_maxchar_surrogates(u, u + size, |
| 1423 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1424 | return NULL; |
| 1425 | |
| 1426 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1427 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1428 | if (!unicode) |
| 1429 | return NULL; |
| 1430 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1431 | switch (PyUnicode_KIND(unicode)) { |
| 1432 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1433 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1434 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1435 | break; |
| 1436 | case PyUnicode_2BYTE_KIND: |
| 1437 | #if Py_UNICODE_SIZE == 2 |
| 1438 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1439 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1440 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1441 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1442 | #endif |
| 1443 | break; |
| 1444 | case PyUnicode_4BYTE_KIND: |
| 1445 | #if SIZEOF_WCHAR_T == 2 |
| 1446 | /* This is the only case which has to process surrogates, thus |
| 1447 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1448 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1449 | #else |
| 1450 | assert(num_surrogates == 0); |
| 1451 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1452 | #endif |
| 1453 | break; |
| 1454 | default: |
| 1455 | assert(0 && "Impossible state"); |
| 1456 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1457 | |
| 1458 | return (PyObject *)unicode; |
| 1459 | } |
| 1460 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1461 | PyObject * |
| 1462 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1463 | { |
| 1464 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1465 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1466 | if (size < 0) { |
| 1467 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1468 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1469 | return NULL; |
| 1470 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1471 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1472 | /* If the Unicode data is known at construction time, we can apply |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1473 | some optimizations which share commonly used objects. |
| 1474 | Also, this means the input must be UTF-8, so fall back to the |
| 1475 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1476 | if (u != NULL) { |
| 1477 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1478 | /* Optimization for empty strings */ |
| 1479 | if (size == 0 && unicode_empty != NULL) { |
| 1480 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1481 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1482 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1483 | |
| 1484 | /* Single characters are shared when using this constructor. |
| 1485 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1486 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1487 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1488 | |
| 1489 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1492 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1493 | if (!unicode) |
| 1494 | return NULL; |
| 1495 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1496 | return (PyObject *)unicode; |
| 1497 | } |
| 1498 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1499 | PyObject * |
| 1500 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1501 | { |
| 1502 | size_t size = strlen(u); |
| 1503 | if (size > PY_SSIZE_T_MAX) { |
| 1504 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1505 | return NULL; |
| 1506 | } |
| 1507 | |
| 1508 | return PyUnicode_FromStringAndSize(u, size); |
| 1509 | } |
| 1510 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1511 | static PyObject* |
| 1512 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1513 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1514 | PyObject *res; |
| 1515 | unsigned char max = 127; |
| 1516 | Py_ssize_t i; |
| 1517 | for (i = 0; i < size; i++) { |
| 1518 | if (u[i] & 0x80) { |
| 1519 | max = 255; |
| 1520 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1521 | } |
| 1522 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1523 | res = PyUnicode_New(size, max); |
| 1524 | if (!res) |
| 1525 | return NULL; |
| 1526 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
| 1527 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1530 | static PyObject* |
| 1531 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1532 | { |
| 1533 | PyObject *res; |
| 1534 | Py_UCS2 max = 0; |
| 1535 | Py_ssize_t i; |
| 1536 | for (i = 0; i < size; i++) |
| 1537 | if (u[i] > max) |
| 1538 | max = u[i]; |
| 1539 | res = PyUnicode_New(size, max); |
| 1540 | if (!res) |
| 1541 | return NULL; |
| 1542 | if (max >= 256) |
| 1543 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1544 | else |
| 1545 | for (i = 0; i < size; i++) |
| 1546 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
| 1547 | return res; |
| 1548 | } |
| 1549 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1550 | static PyObject* |
| 1551 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1552 | { |
| 1553 | PyObject *res; |
| 1554 | Py_UCS4 max = 0; |
| 1555 | Py_ssize_t i; |
| 1556 | for (i = 0; i < size; i++) |
| 1557 | if (u[i] > max) |
| 1558 | max = u[i]; |
| 1559 | res = PyUnicode_New(size, max); |
| 1560 | if (!res) |
| 1561 | return NULL; |
| 1562 | if (max >= 0x10000) |
| 1563 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1564 | else { |
| 1565 | int kind = PyUnicode_KIND(res); |
| 1566 | void *data = PyUnicode_DATA(res); |
| 1567 | for (i = 0; i < size; i++) |
| 1568 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1569 | } |
| 1570 | return res; |
| 1571 | } |
| 1572 | |
| 1573 | PyObject* |
| 1574 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1575 | { |
| 1576 | switch(kind) { |
| 1577 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1578 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1579 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1580 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1581 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1582 | return _PyUnicode_FromUCS4(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1583 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1584 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1585 | return NULL; |
| 1586 | } |
| 1587 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1588 | PyObject* |
| 1589 | PyUnicode_Copy(PyObject *unicode) |
| 1590 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1591 | Py_ssize_t size; |
| 1592 | PyObject *copy; |
| 1593 | void *data; |
| 1594 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1595 | if (!PyUnicode_Check(unicode)) { |
| 1596 | PyErr_BadInternalCall(); |
| 1597 | return NULL; |
| 1598 | } |
| 1599 | if (PyUnicode_READY(unicode)) |
| 1600 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1601 | |
| 1602 | size = PyUnicode_GET_LENGTH(unicode); |
| 1603 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1604 | if (!copy) |
| 1605 | return NULL; |
| 1606 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1607 | |
| 1608 | data = PyUnicode_DATA(unicode); |
| 1609 | switch (PyUnicode_KIND(unicode)) |
| 1610 | { |
| 1611 | case PyUnicode_1BYTE_KIND: |
| 1612 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1613 | break; |
| 1614 | case PyUnicode_2BYTE_KIND: |
| 1615 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1616 | break; |
| 1617 | case PyUnicode_4BYTE_KIND: |
| 1618 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1619 | break; |
| 1620 | default: |
| 1621 | assert(0); |
| 1622 | break; |
| 1623 | } |
| 1624 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1625 | } |
| 1626 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1627 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1628 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1629 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1630 | |
| 1631 | void* |
| 1632 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1633 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1634 | Py_ssize_t len; |
| 1635 | void *result; |
| 1636 | unsigned int skind; |
| 1637 | |
| 1638 | if (PyUnicode_READY(s)) |
| 1639 | return NULL; |
| 1640 | |
| 1641 | len = PyUnicode_GET_LENGTH(s); |
| 1642 | skind = PyUnicode_KIND(s); |
| 1643 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1644 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1645 | return NULL; |
| 1646 | } |
| 1647 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1648 | case PyUnicode_2BYTE_KIND: |
| 1649 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1650 | if (!result) |
| 1651 | return PyErr_NoMemory(); |
| 1652 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1653 | _PyUnicode_CONVERT_BYTES( |
| 1654 | Py_UCS1, Py_UCS2, |
| 1655 | PyUnicode_1BYTE_DATA(s), |
| 1656 | PyUnicode_1BYTE_DATA(s) + len, |
| 1657 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1658 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1659 | case PyUnicode_4BYTE_KIND: |
| 1660 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1661 | if (!result) |
| 1662 | return PyErr_NoMemory(); |
| 1663 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1664 | _PyUnicode_CONVERT_BYTES( |
| 1665 | Py_UCS2, Py_UCS4, |
| 1666 | PyUnicode_2BYTE_DATA(s), |
| 1667 | PyUnicode_2BYTE_DATA(s) + len, |
| 1668 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1669 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1670 | else { |
| 1671 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1672 | _PyUnicode_CONVERT_BYTES( |
| 1673 | Py_UCS1, Py_UCS4, |
| 1674 | PyUnicode_1BYTE_DATA(s), |
| 1675 | PyUnicode_1BYTE_DATA(s) + len, |
| 1676 | result); |
| 1677 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1678 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1679 | default: |
| 1680 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1681 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1682 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1683 | return NULL; |
| 1684 | } |
| 1685 | |
| 1686 | static Py_UCS4* |
| 1687 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1688 | int copy_null) |
| 1689 | { |
| 1690 | int kind; |
| 1691 | void *data; |
| 1692 | Py_ssize_t len, targetlen; |
| 1693 | if (PyUnicode_READY(string) == -1) |
| 1694 | return NULL; |
| 1695 | kind = PyUnicode_KIND(string); |
| 1696 | data = PyUnicode_DATA(string); |
| 1697 | len = PyUnicode_GET_LENGTH(string); |
| 1698 | targetlen = len; |
| 1699 | if (copy_null) |
| 1700 | targetlen++; |
| 1701 | if (!target) { |
| 1702 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1703 | PyErr_NoMemory(); |
| 1704 | return NULL; |
| 1705 | } |
| 1706 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1707 | if (!target) { |
| 1708 | PyErr_NoMemory(); |
| 1709 | return NULL; |
| 1710 | } |
| 1711 | } |
| 1712 | else { |
| 1713 | if (targetsize < targetlen) { |
| 1714 | PyErr_Format(PyExc_SystemError, |
| 1715 | "string is longer than the buffer"); |
| 1716 | if (copy_null && 0 < targetsize) |
| 1717 | target[0] = 0; |
| 1718 | return NULL; |
| 1719 | } |
| 1720 | } |
| 1721 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1722 | Py_ssize_t i; |
| 1723 | for (i = 0; i < len; i++) |
| 1724 | target[i] = PyUnicode_READ(kind, data, i); |
| 1725 | } |
| 1726 | else |
| 1727 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1728 | if (copy_null) |
| 1729 | target[len] = 0; |
| 1730 | return target; |
| 1731 | } |
| 1732 | |
| 1733 | Py_UCS4* |
| 1734 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1735 | int copy_null) |
| 1736 | { |
| 1737 | if (target == NULL || targetsize < 1) { |
| 1738 | PyErr_BadInternalCall(); |
| 1739 | return NULL; |
| 1740 | } |
| 1741 | return as_ucs4(string, target, targetsize, copy_null); |
| 1742 | } |
| 1743 | |
| 1744 | Py_UCS4* |
| 1745 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1746 | { |
| 1747 | return as_ucs4(string, NULL, 0, 1); |
| 1748 | } |
| 1749 | |
| 1750 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1751 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1752 | PyObject * |
| 1753 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1754 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1755 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1756 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1757 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1758 | PyErr_BadInternalCall(); |
| 1759 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1762 | if (size == -1) { |
| 1763 | size = wcslen(w); |
| 1764 | } |
| 1765 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1766 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1769 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1770 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1771 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1772 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1773 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1774 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1775 | *fmt++ = '%'; |
| 1776 | if (width) { |
| 1777 | if (zeropad) |
| 1778 | *fmt++ = '0'; |
| 1779 | fmt += sprintf(fmt, "%d", width); |
| 1780 | } |
| 1781 | if (precision) |
| 1782 | fmt += sprintf(fmt, ".%d", precision); |
| 1783 | if (longflag) |
| 1784 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1785 | else if (longlongflag) { |
| 1786 | /* longlongflag should only ever be nonzero on machines with |
| 1787 | HAVE_LONG_LONG defined */ |
| 1788 | #ifdef HAVE_LONG_LONG |
| 1789 | char *f = PY_FORMAT_LONG_LONG; |
| 1790 | while (*f) |
| 1791 | *fmt++ = *f++; |
| 1792 | #else |
| 1793 | /* we shouldn't ever get here */ |
| 1794 | assert(0); |
| 1795 | *fmt++ = 'l'; |
| 1796 | #endif |
| 1797 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1798 | else if (size_tflag) { |
| 1799 | char *f = PY_FORMAT_SIZE_T; |
| 1800 | while (*f) |
| 1801 | *fmt++ = *f++; |
| 1802 | } |
| 1803 | *fmt++ = c; |
| 1804 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1807 | /* helper for PyUnicode_FromFormatV() */ |
| 1808 | |
| 1809 | static const char* |
| 1810 | parse_format_flags(const char *f, |
| 1811 | int *p_width, int *p_precision, |
| 1812 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 1813 | { |
| 1814 | int width, precision, longflag, longlongflag, size_tflag; |
| 1815 | |
| 1816 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 1817 | f++; |
| 1818 | width = 0; |
| 1819 | while (Py_ISDIGIT((unsigned)*f)) |
| 1820 | width = (width*10) + *f++ - '0'; |
| 1821 | precision = 0; |
| 1822 | if (*f == '.') { |
| 1823 | f++; |
| 1824 | while (Py_ISDIGIT((unsigned)*f)) |
| 1825 | precision = (precision*10) + *f++ - '0'; |
| 1826 | if (*f == '%') { |
| 1827 | /* "%.3%s" => f points to "3" */ |
| 1828 | f--; |
| 1829 | } |
| 1830 | } |
| 1831 | if (*f == '\0') { |
| 1832 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 1833 | f--; |
| 1834 | } |
| 1835 | if (p_width != NULL) |
| 1836 | *p_width = width; |
| 1837 | if (p_precision != NULL) |
| 1838 | *p_precision = precision; |
| 1839 | |
| 1840 | /* Handle %ld, %lu, %lld and %llu. */ |
| 1841 | longflag = 0; |
| 1842 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 1843 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1844 | |
| 1845 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1846 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1847 | longflag = 1; |
| 1848 | ++f; |
| 1849 | } |
| 1850 | #ifdef HAVE_LONG_LONG |
| 1851 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1852 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1853 | longlongflag = 1; |
| 1854 | f += 2; |
| 1855 | } |
| 1856 | #endif |
| 1857 | } |
| 1858 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1859 | 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] | 1860 | size_tflag = 1; |
| 1861 | ++f; |
| 1862 | } |
| 1863 | if (p_longflag != NULL) |
| 1864 | *p_longflag = longflag; |
| 1865 | if (p_longlongflag != NULL) |
| 1866 | *p_longlongflag = longlongflag; |
| 1867 | if (p_size_tflag != NULL) |
| 1868 | *p_size_tflag = size_tflag; |
| 1869 | return f; |
| 1870 | } |
| 1871 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1872 | /* maximum number of characters required for output of %ld. 21 characters |
| 1873 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 1874 | #define MAX_LONG_CHARS 21 |
| 1875 | /* maximum number of characters required for output of %lld. |
| 1876 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 1877 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 1878 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 1879 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1880 | PyObject * |
| 1881 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 1882 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1883 | va_list count; |
| 1884 | Py_ssize_t callcount = 0; |
| 1885 | PyObject **callresults = NULL; |
| 1886 | PyObject **callresult = NULL; |
| 1887 | Py_ssize_t n = 0; |
| 1888 | int width = 0; |
| 1889 | int precision = 0; |
| 1890 | int zeropad; |
| 1891 | const char* f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1892 | PyUnicodeObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1893 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1894 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1895 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 1896 | Py_UCS4 argmaxchar; |
| 1897 | Py_ssize_t numbersize = 0; |
| 1898 | char *numberresults = NULL; |
| 1899 | char *numberresult = NULL; |
| 1900 | Py_ssize_t i; |
| 1901 | int kind; |
| 1902 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1903 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1904 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1905 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 1906 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 1907 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1908 | * result in an array) |
| 1909 | * also esimate a upper bound for all the number formats in the string, |
| 1910 | * numbers will be formated in step 3 and be keept in a '\0'-separated |
| 1911 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1912 | for (f = format; *f; f++) { |
| 1913 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1914 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1915 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 1916 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 1917 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 1918 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1919 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1920 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1921 | #ifdef HAVE_LONG_LONG |
| 1922 | if (longlongflag) { |
| 1923 | if (width < MAX_LONG_LONG_CHARS) |
| 1924 | width = MAX_LONG_LONG_CHARS; |
| 1925 | } |
| 1926 | else |
| 1927 | #endif |
| 1928 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 1929 | including sign. Decimal takes the most space. This |
| 1930 | isn't enough for octal. If a width is specified we |
| 1931 | need more (which we allocate later). */ |
| 1932 | if (width < MAX_LONG_CHARS) |
| 1933 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1934 | |
| 1935 | /* account for the size + '\0' to separate numbers |
| 1936 | inside of the numberresults buffer */ |
| 1937 | numbersize += (width + 1); |
| 1938 | } |
| 1939 | } |
| 1940 | else if ((unsigned char)*f > 127) { |
| 1941 | PyErr_Format(PyExc_ValueError, |
| 1942 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 1943 | "string, got a non-ASCII byte: 0x%02x", |
| 1944 | (unsigned char)*f); |
| 1945 | return NULL; |
| 1946 | } |
| 1947 | } |
| 1948 | /* step 2: allocate memory for the results of |
| 1949 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 1950 | if (callcount) { |
| 1951 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 1952 | if (!callresults) { |
| 1953 | PyErr_NoMemory(); |
| 1954 | return NULL; |
| 1955 | } |
| 1956 | callresult = callresults; |
| 1957 | } |
| 1958 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 1959 | if (numbersize) { |
| 1960 | numberresults = PyObject_Malloc(numbersize); |
| 1961 | if (!numberresults) { |
| 1962 | PyErr_NoMemory(); |
| 1963 | goto fail; |
| 1964 | } |
| 1965 | numberresult = numberresults; |
| 1966 | } |
| 1967 | |
| 1968 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 1969 | for (f = format; *f; f++) { |
| 1970 | if (*f == '%') { |
| 1971 | const char* p; |
| 1972 | int longflag; |
| 1973 | int longlongflag; |
| 1974 | int size_tflag; |
| 1975 | int numprinted; |
| 1976 | |
| 1977 | p = f; |
| 1978 | zeropad = (f[1] == '0'); |
| 1979 | f = parse_format_flags(f, &width, &precision, |
| 1980 | &longflag, &longlongflag, &size_tflag); |
| 1981 | switch (*f) { |
| 1982 | case 'c': |
| 1983 | { |
| 1984 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1985 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1986 | n++; |
| 1987 | break; |
| 1988 | } |
| 1989 | case '%': |
| 1990 | n++; |
| 1991 | break; |
| 1992 | case 'i': |
| 1993 | case 'd': |
| 1994 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1995 | width, precision, *f); |
| 1996 | if (longflag) |
| 1997 | numprinted = sprintf(numberresult, fmt, |
| 1998 | va_arg(count, long)); |
| 1999 | #ifdef HAVE_LONG_LONG |
| 2000 | else if (longlongflag) |
| 2001 | numprinted = sprintf(numberresult, fmt, |
| 2002 | va_arg(count, PY_LONG_LONG)); |
| 2003 | #endif |
| 2004 | else if (size_tflag) |
| 2005 | numprinted = sprintf(numberresult, fmt, |
| 2006 | va_arg(count, Py_ssize_t)); |
| 2007 | else |
| 2008 | numprinted = sprintf(numberresult, fmt, |
| 2009 | va_arg(count, int)); |
| 2010 | n += numprinted; |
| 2011 | /* advance by +1 to skip over the '\0' */ |
| 2012 | numberresult += (numprinted + 1); |
| 2013 | assert(*(numberresult - 1) == '\0'); |
| 2014 | assert(*(numberresult - 2) != '\0'); |
| 2015 | assert(numprinted >= 0); |
| 2016 | assert(numberresult <= numberresults + numbersize); |
| 2017 | break; |
| 2018 | case 'u': |
| 2019 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2020 | width, precision, 'u'); |
| 2021 | if (longflag) |
| 2022 | numprinted = sprintf(numberresult, fmt, |
| 2023 | va_arg(count, unsigned long)); |
| 2024 | #ifdef HAVE_LONG_LONG |
| 2025 | else if (longlongflag) |
| 2026 | numprinted = sprintf(numberresult, fmt, |
| 2027 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2028 | #endif |
| 2029 | else if (size_tflag) |
| 2030 | numprinted = sprintf(numberresult, fmt, |
| 2031 | va_arg(count, size_t)); |
| 2032 | else |
| 2033 | numprinted = sprintf(numberresult, fmt, |
| 2034 | va_arg(count, unsigned int)); |
| 2035 | n += numprinted; |
| 2036 | numberresult += (numprinted + 1); |
| 2037 | assert(*(numberresult - 1) == '\0'); |
| 2038 | assert(*(numberresult - 2) != '\0'); |
| 2039 | assert(numprinted >= 0); |
| 2040 | assert(numberresult <= numberresults + numbersize); |
| 2041 | break; |
| 2042 | case 'x': |
| 2043 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2044 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2045 | n += numprinted; |
| 2046 | numberresult += (numprinted + 1); |
| 2047 | assert(*(numberresult - 1) == '\0'); |
| 2048 | assert(*(numberresult - 2) != '\0'); |
| 2049 | assert(numprinted >= 0); |
| 2050 | assert(numberresult <= numberresults + numbersize); |
| 2051 | break; |
| 2052 | case 'p': |
| 2053 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2054 | /* %p is ill-defined: ensure leading 0x. */ |
| 2055 | if (numberresult[1] == 'X') |
| 2056 | numberresult[1] = 'x'; |
| 2057 | else if (numberresult[1] != 'x') { |
| 2058 | memmove(numberresult + 2, numberresult, |
| 2059 | strlen(numberresult) + 1); |
| 2060 | numberresult[0] = '0'; |
| 2061 | numberresult[1] = 'x'; |
| 2062 | numprinted += 2; |
| 2063 | } |
| 2064 | n += numprinted; |
| 2065 | numberresult += (numprinted + 1); |
| 2066 | assert(*(numberresult - 1) == '\0'); |
| 2067 | assert(*(numberresult - 2) != '\0'); |
| 2068 | assert(numprinted >= 0); |
| 2069 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2070 | break; |
| 2071 | case 's': |
| 2072 | { |
| 2073 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2074 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2075 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2076 | if (!str) |
| 2077 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2078 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2079 | unicode objects, there is no need to call ready on them */ |
| 2080 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2081 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2082 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2083 | /* Remember the str and switch to the next slot */ |
| 2084 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2085 | break; |
| 2086 | } |
| 2087 | case 'U': |
| 2088 | { |
| 2089 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2090 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2091 | if (PyUnicode_READY(obj) == -1) |
| 2092 | goto fail; |
| 2093 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2094 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2095 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2096 | break; |
| 2097 | } |
| 2098 | case 'V': |
| 2099 | { |
| 2100 | PyObject *obj = va_arg(count, PyObject *); |
| 2101 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2102 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2103 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2104 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2105 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2106 | if (PyUnicode_READY(obj) == -1) |
| 2107 | goto fail; |
| 2108 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2109 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2110 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2111 | *callresult++ = NULL; |
| 2112 | } |
| 2113 | else { |
| 2114 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2115 | if (!str_obj) |
| 2116 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2117 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2118 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2119 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2120 | *callresult++ = str_obj; |
| 2121 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2122 | break; |
| 2123 | } |
| 2124 | case 'S': |
| 2125 | { |
| 2126 | PyObject *obj = va_arg(count, PyObject *); |
| 2127 | PyObject *str; |
| 2128 | assert(obj); |
| 2129 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2130 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2131 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2132 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2133 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2134 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2135 | /* Remember the str and switch to the next slot */ |
| 2136 | *callresult++ = str; |
| 2137 | break; |
| 2138 | } |
| 2139 | case 'R': |
| 2140 | { |
| 2141 | PyObject *obj = va_arg(count, PyObject *); |
| 2142 | PyObject *repr; |
| 2143 | assert(obj); |
| 2144 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2145 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2146 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2147 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2148 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2149 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2150 | /* Remember the repr and switch to the next slot */ |
| 2151 | *callresult++ = repr; |
| 2152 | break; |
| 2153 | } |
| 2154 | case 'A': |
| 2155 | { |
| 2156 | PyObject *obj = va_arg(count, PyObject *); |
| 2157 | PyObject *ascii; |
| 2158 | assert(obj); |
| 2159 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2160 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2161 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2162 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2163 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2164 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2165 | /* Remember the repr and switch to the next slot */ |
| 2166 | *callresult++ = ascii; |
| 2167 | break; |
| 2168 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2169 | default: |
| 2170 | /* if we stumble upon an unknown |
| 2171 | formatting code, copy the rest of |
| 2172 | the format string to the output |
| 2173 | string. (we cannot just skip the |
| 2174 | code, since there's no way to know |
| 2175 | what's in the argument list) */ |
| 2176 | n += strlen(p); |
| 2177 | goto expand; |
| 2178 | } |
| 2179 | } else |
| 2180 | n++; |
| 2181 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2182 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2183 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2184 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2185 | we don't have to resize the string. |
| 2186 | There can be no errors beyond this point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2187 | string = (PyUnicodeObject *)PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2188 | if (!string) |
| 2189 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2190 | kind = PyUnicode_KIND(string); |
| 2191 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2192 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2193 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2194 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2195 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2196 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2197 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2198 | |
| 2199 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2200 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2201 | /* checking for == because the last argument could be a empty |
| 2202 | string, which causes i to point to end, the assert at the end of |
| 2203 | the loop */ |
| 2204 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2205 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2206 | switch (*f) { |
| 2207 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2208 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2209 | const int ordinal = va_arg(vargs, int); |
| 2210 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2211 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2212 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2213 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2214 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2215 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2216 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2217 | case 'p': |
| 2218 | /* unused, since we already have the result */ |
| 2219 | if (*f == 'p') |
| 2220 | (void) va_arg(vargs, void *); |
| 2221 | else |
| 2222 | (void) va_arg(vargs, int); |
| 2223 | /* extract the result from numberresults and append. */ |
| 2224 | for (; *numberresult; ++i, ++numberresult) |
| 2225 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2226 | /* skip over the separating '\0' */ |
| 2227 | assert(*numberresult == '\0'); |
| 2228 | numberresult++; |
| 2229 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2230 | break; |
| 2231 | case 's': |
| 2232 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2233 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2234 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2235 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2236 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2237 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2238 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2239 | *callresult, 0, |
| 2240 | size) < 0) |
| 2241 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2242 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2243 | /* We're done with the unicode()/repr() => forget it */ |
| 2244 | Py_DECREF(*callresult); |
| 2245 | /* switch to next unicode()/repr() result */ |
| 2246 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2247 | break; |
| 2248 | } |
| 2249 | case 'U': |
| 2250 | { |
| 2251 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2252 | Py_ssize_t size; |
| 2253 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2254 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2255 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2256 | obj, 0, |
| 2257 | size) < 0) |
| 2258 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2259 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2260 | break; |
| 2261 | } |
| 2262 | case 'V': |
| 2263 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2264 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2265 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2266 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2267 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2268 | size = PyUnicode_GET_LENGTH(obj); |
| 2269 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2270 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2271 | obj, 0, |
| 2272 | size) < 0) |
| 2273 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2274 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2275 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2276 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2277 | assert(PyUnicode_KIND(*callresult) <= |
| 2278 | PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2279 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2280 | *callresult, |
| 2281 | 0, size) < 0) |
| 2282 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2283 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2284 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2285 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2286 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2287 | break; |
| 2288 | } |
| 2289 | case 'S': |
| 2290 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2291 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2292 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2293 | /* unused, since we already have the result */ |
| 2294 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2295 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2296 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2297 | *callresult, 0, |
| 2298 | PyUnicode_GET_LENGTH(*callresult)) < 0) |
| 2299 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2300 | i += PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2301 | /* We're done with the unicode()/repr() => forget it */ |
| 2302 | Py_DECREF(*callresult); |
| 2303 | /* switch to next unicode()/repr() result */ |
| 2304 | ++callresult; |
| 2305 | break; |
| 2306 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2307 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2308 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2309 | break; |
| 2310 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2311 | for (; *p; ++p, ++i) |
| 2312 | PyUnicode_WRITE(kind, data, i, *p); |
| 2313 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2314 | goto end; |
| 2315 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2316 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2317 | else { |
| 2318 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2319 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2320 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2321 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2322 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2323 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2324 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2325 | if (callresults) |
| 2326 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2327 | if (numberresults) |
| 2328 | PyObject_Free(numberresults); |
| 2329 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2330 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2331 | if (callresults) { |
| 2332 | PyObject **callresult2 = callresults; |
| 2333 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2334 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2335 | ++callresult2; |
| 2336 | } |
| 2337 | PyObject_Free(callresults); |
| 2338 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2339 | if (numberresults) |
| 2340 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2341 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2344 | PyObject * |
| 2345 | PyUnicode_FromFormat(const char *format, ...) |
| 2346 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2347 | PyObject* ret; |
| 2348 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2349 | |
| 2350 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2351 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2352 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2353 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2354 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2355 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2356 | va_end(vargs); |
| 2357 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2360 | #ifdef HAVE_WCHAR_H |
| 2361 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2362 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2363 | convert a Unicode object to a wide character string. |
| 2364 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2365 | - 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] | 2366 | character) required to convert the unicode object. Ignore size argument. |
| 2367 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2368 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2369 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2370 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2371 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2372 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 2373 | wchar_t *w, |
| 2374 | Py_ssize_t size) |
| 2375 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2376 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2377 | const wchar_t *wstr; |
| 2378 | |
| 2379 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 2380 | if (wstr == NULL) |
| 2381 | return -1; |
| 2382 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2383 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2384 | if (size > res) |
| 2385 | size = res + 1; |
| 2386 | else |
| 2387 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2388 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2389 | return res; |
| 2390 | } |
| 2391 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2392 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2396 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2397 | wchar_t *w, |
| 2398 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2399 | { |
| 2400 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2401 | PyErr_BadInternalCall(); |
| 2402 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2403 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2404 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2407 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2408 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2409 | Py_ssize_t *size) |
| 2410 | { |
| 2411 | wchar_t* buffer; |
| 2412 | Py_ssize_t buflen; |
| 2413 | |
| 2414 | if (unicode == NULL) { |
| 2415 | PyErr_BadInternalCall(); |
| 2416 | return NULL; |
| 2417 | } |
| 2418 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2419 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2420 | if (buflen == -1) |
| 2421 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2422 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2423 | PyErr_NoMemory(); |
| 2424 | return NULL; |
| 2425 | } |
| 2426 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2427 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2428 | if (buffer == NULL) { |
| 2429 | PyErr_NoMemory(); |
| 2430 | return NULL; |
| 2431 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2432 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2433 | if (buflen == -1) |
| 2434 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2435 | if (size != NULL) |
| 2436 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2437 | return buffer; |
| 2438 | } |
| 2439 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2440 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2441 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2442 | PyObject * |
| 2443 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2444 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2445 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2446 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2447 | PyErr_SetString(PyExc_ValueError, |
| 2448 | "chr() arg not in range(0x110000)"); |
| 2449 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2450 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2451 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2452 | if (ordinal < 256) |
| 2453 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2454 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2455 | v = PyUnicode_New(1, ordinal); |
| 2456 | if (v == NULL) |
| 2457 | return NULL; |
| 2458 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
| 2459 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2462 | PyObject * |
| 2463 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2464 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2465 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2466 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2467 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2468 | if (PyUnicode_READY(obj)) |
| 2469 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2470 | Py_INCREF(obj); |
| 2471 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2472 | } |
| 2473 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2474 | /* For a Unicode subtype that's not a Unicode object, |
| 2475 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2476 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2477 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2478 | PyErr_Format(PyExc_TypeError, |
| 2479 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2480 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2481 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2484 | PyObject * |
| 2485 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2486 | const char *encoding, |
| 2487 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2488 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2489 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2490 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2491 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2492 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2493 | PyErr_BadInternalCall(); |
| 2494 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2495 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2496 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2497 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2498 | if (PyBytes_Check(obj)) { |
| 2499 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2500 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2501 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2502 | } |
| 2503 | else { |
| 2504 | v = PyUnicode_Decode( |
| 2505 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2506 | encoding, errors); |
| 2507 | } |
| 2508 | return v; |
| 2509 | } |
| 2510 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2511 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2512 | PyErr_SetString(PyExc_TypeError, |
| 2513 | "decoding str is not supported"); |
| 2514 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2515 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2516 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2517 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2518 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2519 | PyErr_Format(PyExc_TypeError, |
| 2520 | "coercing to str: need bytes, bytearray " |
| 2521 | "or buffer-like object, %.80s found", |
| 2522 | Py_TYPE(obj)->tp_name); |
| 2523 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2524 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2525 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2526 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2527 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2528 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2529 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2530 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2531 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2532 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2533 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2534 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2537 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2538 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2539 | 1 on success. */ |
| 2540 | static int |
| 2541 | normalize_encoding(const char *encoding, |
| 2542 | char *lower, |
| 2543 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2544 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2545 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2546 | char *l; |
| 2547 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2548 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2549 | e = encoding; |
| 2550 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2551 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2552 | while (*e) { |
| 2553 | if (l == l_end) |
| 2554 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2555 | if (Py_ISUPPER(*e)) { |
| 2556 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2557 | } |
| 2558 | else if (*e == '_') { |
| 2559 | *l++ = '-'; |
| 2560 | e++; |
| 2561 | } |
| 2562 | else { |
| 2563 | *l++ = *e++; |
| 2564 | } |
| 2565 | } |
| 2566 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2567 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2570 | PyObject * |
| 2571 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2572 | Py_ssize_t size, |
| 2573 | const char *encoding, |
| 2574 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2575 | { |
| 2576 | PyObject *buffer = NULL, *unicode; |
| 2577 | Py_buffer info; |
| 2578 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2579 | |
| 2580 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2581 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2582 | |
| 2583 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2584 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2585 | if ((strcmp(lower, "utf-8") == 0) || |
| 2586 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2587 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2588 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2589 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2590 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2591 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2592 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2593 | else if (strcmp(lower, "mbcs") == 0) |
| 2594 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2595 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2596 | else if (strcmp(lower, "ascii") == 0) |
| 2597 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2598 | else if (strcmp(lower, "utf-16") == 0) |
| 2599 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2600 | else if (strcmp(lower, "utf-32") == 0) |
| 2601 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2602 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2603 | |
| 2604 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2605 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2606 | 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] | 2607 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2608 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2609 | if (buffer == NULL) |
| 2610 | goto onError; |
| 2611 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2612 | if (unicode == NULL) |
| 2613 | goto onError; |
| 2614 | if (!PyUnicode_Check(unicode)) { |
| 2615 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2616 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2617 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2618 | Py_DECREF(unicode); |
| 2619 | goto onError; |
| 2620 | } |
| 2621 | Py_DECREF(buffer); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2622 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2623 | Py_DECREF(unicode); |
| 2624 | return NULL; |
| 2625 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2626 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2627 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2628 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2629 | Py_XDECREF(buffer); |
| 2630 | return NULL; |
| 2631 | } |
| 2632 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2633 | PyObject * |
| 2634 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2635 | const char *encoding, |
| 2636 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2637 | { |
| 2638 | PyObject *v; |
| 2639 | |
| 2640 | if (!PyUnicode_Check(unicode)) { |
| 2641 | PyErr_BadArgument(); |
| 2642 | goto onError; |
| 2643 | } |
| 2644 | |
| 2645 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2646 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2647 | |
| 2648 | /* Decode via the codec registry */ |
| 2649 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2650 | if (v == NULL) |
| 2651 | goto onError; |
| 2652 | return v; |
| 2653 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2654 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2655 | return NULL; |
| 2656 | } |
| 2657 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2658 | PyObject * |
| 2659 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2660 | const char *encoding, |
| 2661 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2662 | { |
| 2663 | PyObject *v; |
| 2664 | |
| 2665 | if (!PyUnicode_Check(unicode)) { |
| 2666 | PyErr_BadArgument(); |
| 2667 | goto onError; |
| 2668 | } |
| 2669 | |
| 2670 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2671 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2672 | |
| 2673 | /* Decode via the codec registry */ |
| 2674 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2675 | if (v == NULL) |
| 2676 | goto onError; |
| 2677 | if (!PyUnicode_Check(v)) { |
| 2678 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2679 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2680 | Py_TYPE(v)->tp_name); |
| 2681 | Py_DECREF(v); |
| 2682 | goto onError; |
| 2683 | } |
| 2684 | return v; |
| 2685 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2686 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2687 | return NULL; |
| 2688 | } |
| 2689 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2690 | PyObject * |
| 2691 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2692 | Py_ssize_t size, |
| 2693 | const char *encoding, |
| 2694 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2695 | { |
| 2696 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2697 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2698 | unicode = PyUnicode_FromUnicode(s, size); |
| 2699 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2700 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2701 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2702 | Py_DECREF(unicode); |
| 2703 | return v; |
| 2704 | } |
| 2705 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2706 | PyObject * |
| 2707 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2708 | const char *encoding, |
| 2709 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2710 | { |
| 2711 | PyObject *v; |
| 2712 | |
| 2713 | if (!PyUnicode_Check(unicode)) { |
| 2714 | PyErr_BadArgument(); |
| 2715 | goto onError; |
| 2716 | } |
| 2717 | |
| 2718 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2719 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2720 | |
| 2721 | /* Encode via the codec registry */ |
| 2722 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2723 | if (v == NULL) |
| 2724 | goto onError; |
| 2725 | return v; |
| 2726 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2727 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2728 | return NULL; |
| 2729 | } |
| 2730 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2731 | PyObject * |
| 2732 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2733 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2734 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2735 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2736 | PyUnicode_GET_SIZE(unicode), |
| 2737 | NULL); |
| 2738 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2739 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2740 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2741 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2742 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2743 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2744 | the Python codec requires to encode at least its own filename. Use the C |
| 2745 | version of the locale codec until the codec registry is initialized and |
| 2746 | the Python codec is loaded. |
| 2747 | |
| 2748 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2749 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2750 | subinterpreters. */ |
| 2751 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2752 | return PyUnicode_AsEncodedString(unicode, |
| 2753 | Py_FileSystemDefaultEncoding, |
| 2754 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2755 | } |
| 2756 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2757 | /* locale encoding with surrogateescape */ |
| 2758 | wchar_t *wchar; |
| 2759 | char *bytes; |
| 2760 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2761 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2762 | |
| 2763 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2764 | if (wchar == NULL) |
| 2765 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2766 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2767 | if (bytes == NULL) { |
| 2768 | if (error_pos != (size_t)-1) { |
| 2769 | char *errmsg = strerror(errno); |
| 2770 | PyObject *exc = NULL; |
| 2771 | if (errmsg == NULL) |
| 2772 | errmsg = "Py_wchar2char() failed"; |
| 2773 | raise_encode_exception(&exc, |
| 2774 | "filesystemencoding", |
| 2775 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2776 | error_pos, error_pos+1, |
| 2777 | errmsg); |
| 2778 | Py_XDECREF(exc); |
| 2779 | } |
| 2780 | else |
| 2781 | PyErr_NoMemory(); |
| 2782 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2783 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2784 | } |
| 2785 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2786 | |
| 2787 | bytes_obj = PyBytes_FromString(bytes); |
| 2788 | PyMem_Free(bytes); |
| 2789 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2790 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2791 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2794 | PyObject * |
| 2795 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2796 | const char *encoding, |
| 2797 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2798 | { |
| 2799 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2800 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2801 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2802 | if (!PyUnicode_Check(unicode)) { |
| 2803 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2804 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2805 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2806 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2807 | if (encoding == NULL) { |
| 2808 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2809 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2810 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2811 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2812 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2813 | |
| 2814 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2815 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2816 | if ((strcmp(lower, "utf-8") == 0) || |
| 2817 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2818 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2819 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2820 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2821 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2822 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2823 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2824 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2825 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2826 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2827 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2828 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2829 | else if (strcmp(lower, "mbcs") == 0) |
| 2830 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2831 | PyUnicode_GET_SIZE(unicode), |
| 2832 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2833 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2834 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2835 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2836 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2837 | |
| 2838 | /* Encode via the codec registry */ |
| 2839 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2840 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2841 | return NULL; |
| 2842 | |
| 2843 | /* The normal path */ |
| 2844 | if (PyBytes_Check(v)) |
| 2845 | return v; |
| 2846 | |
| 2847 | /* 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] | 2848 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2849 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2850 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2851 | |
| 2852 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 2853 | "encoder %s returned bytearray instead of bytes", |
| 2854 | encoding); |
| 2855 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2856 | Py_DECREF(v); |
| 2857 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2858 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2859 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2860 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 2861 | Py_DECREF(v); |
| 2862 | return b; |
| 2863 | } |
| 2864 | |
| 2865 | PyErr_Format(PyExc_TypeError, |
| 2866 | "encoder did not return a bytes object (type=%.400s)", |
| 2867 | Py_TYPE(v)->tp_name); |
| 2868 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2869 | return NULL; |
| 2870 | } |
| 2871 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2872 | PyObject * |
| 2873 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2874 | const char *encoding, |
| 2875 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2876 | { |
| 2877 | PyObject *v; |
| 2878 | |
| 2879 | if (!PyUnicode_Check(unicode)) { |
| 2880 | PyErr_BadArgument(); |
| 2881 | goto onError; |
| 2882 | } |
| 2883 | |
| 2884 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2885 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2886 | |
| 2887 | /* Encode via the codec registry */ |
| 2888 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2889 | if (v == NULL) |
| 2890 | goto onError; |
| 2891 | if (!PyUnicode_Check(v)) { |
| 2892 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2893 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2894 | Py_TYPE(v)->tp_name); |
| 2895 | Py_DECREF(v); |
| 2896 | goto onError; |
| 2897 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2898 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2899 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2900 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2901 | return NULL; |
| 2902 | } |
| 2903 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2904 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2905 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2906 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2907 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 2908 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2909 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2910 | PyObject* |
| 2911 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 2912 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2913 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2914 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 2915 | #elif defined(__APPLE__) |
| 2916 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 2917 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2918 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2919 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2920 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2921 | the Python codec requires to encode at least its own filename. Use the C |
| 2922 | version of the locale codec until the codec registry is initialized and |
| 2923 | the Python codec is loaded. |
| 2924 | |
| 2925 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2926 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2927 | subinterpreters. */ |
| 2928 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2929 | return PyUnicode_Decode(s, size, |
| 2930 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 2931 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2932 | } |
| 2933 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2934 | /* locale encoding with surrogateescape */ |
| 2935 | wchar_t *wchar; |
| 2936 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2937 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2938 | |
| 2939 | if (s[size] != '\0' || size != strlen(s)) { |
| 2940 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2941 | return NULL; |
| 2942 | } |
| 2943 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2944 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2945 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 2946 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2947 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2948 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2949 | PyMem_Free(wchar); |
| 2950 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2951 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2952 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2955 | |
| 2956 | int |
| 2957 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 2958 | { |
| 2959 | PyObject *output = NULL; |
| 2960 | Py_ssize_t size; |
| 2961 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2962 | if (arg == NULL) { |
| 2963 | Py_DECREF(*(PyObject**)addr); |
| 2964 | return 1; |
| 2965 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2966 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2967 | output = arg; |
| 2968 | Py_INCREF(output); |
| 2969 | } |
| 2970 | else { |
| 2971 | arg = PyUnicode_FromObject(arg); |
| 2972 | if (!arg) |
| 2973 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2974 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2975 | Py_DECREF(arg); |
| 2976 | if (!output) |
| 2977 | return 0; |
| 2978 | if (!PyBytes_Check(output)) { |
| 2979 | Py_DECREF(output); |
| 2980 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 2981 | return 0; |
| 2982 | } |
| 2983 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 2984 | size = PyBytes_GET_SIZE(output); |
| 2985 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2986 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 2987 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2988 | Py_DECREF(output); |
| 2989 | return 0; |
| 2990 | } |
| 2991 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2992 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2996 | int |
| 2997 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 2998 | { |
| 2999 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3000 | if (arg == NULL) { |
| 3001 | Py_DECREF(*(PyObject**)addr); |
| 3002 | return 1; |
| 3003 | } |
| 3004 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3005 | if (PyUnicode_READY(arg)) |
| 3006 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3007 | output = arg; |
| 3008 | Py_INCREF(output); |
| 3009 | } |
| 3010 | else { |
| 3011 | arg = PyBytes_FromObject(arg); |
| 3012 | if (!arg) |
| 3013 | return 0; |
| 3014 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3015 | PyBytes_GET_SIZE(arg)); |
| 3016 | Py_DECREF(arg); |
| 3017 | if (!output) |
| 3018 | return 0; |
| 3019 | if (!PyUnicode_Check(output)) { |
| 3020 | Py_DECREF(output); |
| 3021 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3022 | return 0; |
| 3023 | } |
| 3024 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3025 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 3026 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3027 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3028 | Py_DECREF(output); |
| 3029 | return 0; |
| 3030 | } |
| 3031 | *(PyObject**)addr = output; |
| 3032 | return Py_CLEANUP_SUPPORTED; |
| 3033 | } |
| 3034 | |
| 3035 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3036 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3037 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3038 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3039 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3040 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 3041 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3042 | if (!PyUnicode_Check(unicode)) { |
| 3043 | PyErr_BadArgument(); |
| 3044 | return NULL; |
| 3045 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3046 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3047 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3048 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3049 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3050 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3051 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3052 | if (bytes == NULL) |
| 3053 | return NULL; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3054 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3055 | if (_PyUnicode_UTF8(u) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3056 | Py_DECREF(bytes); |
| 3057 | return NULL; |
| 3058 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3059 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); |
| 3060 | Py_MEMCPY(_PyUnicode_UTF8(u), PyBytes_AS_STRING(bytes), _PyUnicode_UTF8_LENGTH(u) + 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3061 | Py_DECREF(bytes); |
| 3062 | } |
| 3063 | |
| 3064 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3065 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3066 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
| 3069 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3070 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3071 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3072 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3073 | } |
| 3074 | |
| 3075 | #ifdef Py_DEBUG |
| 3076 | int unicode_as_unicode_calls = 0; |
| 3077 | #endif |
| 3078 | |
| 3079 | |
| 3080 | Py_UNICODE * |
| 3081 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3082 | { |
| 3083 | PyUnicodeObject *u; |
| 3084 | const unsigned char *one_byte; |
| 3085 | #if SIZEOF_WCHAR_T == 4 |
| 3086 | const Py_UCS2 *two_bytes; |
| 3087 | #else |
| 3088 | const Py_UCS4 *four_bytes; |
| 3089 | const Py_UCS4 *ucs4_end; |
| 3090 | Py_ssize_t num_surrogates; |
| 3091 | #endif |
| 3092 | wchar_t *w; |
| 3093 | wchar_t *wchar_end; |
| 3094 | |
| 3095 | if (!PyUnicode_Check(unicode)) { |
| 3096 | PyErr_BadArgument(); |
| 3097 | return NULL; |
| 3098 | } |
| 3099 | u = (PyUnicodeObject*)unicode; |
| 3100 | if (_PyUnicode_WSTR(u) == NULL) { |
| 3101 | /* Non-ASCII compact unicode object */ |
| 3102 | assert(_PyUnicode_KIND(u) != 0); |
| 3103 | assert(PyUnicode_IS_READY(u)); |
| 3104 | |
| 3105 | #ifdef Py_DEBUG |
| 3106 | ++unicode_as_unicode_calls; |
| 3107 | #endif |
| 3108 | |
| 3109 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 3110 | #if SIZEOF_WCHAR_T == 2 |
| 3111 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3112 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 3113 | num_surrogates = 0; |
| 3114 | |
| 3115 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3116 | if (*four_bytes > 0xFFFF) |
| 3117 | ++num_surrogates; |
| 3118 | } |
| 3119 | |
| 3120 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 3121 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 3122 | if (!_PyUnicode_WSTR(u)) { |
| 3123 | PyErr_NoMemory(); |
| 3124 | return NULL; |
| 3125 | } |
| 3126 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 3127 | |
| 3128 | w = _PyUnicode_WSTR(u); |
| 3129 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 3130 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3131 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3132 | if (*four_bytes > 0xFFFF) { |
| 3133 | /* encode surrogate pair in this case */ |
| 3134 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3135 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3136 | } |
| 3137 | else |
| 3138 | *w = *four_bytes; |
| 3139 | |
| 3140 | if (w > wchar_end) { |
| 3141 | assert(0 && "Miscalculated string end"); |
| 3142 | } |
| 3143 | } |
| 3144 | *w = 0; |
| 3145 | #else |
| 3146 | /* sizeof(wchar_t) == 4 */ |
| 3147 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3148 | "should share memory already."); |
| 3149 | return NULL; |
| 3150 | #endif |
| 3151 | } |
| 3152 | else { |
| 3153 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3154 | (_PyUnicode_LENGTH(u) + 1)); |
| 3155 | if (!_PyUnicode_WSTR(u)) { |
| 3156 | PyErr_NoMemory(); |
| 3157 | return NULL; |
| 3158 | } |
| 3159 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 3160 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 3161 | w = _PyUnicode_WSTR(u); |
| 3162 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 3163 | |
| 3164 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 3165 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 3166 | for (; w < wchar_end; ++one_byte, ++w) |
| 3167 | *w = *one_byte; |
| 3168 | /* null-terminate the wstr */ |
| 3169 | *w = 0; |
| 3170 | } |
| 3171 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 3172 | #if SIZEOF_WCHAR_T == 4 |
| 3173 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 3174 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3175 | *w = *two_bytes; |
| 3176 | /* null-terminate the wstr */ |
| 3177 | *w = 0; |
| 3178 | #else |
| 3179 | /* sizeof(wchar_t) == 2 */ |
| 3180 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 3181 | _PyUnicode_WSTR(u) = NULL; |
| 3182 | Py_FatalError("Impossible unicode object state, wstr " |
| 3183 | "and str should share memory already."); |
| 3184 | return NULL; |
| 3185 | #endif |
| 3186 | } |
| 3187 | else { |
| 3188 | assert(0 && "This should never happen."); |
| 3189 | } |
| 3190 | } |
| 3191 | } |
| 3192 | if (size != NULL) |
| 3193 | *size = PyUnicode_WSTR_LENGTH(u); |
| 3194 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3197 | Py_UNICODE * |
| 3198 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3199 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3200 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3203 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3204 | Py_ssize_t |
| 3205 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3206 | { |
| 3207 | if (!PyUnicode_Check(unicode)) { |
| 3208 | PyErr_BadArgument(); |
| 3209 | goto onError; |
| 3210 | } |
| 3211 | return PyUnicode_GET_SIZE(unicode); |
| 3212 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3213 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3214 | return -1; |
| 3215 | } |
| 3216 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3217 | Py_ssize_t |
| 3218 | PyUnicode_GetLength(PyObject *unicode) |
| 3219 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3220 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3221 | PyErr_BadArgument(); |
| 3222 | return -1; |
| 3223 | } |
| 3224 | |
| 3225 | return PyUnicode_GET_LENGTH(unicode); |
| 3226 | } |
| 3227 | |
| 3228 | Py_UCS4 |
| 3229 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3230 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3231 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3232 | PyErr_BadArgument(); |
| 3233 | return (Py_UCS4)-1; |
| 3234 | } |
| 3235 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3236 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3237 | return (Py_UCS4)-1; |
| 3238 | } |
| 3239 | return PyUnicode_READ_CHAR(unicode, index); |
| 3240 | } |
| 3241 | |
| 3242 | int |
| 3243 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3244 | { |
| 3245 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3246 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3247 | return -1; |
| 3248 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3249 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3250 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3251 | return -1; |
| 3252 | } |
| 3253 | if (_PyUnicode_Dirty(unicode)) |
| 3254 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3255 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3256 | index, ch); |
| 3257 | return 0; |
| 3258 | } |
| 3259 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3260 | const char * |
| 3261 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3262 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3263 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3266 | /* create or adjust a UnicodeDecodeError */ |
| 3267 | static void |
| 3268 | make_decode_exception(PyObject **exceptionObject, |
| 3269 | const char *encoding, |
| 3270 | const char *input, Py_ssize_t length, |
| 3271 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3272 | const char *reason) |
| 3273 | { |
| 3274 | if (*exceptionObject == NULL) { |
| 3275 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3276 | encoding, input, length, startpos, endpos, reason); |
| 3277 | } |
| 3278 | else { |
| 3279 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3280 | goto onError; |
| 3281 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3282 | goto onError; |
| 3283 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3284 | goto onError; |
| 3285 | } |
| 3286 | return; |
| 3287 | |
| 3288 | onError: |
| 3289 | Py_DECREF(*exceptionObject); |
| 3290 | *exceptionObject = NULL; |
| 3291 | } |
| 3292 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3293 | /* error handling callback helper: |
| 3294 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3295 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3296 | and adjust various state variables. |
| 3297 | return 0 on success, -1 on error |
| 3298 | */ |
| 3299 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3300 | static int |
| 3301 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3302 | const char *encoding, const char *reason, |
| 3303 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3304 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3305 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3306 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3307 | 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] | 3308 | |
| 3309 | PyObject *restuple = NULL; |
| 3310 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3311 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3312 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3313 | Py_ssize_t requiredsize; |
| 3314 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3315 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3316 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3317 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3318 | int res = -1; |
| 3319 | |
| 3320 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3321 | *errorHandler = PyCodec_LookupError(errors); |
| 3322 | if (*errorHandler == NULL) |
| 3323 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3326 | make_decode_exception(exceptionObject, |
| 3327 | encoding, |
| 3328 | *input, *inend - *input, |
| 3329 | *startinpos, *endinpos, |
| 3330 | reason); |
| 3331 | if (*exceptionObject == NULL) |
| 3332 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3333 | |
| 3334 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3335 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3336 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3337 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3338 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3339 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3340 | } |
| 3341 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3342 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3343 | |
| 3344 | /* Copy back the bytes variables, which might have been modified by the |
| 3345 | callback */ |
| 3346 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3347 | if (!inputobj) |
| 3348 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3349 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3350 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3351 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3352 | *input = PyBytes_AS_STRING(inputobj); |
| 3353 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3354 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3355 | /* we can DECREF safely, as the exception has another reference, |
| 3356 | so the object won't go away. */ |
| 3357 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3358 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3359 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3360 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3361 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3362 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3363 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3364 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3365 | |
| 3366 | /* need more space? (at least enough for what we |
| 3367 | have+the replacement+the rest of the string (starting |
| 3368 | at the new input position), so we won't have to check space |
| 3369 | when there are no errors in the rest of the string) */ |
| 3370 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3371 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3372 | requiredsize = *outpos + repsize + insize-newpos; |
| 3373 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3374 | if (requiredsize<2*outsize) |
| 3375 | requiredsize = 2*outsize; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3376 | if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3377 | goto onError; |
| 3378 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3379 | } |
| 3380 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3381 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3382 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3383 | *outptr += repsize; |
| 3384 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3385 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3386 | /* we made it! */ |
| 3387 | res = 0; |
| 3388 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3389 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3390 | Py_XDECREF(restuple); |
| 3391 | return res; |
| 3392 | } |
| 3393 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3394 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3395 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3396 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3397 | |
| 3398 | /* Three simple macros defining base-64. */ |
| 3399 | |
| 3400 | /* Is c a base-64 character? */ |
| 3401 | |
| 3402 | #define IS_BASE64(c) \ |
| 3403 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3404 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3405 | ((c) >= '0' && (c) <= '9') || \ |
| 3406 | (c) == '+' || (c) == '/') |
| 3407 | |
| 3408 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3409 | |
| 3410 | #define FROM_BASE64(c) \ |
| 3411 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3412 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3413 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3414 | (c) == '+' ? 62 : 63) |
| 3415 | |
| 3416 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3417 | |
| 3418 | #define TO_BASE64(n) \ |
| 3419 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3420 | |
| 3421 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3422 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3423 | * byte not decoding to itself is the + which begins a base64 |
| 3424 | * string. */ |
| 3425 | |
| 3426 | #define DECODE_DIRECT(c) \ |
| 3427 | ((c) <= 127 && (c) != '+') |
| 3428 | |
| 3429 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3430 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3431 | * the above). See RFC2152. This array identifies these different |
| 3432 | * sets: |
| 3433 | * 0 : "Set D" |
| 3434 | * alphanumeric and '(),-./:? |
| 3435 | * 1 : "Set O" |
| 3436 | * !"#$%&*;<=>@[]^_`{|} |
| 3437 | * 2 : "whitespace" |
| 3438 | * ht nl cr sp |
| 3439 | * 3 : special (must be base64 encoded) |
| 3440 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3441 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3442 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3443 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3444 | char utf7_category[128] = { |
| 3445 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3446 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3447 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3448 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3449 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3450 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3451 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3452 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3453 | /* @ A B C D E F G H I J K L M N O */ |
| 3454 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3455 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3456 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3457 | /* ` a b c d e f g h i j k l m n o */ |
| 3458 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3459 | /* p q r s t u v w x y z { | } ~ del */ |
| 3460 | 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] | 3461 | }; |
| 3462 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3463 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3464 | * answer depends on whether we are encoding set O as itself, and also |
| 3465 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3466 | * clear that the answers to these questions vary between |
| 3467 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3468 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3469 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3470 | ((c) < 128 && (c) > 0 && \ |
| 3471 | ((utf7_category[(c)] == 0) || \ |
| 3472 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3473 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3474 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3475 | PyObject * |
| 3476 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3477 | Py_ssize_t size, |
| 3478 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3479 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3480 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3481 | } |
| 3482 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3483 | /* The decoder. The only state we preserve is our read position, |
| 3484 | * i.e. how many characters we have consumed. So if we end in the |
| 3485 | * middle of a shift sequence we have to back off the read position |
| 3486 | * and the output to the beginning of the sequence, otherwise we lose |
| 3487 | * all the shift state (seen bits, number of bits seen, high |
| 3488 | * surrogate). */ |
| 3489 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3490 | PyObject * |
| 3491 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3492 | Py_ssize_t size, |
| 3493 | const char *errors, |
| 3494 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3495 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3496 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3497 | Py_ssize_t startinpos; |
| 3498 | Py_ssize_t endinpos; |
| 3499 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3500 | const char *e; |
| 3501 | PyUnicodeObject *unicode; |
| 3502 | Py_UNICODE *p; |
| 3503 | const char *errmsg = ""; |
| 3504 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3505 | Py_UNICODE *shiftOutStart; |
| 3506 | unsigned int base64bits = 0; |
| 3507 | unsigned long base64buffer = 0; |
| 3508 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3509 | PyObject *errorHandler = NULL; |
| 3510 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3511 | |
| 3512 | unicode = _PyUnicode_New(size); |
| 3513 | if (!unicode) |
| 3514 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3515 | if (size == 0) { |
| 3516 | if (consumed) |
| 3517 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3518 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3519 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3520 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3521 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3522 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3523 | e = s + size; |
| 3524 | |
| 3525 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3526 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3527 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3528 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3529 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3530 | if (inShift) { /* in a base-64 section */ |
| 3531 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3532 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3533 | base64bits += 6; |
| 3534 | s++; |
| 3535 | if (base64bits >= 16) { |
| 3536 | /* we have enough bits for a UTF-16 value */ |
| 3537 | Py_UNICODE outCh = (Py_UNICODE) |
| 3538 | (base64buffer >> (base64bits-16)); |
| 3539 | base64bits -= 16; |
| 3540 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3541 | if (surrogate) { |
| 3542 | /* expecting a second surrogate */ |
| 3543 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3544 | #ifdef Py_UNICODE_WIDE |
| 3545 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3546 | | (outCh & 0x3FF)) + 0x10000; |
| 3547 | #else |
| 3548 | *p++ = surrogate; |
| 3549 | *p++ = outCh; |
| 3550 | #endif |
| 3551 | surrogate = 0; |
| 3552 | } |
| 3553 | else { |
| 3554 | surrogate = 0; |
| 3555 | errmsg = "second surrogate missing"; |
| 3556 | goto utf7Error; |
| 3557 | } |
| 3558 | } |
| 3559 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3560 | /* first surrogate */ |
| 3561 | surrogate = outCh; |
| 3562 | } |
| 3563 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3564 | errmsg = "unexpected second surrogate"; |
| 3565 | goto utf7Error; |
| 3566 | } |
| 3567 | else { |
| 3568 | *p++ = outCh; |
| 3569 | } |
| 3570 | } |
| 3571 | } |
| 3572 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3573 | inShift = 0; |
| 3574 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3575 | if (surrogate) { |
| 3576 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3577 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3578 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3579 | if (base64bits > 0) { /* left-over bits */ |
| 3580 | if (base64bits >= 6) { |
| 3581 | /* We've seen at least one base-64 character */ |
| 3582 | errmsg = "partial character in shift sequence"; |
| 3583 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3584 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3585 | else { |
| 3586 | /* Some bits remain; they should be zero */ |
| 3587 | if (base64buffer != 0) { |
| 3588 | errmsg = "non-zero padding bits in shift sequence"; |
| 3589 | goto utf7Error; |
| 3590 | } |
| 3591 | } |
| 3592 | } |
| 3593 | if (ch != '-') { |
| 3594 | /* '-' is absorbed; other terminating |
| 3595 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3596 | *p++ = ch; |
| 3597 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3598 | } |
| 3599 | } |
| 3600 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3601 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3602 | s++; /* consume '+' */ |
| 3603 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3604 | s++; |
| 3605 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3606 | } |
| 3607 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3608 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3609 | shiftOutStart = p; |
| 3610 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3611 | } |
| 3612 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3613 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3614 | *p++ = ch; |
| 3615 | s++; |
| 3616 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3617 | else { |
| 3618 | startinpos = s-starts; |
| 3619 | s++; |
| 3620 | errmsg = "unexpected special character"; |
| 3621 | goto utf7Error; |
| 3622 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3623 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3624 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3625 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3626 | endinpos = s-starts; |
| 3627 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3628 | errors, &errorHandler, |
| 3629 | "utf7", errmsg, |
| 3630 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3631 | &unicode, &outpos, &p)) |
| 3632 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3635 | /* end of string */ |
| 3636 | |
| 3637 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3638 | /* if we're in an inconsistent state, that's an error */ |
| 3639 | if (surrogate || |
| 3640 | (base64bits >= 6) || |
| 3641 | (base64bits > 0 && base64buffer != 0)) { |
| 3642 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3643 | endinpos = size; |
| 3644 | if (unicode_decode_call_errorhandler( |
| 3645 | errors, &errorHandler, |
| 3646 | "utf7", "unterminated shift sequence", |
| 3647 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3648 | &unicode, &outpos, &p)) |
| 3649 | goto onError; |
| 3650 | if (s < e) |
| 3651 | goto restart; |
| 3652 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3653 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3654 | |
| 3655 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3656 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3657 | if (inShift) { |
| 3658 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3659 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3660 | } |
| 3661 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3662 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3663 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3664 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3665 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3666 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3667 | goto onError; |
| 3668 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3669 | Py_XDECREF(errorHandler); |
| 3670 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3671 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3672 | Py_DECREF(unicode); |
| 3673 | return NULL; |
| 3674 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3675 | return (PyObject *)unicode; |
| 3676 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3677 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3678 | Py_XDECREF(errorHandler); |
| 3679 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3680 | Py_DECREF(unicode); |
| 3681 | return NULL; |
| 3682 | } |
| 3683 | |
| 3684 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3685 | PyObject * |
| 3686 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3687 | Py_ssize_t size, |
| 3688 | int base64SetO, |
| 3689 | int base64WhiteSpace, |
| 3690 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3691 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3692 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3693 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3694 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3695 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3696 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3697 | unsigned int base64bits = 0; |
| 3698 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3699 | char * out; |
| 3700 | char * start; |
| 3701 | |
| 3702 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3703 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3704 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3705 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3706 | return PyErr_NoMemory(); |
| 3707 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3708 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3709 | if (v == NULL) |
| 3710 | return NULL; |
| 3711 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3712 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3713 | for (;i < size; ++i) { |
| 3714 | Py_UNICODE ch = s[i]; |
| 3715 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3716 | if (inShift) { |
| 3717 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3718 | /* shifting out */ |
| 3719 | if (base64bits) { /* output remaining bits */ |
| 3720 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3721 | base64buffer = 0; |
| 3722 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3723 | } |
| 3724 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3725 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3726 | so no '-' is required, except if the character is itself a '-' */ |
| 3727 | if (IS_BASE64(ch) || ch == '-') { |
| 3728 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3729 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3730 | *out++ = (char) ch; |
| 3731 | } |
| 3732 | else { |
| 3733 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3734 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3735 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3736 | else { /* not in a shift sequence */ |
| 3737 | if (ch == '+') { |
| 3738 | *out++ = '+'; |
| 3739 | *out++ = '-'; |
| 3740 | } |
| 3741 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3742 | *out++ = (char) ch; |
| 3743 | } |
| 3744 | else { |
| 3745 | *out++ = '+'; |
| 3746 | inShift = 1; |
| 3747 | goto encode_char; |
| 3748 | } |
| 3749 | } |
| 3750 | continue; |
| 3751 | encode_char: |
| 3752 | #ifdef Py_UNICODE_WIDE |
| 3753 | if (ch >= 0x10000) { |
| 3754 | /* code first surrogate */ |
| 3755 | base64bits += 16; |
| 3756 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3757 | while (base64bits >= 6) { |
| 3758 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3759 | base64bits -= 6; |
| 3760 | } |
| 3761 | /* prepare second surrogate */ |
| 3762 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3763 | } |
| 3764 | #endif |
| 3765 | base64bits += 16; |
| 3766 | base64buffer = (base64buffer << 16) | ch; |
| 3767 | while (base64bits >= 6) { |
| 3768 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3769 | base64bits -= 6; |
| 3770 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3771 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3772 | if (base64bits) |
| 3773 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3774 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3775 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3776 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3777 | return NULL; |
| 3778 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3779 | } |
| 3780 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3781 | #undef IS_BASE64 |
| 3782 | #undef FROM_BASE64 |
| 3783 | #undef TO_BASE64 |
| 3784 | #undef DECODE_DIRECT |
| 3785 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3786 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3787 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 3788 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3789 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3790 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3791 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 3792 | illegal prefix. See RFC 3629 for details */ |
| 3793 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 3794 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 3795 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3796 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3797 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3798 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3799 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3800 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 3801 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8F */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3802 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3803 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3804 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 3805 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 3806 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 3807 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 3808 | 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0-F4 + F5-FF */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3809 | }; |
| 3810 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3811 | PyObject * |
| 3812 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3813 | Py_ssize_t size, |
| 3814 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3815 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3816 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 3817 | } |
| 3818 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3819 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 3820 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 3821 | |
| 3822 | /* Mask to quickly check whether a C 'long' contains a |
| 3823 | non-ASCII, UTF8-encoded char. */ |
| 3824 | #if (SIZEOF_LONG == 8) |
| 3825 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 3826 | #elif (SIZEOF_LONG == 4) |
| 3827 | # define ASCII_CHAR_MASK 0x80808080L |
| 3828 | #else |
| 3829 | # error C 'long' size should be either 4 or 8! |
| 3830 | #endif |
| 3831 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3832 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 3833 | the size of the decoded unicode string and if any major errors were |
| 3834 | encountered. |
| 3835 | |
| 3836 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 3837 | if the string contains surrogates, and if all continuation bytes are |
| 3838 | within the correct ranges, these checks are performed in |
| 3839 | PyUnicode_DecodeUTF8Stateful. |
| 3840 | |
| 3841 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 3842 | will be bogus and you should not rely on useful information in them. |
| 3843 | */ |
| 3844 | static Py_UCS4 |
| 3845 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 3846 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 3847 | int *has_errors) |
| 3848 | { |
| 3849 | Py_ssize_t n; |
| 3850 | Py_ssize_t char_count = 0; |
| 3851 | Py_UCS4 max_char = 127, new_max; |
| 3852 | Py_UCS4 upper_bound; |
| 3853 | const unsigned char *p = (const unsigned char *)s; |
| 3854 | const unsigned char *end = p + string_size; |
| 3855 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 3856 | int err = 0; |
| 3857 | |
| 3858 | for (; p < end && !err; ++p, ++char_count) { |
| 3859 | /* Only check value if it's not a ASCII char... */ |
| 3860 | if (*p < 0x80) { |
| 3861 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 3862 | an explanation. */ |
| 3863 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 3864 | /* Help register allocation */ |
| 3865 | register const unsigned char *_p = p; |
| 3866 | while (_p < aligned_end) { |
| 3867 | unsigned long value = *(unsigned long *) _p; |
| 3868 | if (value & ASCII_CHAR_MASK) |
| 3869 | break; |
| 3870 | _p += SIZEOF_LONG; |
| 3871 | char_count += SIZEOF_LONG; |
| 3872 | } |
| 3873 | p = _p; |
| 3874 | if (p == end) |
| 3875 | break; |
| 3876 | } |
| 3877 | } |
| 3878 | if (*p >= 0x80) { |
| 3879 | n = utf8_code_length[*p]; |
| 3880 | new_max = max_char; |
| 3881 | switch (n) { |
| 3882 | /* invalid start byte */ |
| 3883 | case 0: |
| 3884 | err = 1; |
| 3885 | break; |
| 3886 | case 2: |
| 3887 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 3888 | Approximate the upper bound of the code point, |
| 3889 | if this flips over 255 we can be sure it will be more |
| 3890 | than 255 and the string will need 2 bytes per code coint, |
| 3891 | if it stays under or equal to 255, we can be sure 1 byte |
| 3892 | is enough. |
| 3893 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 3894 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 3895 | if (max_char < upper_bound) |
| 3896 | new_max = upper_bound; |
| 3897 | /* Ensure we track at least that we left ASCII space. */ |
| 3898 | if (new_max < 128) |
| 3899 | new_max = 128; |
| 3900 | break; |
| 3901 | case 3: |
| 3902 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 3903 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 3904 | if (max_char < 65535) |
| 3905 | new_max = 65535; |
| 3906 | break; |
| 3907 | case 4: |
| 3908 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 3909 | new_max = 65537; |
| 3910 | break; |
| 3911 | /* Internal error, this should be caught by the first if */ |
| 3912 | case 1: |
| 3913 | default: |
| 3914 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 3915 | err = 1; |
| 3916 | } |
| 3917 | /* Instead of number of overall bytes for this code point, |
| 3918 | n containts the number of following bytes: */ |
| 3919 | --n; |
| 3920 | /* Check if the follow up chars are all valid continuation bytes */ |
| 3921 | if (n >= 1) { |
| 3922 | const unsigned char *cont; |
| 3923 | if ((p + n) >= end) { |
| 3924 | if (consumed == 0) |
| 3925 | /* incomplete data, non-incremental decoding */ |
| 3926 | err = 1; |
| 3927 | break; |
| 3928 | } |
| 3929 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 3930 | if ((*cont & 0xc0) != 0x80) { |
| 3931 | err = 1; |
| 3932 | break; |
| 3933 | } |
| 3934 | } |
| 3935 | p += n; |
| 3936 | } |
| 3937 | else |
| 3938 | err = 1; |
| 3939 | max_char = new_max; |
| 3940 | } |
| 3941 | } |
| 3942 | |
| 3943 | if (unicode_size) |
| 3944 | *unicode_size = char_count; |
| 3945 | if (has_errors) |
| 3946 | *has_errors = err; |
| 3947 | return max_char; |
| 3948 | } |
| 3949 | |
| 3950 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 3951 | of the legacy unicode representation */ |
| 3952 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 3953 | do { \ |
| 3954 | const int k_ = (kind); \ |
| 3955 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 3956 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 3957 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 3958 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 3959 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 3960 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 3961 | else \ |
| 3962 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 3963 | } while (0) |
| 3964 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3965 | PyObject * |
| 3966 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3967 | Py_ssize_t size, |
| 3968 | const char *errors, |
| 3969 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3970 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3971 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3972 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3973 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3974 | Py_ssize_t startinpos; |
| 3975 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3976 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3977 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3978 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3979 | PyObject *errorHandler = NULL; |
| 3980 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3981 | Py_UCS4 maxchar = 0; |
| 3982 | Py_ssize_t unicode_size; |
| 3983 | Py_ssize_t i; |
| 3984 | int kind; |
| 3985 | void *data; |
| 3986 | int has_errors; |
| 3987 | Py_UNICODE *error_outptr; |
| 3988 | #if SIZEOF_WCHAR_T == 2 |
| 3989 | Py_ssize_t wchar_offset = 0; |
| 3990 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3991 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3992 | if (size == 0) { |
| 3993 | if (consumed) |
| 3994 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3995 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3996 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3997 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 3998 | consumed, &has_errors); |
| 3999 | if (has_errors) { |
| 4000 | unicode = _PyUnicode_New(size); |
| 4001 | if (!unicode) |
| 4002 | return NULL; |
| 4003 | kind = PyUnicode_WCHAR_KIND; |
| 4004 | data = PyUnicode_AS_UNICODE(unicode); |
| 4005 | assert(data != NULL); |
| 4006 | } |
| 4007 | else { |
| 4008 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 4009 | if (!unicode) |
| 4010 | return NULL; |
| 4011 | /* When the string is ASCII only, just use memcpy and return. |
| 4012 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4013 | sequence at the end of the ASCII block. */ |
| 4014 | if (maxchar < 128 && size == unicode_size) { |
| 4015 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4016 | return (PyObject *)unicode; |
| 4017 | } |
| 4018 | kind = PyUnicode_KIND(unicode); |
| 4019 | data = PyUnicode_DATA(unicode); |
| 4020 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4021 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4022 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4023 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4024 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4025 | |
| 4026 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4027 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4028 | |
| 4029 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4030 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4031 | input will consist of an overwhelming majority of ASCII |
| 4032 | characters, we try to optimize for this case by checking |
| 4033 | as many characters as a C 'long' can contain. |
| 4034 | First, check if we can do an aligned read, as most CPUs have |
| 4035 | a penalty for unaligned reads. |
| 4036 | */ |
| 4037 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4038 | /* Help register allocation */ |
| 4039 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4040 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4041 | while (_s < aligned_end) { |
| 4042 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4043 | and do a fast unrolled copy if it only contains ASCII |
| 4044 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4045 | unsigned long value = *(unsigned long *) _s; |
| 4046 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4047 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4048 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4049 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4050 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4051 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4052 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4053 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4054 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4055 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4056 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4057 | #endif |
| 4058 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4059 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4060 | } |
| 4061 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4062 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4063 | if (s == e) |
| 4064 | break; |
| 4065 | ch = (unsigned char)*s; |
| 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4070 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4071 | s++; |
| 4072 | continue; |
| 4073 | } |
| 4074 | |
| 4075 | n = utf8_code_length[ch]; |
| 4076 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4077 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4078 | if (consumed) |
| 4079 | break; |
| 4080 | else { |
| 4081 | errmsg = "unexpected end of data"; |
| 4082 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4083 | endinpos = startinpos+1; |
| 4084 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4085 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4086 | goto utf8Error; |
| 4087 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4088 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4089 | |
| 4090 | switch (n) { |
| 4091 | |
| 4092 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4093 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4094 | startinpos = s-starts; |
| 4095 | endinpos = startinpos+1; |
| 4096 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4097 | |
| 4098 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4099 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4100 | startinpos = s-starts; |
| 4101 | endinpos = startinpos+1; |
| 4102 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4103 | |
| 4104 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4105 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4106 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4107 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4108 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4109 | goto utf8Error; |
| 4110 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4111 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4112 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4113 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4114 | break; |
| 4115 | |
| 4116 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4117 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4118 | will result in surrogates in range d800-dfff. Surrogates are |
| 4119 | not valid UTF-8 so they are rejected. |
| 4120 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4121 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4122 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4123 | (s[2] & 0xc0) != 0x80 || |
| 4124 | ((unsigned char)s[0] == 0xE0 && |
| 4125 | (unsigned char)s[1] < 0xA0) || |
| 4126 | ((unsigned char)s[0] == 0xED && |
| 4127 | (unsigned char)s[1] > 0x9F)) { |
| 4128 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4129 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4130 | endinpos = startinpos + 1; |
| 4131 | |
| 4132 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4133 | continuation byte is s[2], so increment endinpos by 1, |
| 4134 | if not, s[1] is invalid and endinpos doesn't need to |
| 4135 | be incremented. */ |
| 4136 | if ((s[1] & 0xC0) == 0x80) |
| 4137 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4138 | goto utf8Error; |
| 4139 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4140 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4141 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4142 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4143 | break; |
| 4144 | |
| 4145 | case 4: |
| 4146 | if ((s[1] & 0xc0) != 0x80 || |
| 4147 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4148 | (s[3] & 0xc0) != 0x80 || |
| 4149 | ((unsigned char)s[0] == 0xF0 && |
| 4150 | (unsigned char)s[1] < 0x90) || |
| 4151 | ((unsigned char)s[0] == 0xF4 && |
| 4152 | (unsigned char)s[1] > 0x8F)) { |
| 4153 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4154 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4155 | endinpos = startinpos + 1; |
| 4156 | if ((s[1] & 0xC0) == 0x80) { |
| 4157 | endinpos++; |
| 4158 | if ((s[2] & 0xC0) == 0x80) |
| 4159 | endinpos++; |
| 4160 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4161 | goto utf8Error; |
| 4162 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4163 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4164 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4165 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4166 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4167 | /* If the string is flexible or we have native UCS-4, write |
| 4168 | directly.. */ |
| 4169 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4170 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4172 | else { |
| 4173 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4174 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4175 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4176 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4177 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4178 | /* high surrogate = top 10 bits added to D800 */ |
| 4179 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4180 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4181 | |
| 4182 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4183 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4184 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4185 | } |
| 4186 | #if SIZEOF_WCHAR_T == 2 |
| 4187 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4188 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4189 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4190 | } |
| 4191 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4192 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4193 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4194 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4195 | /* If this is not yet a resizable string, make it one.. */ |
| 4196 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4197 | const Py_UNICODE *u; |
| 4198 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 4199 | if (!new_unicode) |
| 4200 | goto onError; |
| 4201 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 4202 | if (!u) |
| 4203 | goto onError; |
| 4204 | #if SIZEOF_WCHAR_T == 2 |
| 4205 | i += wchar_offset; |
| 4206 | #endif |
| 4207 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4208 | Py_DECREF(unicode); |
| 4209 | unicode = new_unicode; |
| 4210 | kind = 0; |
| 4211 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4212 | assert(data != NULL); |
| 4213 | } |
| 4214 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4215 | if (unicode_decode_call_errorhandler( |
| 4216 | errors, &errorHandler, |
| 4217 | "utf8", errmsg, |
| 4218 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4219 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4220 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4221 | /* Update data because unicode_decode_call_errorhandler might have |
| 4222 | re-created or resized the unicode object. */ |
| 4223 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4224 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4225 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4226 | /* Ensure the unicode_size calculation above was correct: */ |
| 4227 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4228 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4229 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4230 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4231 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4232 | /* Adjust length and ready string when it contained errors and |
| 4233 | is of the old resizable kind. */ |
| 4234 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4235 | if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4236 | goto onError; |
| 4237 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4238 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4239 | Py_XDECREF(errorHandler); |
| 4240 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4241 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4242 | Py_DECREF(unicode); |
| 4243 | return NULL; |
| 4244 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4245 | return (PyObject *)unicode; |
| 4246 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4247 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4248 | Py_XDECREF(errorHandler); |
| 4249 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4250 | Py_DECREF(unicode); |
| 4251 | return NULL; |
| 4252 | } |
| 4253 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4254 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4255 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4256 | #ifdef __APPLE__ |
| 4257 | |
| 4258 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4259 | used to decode the command line arguments on Mac OS X. */ |
| 4260 | |
| 4261 | wchar_t* |
| 4262 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4263 | { |
| 4264 | int n; |
| 4265 | const char *e; |
| 4266 | wchar_t *unicode, *p; |
| 4267 | |
| 4268 | /* Note: size will always be longer than the resulting Unicode |
| 4269 | character count */ |
| 4270 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4271 | PyErr_NoMemory(); |
| 4272 | return NULL; |
| 4273 | } |
| 4274 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4275 | if (!unicode) |
| 4276 | return NULL; |
| 4277 | |
| 4278 | /* Unpack UTF-8 encoded data */ |
| 4279 | p = unicode; |
| 4280 | e = s + size; |
| 4281 | while (s < e) { |
| 4282 | Py_UCS4 ch = (unsigned char)*s; |
| 4283 | |
| 4284 | if (ch < 0x80) { |
| 4285 | *p++ = (wchar_t)ch; |
| 4286 | s++; |
| 4287 | continue; |
| 4288 | } |
| 4289 | |
| 4290 | n = utf8_code_length[ch]; |
| 4291 | if (s + n > e) { |
| 4292 | goto surrogateescape; |
| 4293 | } |
| 4294 | |
| 4295 | switch (n) { |
| 4296 | case 0: |
| 4297 | case 1: |
| 4298 | goto surrogateescape; |
| 4299 | |
| 4300 | case 2: |
| 4301 | if ((s[1] & 0xc0) != 0x80) |
| 4302 | goto surrogateescape; |
| 4303 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4304 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4305 | *p++ = (wchar_t)ch; |
| 4306 | break; |
| 4307 | |
| 4308 | case 3: |
| 4309 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4310 | will result in surrogates in range d800-dfff. Surrogates are |
| 4311 | not valid UTF-8 so they are rejected. |
| 4312 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4313 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4314 | if ((s[1] & 0xc0) != 0x80 || |
| 4315 | (s[2] & 0xc0) != 0x80 || |
| 4316 | ((unsigned char)s[0] == 0xE0 && |
| 4317 | (unsigned char)s[1] < 0xA0) || |
| 4318 | ((unsigned char)s[0] == 0xED && |
| 4319 | (unsigned char)s[1] > 0x9F)) { |
| 4320 | |
| 4321 | goto surrogateescape; |
| 4322 | } |
| 4323 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4324 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4325 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4326 | break; |
| 4327 | |
| 4328 | case 4: |
| 4329 | if ((s[1] & 0xc0) != 0x80 || |
| 4330 | (s[2] & 0xc0) != 0x80 || |
| 4331 | (s[3] & 0xc0) != 0x80 || |
| 4332 | ((unsigned char)s[0] == 0xF0 && |
| 4333 | (unsigned char)s[1] < 0x90) || |
| 4334 | ((unsigned char)s[0] == 0xF4 && |
| 4335 | (unsigned char)s[1] > 0x8F)) { |
| 4336 | goto surrogateescape; |
| 4337 | } |
| 4338 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4339 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4340 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4341 | |
| 4342 | #if SIZEOF_WCHAR_T == 4 |
| 4343 | *p++ = (wchar_t)ch; |
| 4344 | #else |
| 4345 | /* compute and append the two surrogates: */ |
| 4346 | |
| 4347 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4348 | ch -= 0x10000; |
| 4349 | |
| 4350 | /* high surrogate = top 10 bits added to D800 */ |
| 4351 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4352 | |
| 4353 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4354 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4355 | #endif |
| 4356 | break; |
| 4357 | } |
| 4358 | s += n; |
| 4359 | continue; |
| 4360 | |
| 4361 | surrogateescape: |
| 4362 | *p++ = 0xDC00 + ch; |
| 4363 | s++; |
| 4364 | } |
| 4365 | *p = L'\0'; |
| 4366 | return unicode; |
| 4367 | } |
| 4368 | |
| 4369 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4370 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4371 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4372 | |
| 4373 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4374 | and allocate exactly as much space needed at the end. Else allocate the |
| 4375 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4376 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4377 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4378 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4379 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4380 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4381 | #define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */ |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4382 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4383 | Py_ssize_t i; /* index into s of next input byte */ |
| 4384 | PyObject *result; /* result string object */ |
| 4385 | char *p; /* next free byte in output buffer */ |
| 4386 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4387 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4388 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4389 | PyObject *errorHandler = NULL; |
| 4390 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4391 | int kind; |
| 4392 | void *data; |
| 4393 | Py_ssize_t size; |
| 4394 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4395 | #if SIZEOF_WCHAR_T == 2 |
| 4396 | Py_ssize_t wchar_offset = 0; |
| 4397 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4398 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4399 | if (!PyUnicode_Check(unicode)) { |
| 4400 | PyErr_BadArgument(); |
| 4401 | return NULL; |
| 4402 | } |
| 4403 | |
| 4404 | if (PyUnicode_READY(unicode) == -1) |
| 4405 | return NULL; |
| 4406 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4407 | if (PyUnicode_UTF8(unicode)) |
| 4408 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4409 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4410 | |
| 4411 | kind = PyUnicode_KIND(unicode); |
| 4412 | data = PyUnicode_DATA(unicode); |
| 4413 | size = PyUnicode_GET_LENGTH(unicode); |
| 4414 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4415 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4416 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4417 | if (size <= MAX_SHORT_UNICHARS) { |
| 4418 | /* Write into the stack buffer; nallocated can't overflow. |
| 4419 | * At the end, we'll allocate exactly as much heap space as it |
| 4420 | * turns out we need. |
| 4421 | */ |
| 4422 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4423 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4424 | p = stackbuf; |
| 4425 | } |
| 4426 | else { |
| 4427 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4428 | nallocated = size * 4; |
| 4429 | if (nallocated / 4 != size) /* overflow! */ |
| 4430 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4431 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4432 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4433 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4434 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4435 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4436 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4437 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4438 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4439 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4440 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4441 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4442 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4443 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4444 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4445 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4446 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4447 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4448 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4449 | Py_ssize_t newpos; |
| 4450 | PyObject *rep; |
| 4451 | Py_ssize_t repsize, k, startpos; |
| 4452 | startpos = i-1; |
| 4453 | #if SIZEOF_WCHAR_T == 2 |
| 4454 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4455 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4456 | rep = unicode_encode_call_errorhandler( |
| 4457 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4458 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4459 | &exc, startpos, startpos+1, &newpos); |
| 4460 | if (!rep) |
| 4461 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4462 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4463 | if (PyBytes_Check(rep)) |
| 4464 | repsize = PyBytes_GET_SIZE(rep); |
| 4465 | else |
| 4466 | repsize = PyUnicode_GET_SIZE(rep); |
| 4467 | |
| 4468 | if (repsize > 4) { |
| 4469 | Py_ssize_t offset; |
| 4470 | |
| 4471 | if (result == NULL) |
| 4472 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4473 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4474 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4475 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4476 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4477 | /* integer overflow */ |
| 4478 | PyErr_NoMemory(); |
| 4479 | goto error; |
| 4480 | } |
| 4481 | nallocated += repsize - 4; |
| 4482 | if (result != NULL) { |
| 4483 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4484 | goto error; |
| 4485 | } else { |
| 4486 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4487 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4488 | goto error; |
| 4489 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4490 | } |
| 4491 | p = PyBytes_AS_STRING(result) + offset; |
| 4492 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4493 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4494 | if (PyBytes_Check(rep)) { |
| 4495 | char *prep = PyBytes_AS_STRING(rep); |
| 4496 | for(k = repsize; k > 0; k--) |
| 4497 | *p++ = *prep++; |
| 4498 | } else /* rep is unicode */ { |
| 4499 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4500 | Py_UNICODE c; |
| 4501 | |
| 4502 | for(k=0; k<repsize; k++) { |
| 4503 | c = prep[k]; |
| 4504 | if (0x80 <= c) { |
| 4505 | raise_encode_exception(&exc, "utf-8", |
| 4506 | PyUnicode_AS_UNICODE(unicode), |
| 4507 | size, i-1, i, |
| 4508 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4509 | goto error; |
| 4510 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4511 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4512 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4513 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4514 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4515 | } else if (ch < 0x10000) { |
| 4516 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4517 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4518 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4519 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4520 | /* Encode UCS4 Unicode ordinals */ |
| 4521 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4522 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4523 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4524 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4525 | #if SIZEOF_WCHAR_T == 2 |
| 4526 | wchar_offset++; |
| 4527 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4528 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4529 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4530 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4531 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4532 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4533 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4534 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4535 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4536 | } |
| 4537 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4538 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4539 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4540 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4541 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4542 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4543 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4544 | Py_XDECREF(errorHandler); |
| 4545 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4546 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4547 | error: |
| 4548 | Py_XDECREF(errorHandler); |
| 4549 | Py_XDECREF(exc); |
| 4550 | Py_XDECREF(result); |
| 4551 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4552 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4553 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4554 | } |
| 4555 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4556 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4557 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4558 | Py_ssize_t size, |
| 4559 | const char *errors) |
| 4560 | { |
| 4561 | PyObject *v, *unicode; |
| 4562 | |
| 4563 | unicode = PyUnicode_FromUnicode(s, size); |
| 4564 | if (unicode == NULL) |
| 4565 | return NULL; |
| 4566 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4567 | Py_DECREF(unicode); |
| 4568 | return v; |
| 4569 | } |
| 4570 | |
| 4571 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4572 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4573 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4574 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4577 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4578 | |
| 4579 | PyObject * |
| 4580 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4581 | Py_ssize_t size, |
| 4582 | const char *errors, |
| 4583 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4584 | { |
| 4585 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4586 | } |
| 4587 | |
| 4588 | PyObject * |
| 4589 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4590 | Py_ssize_t size, |
| 4591 | const char *errors, |
| 4592 | int *byteorder, |
| 4593 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4594 | { |
| 4595 | const char *starts = s; |
| 4596 | Py_ssize_t startinpos; |
| 4597 | Py_ssize_t endinpos; |
| 4598 | Py_ssize_t outpos; |
| 4599 | PyUnicodeObject *unicode; |
| 4600 | Py_UNICODE *p; |
| 4601 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4602 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4603 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4604 | #else |
| 4605 | const int pairs = 0; |
| 4606 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4607 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4608 | int bo = 0; /* assume native ordering by default */ |
| 4609 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4610 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4611 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4612 | int iorder[] = {0, 1, 2, 3}; |
| 4613 | #else |
| 4614 | int iorder[] = {3, 2, 1, 0}; |
| 4615 | #endif |
| 4616 | PyObject *errorHandler = NULL; |
| 4617 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4618 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4619 | q = (unsigned char *)s; |
| 4620 | e = q + size; |
| 4621 | |
| 4622 | if (byteorder) |
| 4623 | bo = *byteorder; |
| 4624 | |
| 4625 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4626 | byte order setting accordingly. In native mode, the leading BOM |
| 4627 | mark is skipped, in all other modes, it is copied to the output |
| 4628 | stream as-is (giving a ZWNBSP character). */ |
| 4629 | if (bo == 0) { |
| 4630 | if (size >= 4) { |
| 4631 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4632 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4633 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4634 | if (bom == 0x0000FEFF) { |
| 4635 | q += 4; |
| 4636 | bo = -1; |
| 4637 | } |
| 4638 | else if (bom == 0xFFFE0000) { |
| 4639 | q += 4; |
| 4640 | bo = 1; |
| 4641 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4642 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4643 | if (bom == 0x0000FEFF) { |
| 4644 | q += 4; |
| 4645 | bo = 1; |
| 4646 | } |
| 4647 | else if (bom == 0xFFFE0000) { |
| 4648 | q += 4; |
| 4649 | bo = -1; |
| 4650 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4651 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4652 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4653 | } |
| 4654 | |
| 4655 | if (bo == -1) { |
| 4656 | /* force LE */ |
| 4657 | iorder[0] = 0; |
| 4658 | iorder[1] = 1; |
| 4659 | iorder[2] = 2; |
| 4660 | iorder[3] = 3; |
| 4661 | } |
| 4662 | else if (bo == 1) { |
| 4663 | /* force BE */ |
| 4664 | iorder[0] = 3; |
| 4665 | iorder[1] = 2; |
| 4666 | iorder[2] = 1; |
| 4667 | iorder[3] = 0; |
| 4668 | } |
| 4669 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4670 | /* On narrow builds we split characters outside the BMP into two |
| 4671 | codepoints => count how much extra space we need. */ |
| 4672 | #ifndef Py_UNICODE_WIDE |
| 4673 | for (qq = q; qq < e; qq += 4) |
| 4674 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4675 | pairs++; |
| 4676 | #endif |
| 4677 | |
| 4678 | /* This might be one to much, because of a BOM */ |
| 4679 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4680 | if (!unicode) |
| 4681 | return NULL; |
| 4682 | if (size == 0) |
| 4683 | return (PyObject *)unicode; |
| 4684 | |
| 4685 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4686 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4687 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4688 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4689 | Py_UCS4 ch; |
| 4690 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4691 | if (e-q<4) { |
| 4692 | if (consumed) |
| 4693 | break; |
| 4694 | errmsg = "truncated data"; |
| 4695 | startinpos = ((const char *)q)-starts; |
| 4696 | endinpos = ((const char *)e)-starts; |
| 4697 | goto utf32Error; |
| 4698 | /* The remaining input chars are ignored if the callback |
| 4699 | chooses to skip the input */ |
| 4700 | } |
| 4701 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4702 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4703 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4704 | if (ch >= 0x110000) |
| 4705 | { |
| 4706 | errmsg = "codepoint not in range(0x110000)"; |
| 4707 | startinpos = ((const char *)q)-starts; |
| 4708 | endinpos = startinpos+4; |
| 4709 | goto utf32Error; |
| 4710 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4711 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4712 | if (ch >= 0x10000) |
| 4713 | { |
| 4714 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4715 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4716 | } |
| 4717 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4718 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4719 | *p++ = ch; |
| 4720 | q += 4; |
| 4721 | continue; |
| 4722 | utf32Error: |
| 4723 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4724 | if (unicode_decode_call_errorhandler( |
| 4725 | errors, &errorHandler, |
| 4726 | "utf32", errmsg, |
| 4727 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4728 | &unicode, &outpos, &p)) |
| 4729 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | if (byteorder) |
| 4733 | *byteorder = bo; |
| 4734 | |
| 4735 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4736 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4737 | |
| 4738 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 4739 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4740 | goto onError; |
| 4741 | |
| 4742 | Py_XDECREF(errorHandler); |
| 4743 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4744 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4745 | Py_DECREF(unicode); |
| 4746 | return NULL; |
| 4747 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4748 | return (PyObject *)unicode; |
| 4749 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4750 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4751 | Py_DECREF(unicode); |
| 4752 | Py_XDECREF(errorHandler); |
| 4753 | Py_XDECREF(exc); |
| 4754 | return NULL; |
| 4755 | } |
| 4756 | |
| 4757 | PyObject * |
| 4758 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4759 | Py_ssize_t size, |
| 4760 | const char *errors, |
| 4761 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4762 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4763 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4764 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4765 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4766 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4767 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4768 | #else |
| 4769 | const int pairs = 0; |
| 4770 | #endif |
| 4771 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4772 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4773 | int iorder[] = {0, 1, 2, 3}; |
| 4774 | #else |
| 4775 | int iorder[] = {3, 2, 1, 0}; |
| 4776 | #endif |
| 4777 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4778 | #define STORECHAR(CH) \ |
| 4779 | do { \ |
| 4780 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4781 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4782 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4783 | p[iorder[0]] = (CH) & 0xff; \ |
| 4784 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4785 | } while(0) |
| 4786 | |
| 4787 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 4788 | so we need less space. */ |
| 4789 | #ifndef Py_UNICODE_WIDE |
| 4790 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4791 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 4792 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 4793 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4794 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4795 | nsize = (size - pairs + (byteorder == 0)); |
| 4796 | bytesize = nsize * 4; |
| 4797 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4798 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4799 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4800 | if (v == NULL) |
| 4801 | return NULL; |
| 4802 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4803 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4804 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4805 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4806 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4807 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4808 | |
| 4809 | if (byteorder == -1) { |
| 4810 | /* force LE */ |
| 4811 | iorder[0] = 0; |
| 4812 | iorder[1] = 1; |
| 4813 | iorder[2] = 2; |
| 4814 | iorder[3] = 3; |
| 4815 | } |
| 4816 | else if (byteorder == 1) { |
| 4817 | /* force BE */ |
| 4818 | iorder[0] = 3; |
| 4819 | iorder[1] = 2; |
| 4820 | iorder[2] = 1; |
| 4821 | iorder[3] = 0; |
| 4822 | } |
| 4823 | |
| 4824 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4825 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4826 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4827 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 4828 | Py_UCS4 ch2 = *s; |
| 4829 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 4830 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 4831 | s++; |
| 4832 | size--; |
| 4833 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4834 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4835 | #endif |
| 4836 | STORECHAR(ch); |
| 4837 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4838 | |
| 4839 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4840 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4841 | #undef STORECHAR |
| 4842 | } |
| 4843 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4844 | PyObject * |
| 4845 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4846 | { |
| 4847 | if (!PyUnicode_Check(unicode)) { |
| 4848 | PyErr_BadArgument(); |
| 4849 | return NULL; |
| 4850 | } |
| 4851 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4852 | PyUnicode_GET_SIZE(unicode), |
| 4853 | NULL, |
| 4854 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4855 | } |
| 4856 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4857 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 4858 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4859 | PyObject * |
| 4860 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4861 | Py_ssize_t size, |
| 4862 | const char *errors, |
| 4863 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4864 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4865 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 4866 | } |
| 4867 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4868 | /* Two masks for fast checking of whether a C 'long' may contain |
| 4869 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 4870 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 4871 | rare in most input. |
| 4872 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 4873 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4874 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4875 | #if (SIZEOF_LONG == 8) |
| 4876 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 4877 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 4878 | #elif (SIZEOF_LONG == 4) |
| 4879 | # define FAST_CHAR_MASK 0x80008000L |
| 4880 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 4881 | #else |
| 4882 | # error C 'long' size should be either 4 or 8! |
| 4883 | #endif |
| 4884 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4885 | PyObject * |
| 4886 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4887 | Py_ssize_t size, |
| 4888 | const char *errors, |
| 4889 | int *byteorder, |
| 4890 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4891 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4892 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4893 | Py_ssize_t startinpos; |
| 4894 | Py_ssize_t endinpos; |
| 4895 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4896 | PyUnicodeObject *unicode; |
| 4897 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4898 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4899 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4900 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4901 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4902 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 4903 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4904 | int ihi = 1, ilo = 0; |
| 4905 | #else |
| 4906 | int ihi = 0, ilo = 1; |
| 4907 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4908 | PyObject *errorHandler = NULL; |
| 4909 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4910 | |
| 4911 | /* Note: size will always be longer than the resulting Unicode |
| 4912 | character count */ |
| 4913 | unicode = _PyUnicode_New(size); |
| 4914 | if (!unicode) |
| 4915 | return NULL; |
| 4916 | if (size == 0) |
| 4917 | return (PyObject *)unicode; |
| 4918 | |
| 4919 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4920 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4921 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4922 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4923 | |
| 4924 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4925 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4926 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4927 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4928 | byte order setting accordingly. In native mode, the leading BOM |
| 4929 | mark is skipped, in all other modes, it is copied to the output |
| 4930 | stream as-is (giving a ZWNBSP character). */ |
| 4931 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4932 | if (size >= 2) { |
| 4933 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4934 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4935 | if (bom == 0xFEFF) { |
| 4936 | q += 2; |
| 4937 | bo = -1; |
| 4938 | } |
| 4939 | else if (bom == 0xFFFE) { |
| 4940 | q += 2; |
| 4941 | bo = 1; |
| 4942 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4943 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4944 | if (bom == 0xFEFF) { |
| 4945 | q += 2; |
| 4946 | bo = 1; |
| 4947 | } |
| 4948 | else if (bom == 0xFFFE) { |
| 4949 | q += 2; |
| 4950 | bo = -1; |
| 4951 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4952 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4953 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4954 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4955 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4956 | if (bo == -1) { |
| 4957 | /* force LE */ |
| 4958 | ihi = 1; |
| 4959 | ilo = 0; |
| 4960 | } |
| 4961 | else if (bo == 1) { |
| 4962 | /* force BE */ |
| 4963 | ihi = 0; |
| 4964 | ilo = 1; |
| 4965 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4966 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4967 | native_ordering = ilo < ihi; |
| 4968 | #else |
| 4969 | native_ordering = ilo > ihi; |
| 4970 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4971 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4972 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4973 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4974 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4975 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 4976 | reads are more expensive, better to defer to another iteration. */ |
| 4977 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 4978 | /* Fast path for runs of non-surrogate chars. */ |
| 4979 | register const unsigned char *_q = q; |
| 4980 | Py_UNICODE *_p = p; |
| 4981 | if (native_ordering) { |
| 4982 | /* Native ordering is simple: as long as the input cannot |
| 4983 | possibly contain a surrogate char, do an unrolled copy |
| 4984 | of several 16-bit code points to the target object. |
| 4985 | The non-surrogate check is done on several input bytes |
| 4986 | at a time (as many as a C 'long' can contain). */ |
| 4987 | while (_q < aligned_end) { |
| 4988 | unsigned long data = * (unsigned long *) _q; |
| 4989 | if (data & FAST_CHAR_MASK) |
| 4990 | break; |
| 4991 | _p[0] = ((unsigned short *) _q)[0]; |
| 4992 | _p[1] = ((unsigned short *) _q)[1]; |
| 4993 | #if (SIZEOF_LONG == 8) |
| 4994 | _p[2] = ((unsigned short *) _q)[2]; |
| 4995 | _p[3] = ((unsigned short *) _q)[3]; |
| 4996 | #endif |
| 4997 | _q += SIZEOF_LONG; |
| 4998 | _p += SIZEOF_LONG / 2; |
| 4999 | } |
| 5000 | } |
| 5001 | else { |
| 5002 | /* Byteswapped ordering is similar, but we must decompose |
| 5003 | the copy bytewise, and take care of zero'ing out the |
| 5004 | upper bytes if the target object is in 32-bit units |
| 5005 | (that is, in UCS-4 builds). */ |
| 5006 | while (_q < aligned_end) { |
| 5007 | unsigned long data = * (unsigned long *) _q; |
| 5008 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 5009 | break; |
| 5010 | /* Zero upper bytes in UCS-4 builds */ |
| 5011 | #if (Py_UNICODE_SIZE > 2) |
| 5012 | _p[0] = 0; |
| 5013 | _p[1] = 0; |
| 5014 | #if (SIZEOF_LONG == 8) |
| 5015 | _p[2] = 0; |
| 5016 | _p[3] = 0; |
| 5017 | #endif |
| 5018 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5019 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 5020 | fill the two last bytes of each 4-byte unit. */ |
| 5021 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 5022 | # define OFF 2 |
| 5023 | #else |
| 5024 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5025 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5026 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 5027 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 5028 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 5029 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 5030 | #if (SIZEOF_LONG == 8) |
| 5031 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 5032 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 5033 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 5034 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 5035 | #endif |
| 5036 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5037 | _q += SIZEOF_LONG; |
| 5038 | _p += SIZEOF_LONG / 2; |
| 5039 | } |
| 5040 | } |
| 5041 | p = _p; |
| 5042 | q = _q; |
| 5043 | if (q >= e) |
| 5044 | break; |
| 5045 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5046 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5047 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5048 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5049 | |
| 5050 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5051 | *p++ = ch; |
| 5052 | continue; |
| 5053 | } |
| 5054 | |
| 5055 | /* UTF-16 code pair: */ |
| 5056 | if (q > e) { |
| 5057 | errmsg = "unexpected end of data"; |
| 5058 | startinpos = (((const char *)q) - 2) - starts; |
| 5059 | endinpos = ((const char *)e) + 1 - starts; |
| 5060 | goto utf16Error; |
| 5061 | } |
| 5062 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5063 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5064 | q += 2; |
| 5065 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5066 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5067 | *p++ = ch; |
| 5068 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5069 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5070 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5071 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5072 | continue; |
| 5073 | } |
| 5074 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5075 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5076 | startinpos = (((const char *)q)-4)-starts; |
| 5077 | endinpos = startinpos+2; |
| 5078 | goto utf16Error; |
| 5079 | } |
| 5080 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5081 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5082 | errmsg = "illegal encoding"; |
| 5083 | startinpos = (((const char *)q)-2)-starts; |
| 5084 | endinpos = startinpos+2; |
| 5085 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5086 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5087 | utf16Error: |
| 5088 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5089 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5090 | errors, |
| 5091 | &errorHandler, |
| 5092 | "utf16", errmsg, |
| 5093 | &starts, |
| 5094 | (const char **)&e, |
| 5095 | &startinpos, |
| 5096 | &endinpos, |
| 5097 | &exc, |
| 5098 | (const char **)&q, |
| 5099 | &unicode, |
| 5100 | &outpos, |
| 5101 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5102 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5103 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5104 | /* remaining byte at the end? (size should be even) */ |
| 5105 | if (e == q) { |
| 5106 | if (!consumed) { |
| 5107 | errmsg = "truncated data"; |
| 5108 | startinpos = ((const char *)q) - starts; |
| 5109 | endinpos = ((const char *)e) + 1 - starts; |
| 5110 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5111 | if (unicode_decode_call_errorhandler( |
| 5112 | errors, |
| 5113 | &errorHandler, |
| 5114 | "utf16", errmsg, |
| 5115 | &starts, |
| 5116 | (const char **)&e, |
| 5117 | &startinpos, |
| 5118 | &endinpos, |
| 5119 | &exc, |
| 5120 | (const char **)&q, |
| 5121 | &unicode, |
| 5122 | &outpos, |
| 5123 | &p)) |
| 5124 | goto onError; |
| 5125 | /* The remaining input chars are ignored if the callback |
| 5126 | chooses to skip the input */ |
| 5127 | } |
| 5128 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5129 | |
| 5130 | if (byteorder) |
| 5131 | *byteorder = bo; |
| 5132 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5133 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5134 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5136 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5137 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5138 | goto onError; |
| 5139 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5140 | Py_XDECREF(errorHandler); |
| 5141 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5142 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5143 | Py_DECREF(unicode); |
| 5144 | return NULL; |
| 5145 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5146 | return (PyObject *)unicode; |
| 5147 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5148 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5149 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5150 | Py_XDECREF(errorHandler); |
| 5151 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5152 | return NULL; |
| 5153 | } |
| 5154 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5155 | #undef FAST_CHAR_MASK |
| 5156 | #undef SWAPPED_FAST_CHAR_MASK |
| 5157 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5158 | PyObject * |
| 5159 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5160 | Py_ssize_t size, |
| 5161 | const char *errors, |
| 5162 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5163 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5164 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5165 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5166 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5167 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5168 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5169 | #else |
| 5170 | const int pairs = 0; |
| 5171 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5172 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5173 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5174 | int ihi = 1, ilo = 0; |
| 5175 | #else |
| 5176 | int ihi = 0, ilo = 1; |
| 5177 | #endif |
| 5178 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5179 | #define STORECHAR(CH) \ |
| 5180 | do { \ |
| 5181 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5182 | p[ilo] = (CH) & 0xff; \ |
| 5183 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5184 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5185 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5186 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5187 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5188 | if (s[i] >= 0x10000) |
| 5189 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5190 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5191 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5192 | if (size > PY_SSIZE_T_MAX || |
| 5193 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5194 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5195 | nsize = size + pairs + (byteorder == 0); |
| 5196 | bytesize = nsize * 2; |
| 5197 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5198 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5199 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5200 | if (v == NULL) |
| 5201 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5202 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5203 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5204 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5205 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5206 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5207 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5208 | |
| 5209 | if (byteorder == -1) { |
| 5210 | /* force LE */ |
| 5211 | ihi = 1; |
| 5212 | ilo = 0; |
| 5213 | } |
| 5214 | else if (byteorder == 1) { |
| 5215 | /* force BE */ |
| 5216 | ihi = 0; |
| 5217 | ilo = 1; |
| 5218 | } |
| 5219 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5220 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5221 | Py_UNICODE ch = *s++; |
| 5222 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5223 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5224 | if (ch >= 0x10000) { |
| 5225 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5226 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5227 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5228 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5229 | STORECHAR(ch); |
| 5230 | if (ch2) |
| 5231 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5232 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5233 | |
| 5234 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5235 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5236 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5239 | PyObject * |
| 5240 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5241 | { |
| 5242 | if (!PyUnicode_Check(unicode)) { |
| 5243 | PyErr_BadArgument(); |
| 5244 | return NULL; |
| 5245 | } |
| 5246 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5247 | PyUnicode_GET_SIZE(unicode), |
| 5248 | NULL, |
| 5249 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5250 | } |
| 5251 | |
| 5252 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5253 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5254 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5255 | if all the escapes in the string make it still a valid ASCII string. |
| 5256 | Returns -1 if any escapes were found which cause the string to |
| 5257 | pop out of ASCII range. Otherwise returns the length of the |
| 5258 | required buffer to hold the string. |
| 5259 | */ |
| 5260 | Py_ssize_t |
| 5261 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5262 | { |
| 5263 | const unsigned char *p = (const unsigned char *)s; |
| 5264 | const unsigned char *end = p + size; |
| 5265 | Py_ssize_t length = 0; |
| 5266 | |
| 5267 | if (size < 0) |
| 5268 | return -1; |
| 5269 | |
| 5270 | for (; p < end; ++p) { |
| 5271 | if (*p > 127) { |
| 5272 | /* Non-ASCII */ |
| 5273 | return -1; |
| 5274 | } |
| 5275 | else if (*p != '\\') { |
| 5276 | /* Normal character */ |
| 5277 | ++length; |
| 5278 | } |
| 5279 | else { |
| 5280 | /* Backslash-escape, check next char */ |
| 5281 | ++p; |
| 5282 | /* Escape sequence reaches till end of string or |
| 5283 | non-ASCII follow-up. */ |
| 5284 | if (p >= end || *p > 127) |
| 5285 | return -1; |
| 5286 | switch (*p) { |
| 5287 | case '\n': |
| 5288 | /* backslash + \n result in zero characters */ |
| 5289 | break; |
| 5290 | case '\\': case '\'': case '\"': |
| 5291 | case 'b': case 'f': case 't': |
| 5292 | case 'n': case 'r': case 'v': case 'a': |
| 5293 | ++length; |
| 5294 | break; |
| 5295 | case '0': case '1': case '2': case '3': |
| 5296 | case '4': case '5': case '6': case '7': |
| 5297 | case 'x': case 'u': case 'U': case 'N': |
| 5298 | /* these do not guarantee ASCII characters */ |
| 5299 | return -1; |
| 5300 | default: |
| 5301 | /* count the backslash + the other character */ |
| 5302 | length += 2; |
| 5303 | } |
| 5304 | } |
| 5305 | } |
| 5306 | return length; |
| 5307 | } |
| 5308 | |
| 5309 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5310 | or treat string as ASCII. */ |
| 5311 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5312 | do { \ |
| 5313 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5314 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5315 | else \ |
| 5316 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5317 | } while (0) |
| 5318 | |
| 5319 | #define WRITE_WSTR(buf, index, value) \ |
| 5320 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5321 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5322 | |
| 5323 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5324 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5325 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5326 | PyObject * |
| 5327 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5328 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5329 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5330 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5331 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5332 | Py_ssize_t startinpos; |
| 5333 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5334 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5335 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5336 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5337 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5338 | char* message; |
| 5339 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5340 | PyObject *errorHandler = NULL; |
| 5341 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5342 | Py_ssize_t ascii_length; |
| 5343 | Py_ssize_t i; |
| 5344 | int kind; |
| 5345 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5346 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5347 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5348 | |
| 5349 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5350 | either the string is pure ASCII with named escapes like \n, etc. |
| 5351 | and we determined it's exact size (common case) |
| 5352 | or it contains \x, \u, ... escape sequences. then we create a |
| 5353 | legacy wchar string and resize it at the end of this function. */ |
| 5354 | if (ascii_length >= 0) { |
| 5355 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 5356 | if (!v) |
| 5357 | goto onError; |
| 5358 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5359 | kind = PyUnicode_1BYTE_KIND; |
| 5360 | data = PyUnicode_DATA(v); |
| 5361 | } |
| 5362 | else { |
| 5363 | /* Escaped strings will always be longer than the resulting |
| 5364 | Unicode string, so we start with size here and then reduce the |
| 5365 | length after conversion to the true value. |
| 5366 | (but if the error callback returns a long replacement string |
| 5367 | we'll have to allocate more space) */ |
| 5368 | v = _PyUnicode_New(size); |
| 5369 | if (!v) |
| 5370 | goto onError; |
| 5371 | kind = PyUnicode_WCHAR_KIND; |
| 5372 | data = PyUnicode_AS_UNICODE(v); |
| 5373 | } |
| 5374 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5375 | if (size == 0) |
| 5376 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5377 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5378 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5379 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5380 | while (s < end) { |
| 5381 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5382 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5383 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5384 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5385 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5386 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5387 | } |
| 5388 | else { |
| 5389 | /* The only case in which i == ascii_length is a backslash |
| 5390 | followed by a newline. */ |
| 5391 | assert(i <= ascii_length); |
| 5392 | } |
| 5393 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5394 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5395 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5396 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5397 | continue; |
| 5398 | } |
| 5399 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5400 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5401 | /* \ - Escapes */ |
| 5402 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5403 | c = *s++; |
| 5404 | if (s > end) |
| 5405 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5406 | |
| 5407 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5408 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5409 | } |
| 5410 | else { |
| 5411 | /* The only case in which i == ascii_length is a backslash |
| 5412 | followed by a newline. */ |
| 5413 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5414 | } |
| 5415 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5416 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5417 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5418 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5419 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5420 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5421 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5422 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5423 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5424 | /* FF */ |
| 5425 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5426 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5427 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5428 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5429 | /* VT */ |
| 5430 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5431 | /* BEL, not classic C */ |
| 5432 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5433 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5434 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5435 | case '0': case '1': case '2': case '3': |
| 5436 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5437 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5438 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5439 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5440 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5441 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5442 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5443 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5444 | break; |
| 5445 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5446 | /* hex escapes */ |
| 5447 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5448 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5449 | digits = 2; |
| 5450 | message = "truncated \\xXX escape"; |
| 5451 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5452 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5453 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5454 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5455 | digits = 4; |
| 5456 | message = "truncated \\uXXXX escape"; |
| 5457 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5458 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5459 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5460 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5461 | digits = 8; |
| 5462 | message = "truncated \\UXXXXXXXX escape"; |
| 5463 | hexescape: |
| 5464 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5465 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5466 | if (s+digits>end) { |
| 5467 | endinpos = size; |
| 5468 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5469 | errors, &errorHandler, |
| 5470 | "unicodeescape", "end of string in escape sequence", |
| 5471 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5472 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5473 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5474 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5475 | goto nextByte; |
| 5476 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5477 | for (j = 0; j < digits; ++j) { |
| 5478 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5479 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5480 | endinpos = (s+j+1)-starts; |
| 5481 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5482 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5483 | errors, &errorHandler, |
| 5484 | "unicodeescape", message, |
| 5485 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5486 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5487 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5488 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5489 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5490 | } |
| 5491 | chr = (chr<<4) & ~0xF; |
| 5492 | if (c >= '0' && c <= '9') |
| 5493 | chr += c - '0'; |
| 5494 | else if (c >= 'a' && c <= 'f') |
| 5495 | chr += 10 + c - 'a'; |
| 5496 | else |
| 5497 | chr += 10 + c - 'A'; |
| 5498 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5499 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5500 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5501 | /* _decoding_error will have already written into the |
| 5502 | target buffer. */ |
| 5503 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5504 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5505 | /* when we get here, chr is a 32-bit unicode character */ |
| 5506 | if (chr <= 0xffff) |
| 5507 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5508 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5509 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5510 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5511 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5512 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5513 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5514 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5515 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5516 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5517 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5518 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5519 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5520 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5521 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5522 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5523 | errors, &errorHandler, |
| 5524 | "unicodeescape", "illegal Unicode character", |
| 5525 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5526 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5527 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5528 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5529 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5530 | break; |
| 5531 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5532 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5533 | case 'N': |
| 5534 | message = "malformed \\N character escape"; |
| 5535 | if (ucnhash_CAPI == NULL) { |
| 5536 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5537 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5538 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5539 | if (ucnhash_CAPI == NULL) |
| 5540 | goto ucnhashError; |
| 5541 | } |
| 5542 | if (*s == '{') { |
| 5543 | const char *start = s+1; |
| 5544 | /* look for the closing brace */ |
| 5545 | while (*s != '}' && s < end) |
| 5546 | s++; |
| 5547 | if (s > start && s < end && *s == '}') { |
| 5548 | /* found a name. look it up in the unicode database */ |
| 5549 | message = "unknown Unicode character name"; |
| 5550 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5551 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5552 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5553 | goto store; |
| 5554 | } |
| 5555 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5556 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5557 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5558 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5559 | errors, &errorHandler, |
| 5560 | "unicodeescape", message, |
| 5561 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5562 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5563 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5564 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5565 | break; |
| 5566 | |
| 5567 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5568 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5569 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5570 | message = "\\ at end of string"; |
| 5571 | s--; |
| 5572 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5573 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5574 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5575 | errors, &errorHandler, |
| 5576 | "unicodeescape", message, |
| 5577 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5578 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5579 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5580 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5581 | } |
| 5582 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5583 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5584 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5585 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5586 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5587 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5588 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5589 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5590 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5591 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5592 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5593 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5594 | if (kind == PyUnicode_WCHAR_KIND) |
| 5595 | { |
| 5596 | if (PyUnicode_Resize((PyObject**)&v, i) < 0) |
| 5597 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5598 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5599 | Py_XDECREF(errorHandler); |
| 5600 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5601 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5602 | Py_DECREF(v); |
| 5603 | return NULL; |
| 5604 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5605 | return (PyObject *)v; |
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 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5612 | Py_XDECREF(v); |
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: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | Py_XDECREF(v); |
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 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5624 | #undef WRITE_ASCII_OR_WSTR |
| 5625 | #undef WRITE_WSTR |
| 5626 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5628 | |
| 5629 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5630 | appropriate. |
| 5631 | |
| 5632 | */ |
| 5633 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5634 | static const char *hexdigits = "0123456789abcdef"; |
| 5635 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5636 | PyObject * |
| 5637 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5638 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5640 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5642 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5643 | #ifdef Py_UNICODE_WIDE |
| 5644 | const Py_ssize_t expandsize = 10; |
| 5645 | #else |
| 5646 | const Py_ssize_t expandsize = 6; |
| 5647 | #endif |
| 5648 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5649 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5650 | better to choose a different scheme. Perhaps scan the |
| 5651 | first N-chars of the string and allocate based on that size. |
| 5652 | */ |
| 5653 | /* Initial allocation is based on the longest-possible unichr |
| 5654 | escape. |
| 5655 | |
| 5656 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5657 | unichr, so in this case it's the longest unichr escape. In |
| 5658 | narrow (UTF-16) builds this is five chars per source unichr |
| 5659 | since there are two unichrs in the surrogate pair, so in narrow |
| 5660 | (UTF-16) builds it's not the longest unichr escape. |
| 5661 | |
| 5662 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5663 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5664 | escape. |
| 5665 | */ |
| 5666 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5667 | if (size == 0) |
| 5668 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5669 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5670 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5671 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5672 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5673 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5674 | 2 |
| 5675 | + expandsize*size |
| 5676 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5677 | if (repr == NULL) |
| 5678 | return NULL; |
| 5679 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5680 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5681 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5682 | while (size-- > 0) { |
| 5683 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5684 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5685 | /* Escape backslashes */ |
| 5686 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | *p++ = '\\'; |
| 5688 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5689 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5690 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5691 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5692 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5693 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5694 | else if (ch >= 0x10000) { |
| 5695 | *p++ = '\\'; |
| 5696 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5697 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5698 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5699 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5700 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5701 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5702 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5703 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5704 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5705 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5706 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5707 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5708 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5709 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5710 | Py_UNICODE ch2; |
| 5711 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5712 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5713 | ch2 = *s++; |
| 5714 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5715 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5716 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5717 | *p++ = '\\'; |
| 5718 | *p++ = 'U'; |
| 5719 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5720 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5721 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5722 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5723 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5724 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5725 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5726 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5727 | continue; |
| 5728 | } |
| 5729 | /* Fall through: isolated surrogates are copied as-is */ |
| 5730 | s--; |
| 5731 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5732 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5733 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5734 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5736 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | *p++ = '\\'; |
| 5738 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5739 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5740 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5741 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5742 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5743 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5744 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5745 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5746 | else if (ch == '\t') { |
| 5747 | *p++ = '\\'; |
| 5748 | *p++ = 't'; |
| 5749 | } |
| 5750 | else if (ch == '\n') { |
| 5751 | *p++ = '\\'; |
| 5752 | *p++ = 'n'; |
| 5753 | } |
| 5754 | else if (ch == '\r') { |
| 5755 | *p++ = '\\'; |
| 5756 | *p++ = 'r'; |
| 5757 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5758 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5759 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5760 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5761 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5762 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5763 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5764 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5765 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5766 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5767 | /* Copy everything else as-is */ |
| 5768 | else |
| 5769 | *p++ = (char) ch; |
| 5770 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5771 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5772 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5773 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5774 | return NULL; |
| 5775 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5776 | } |
| 5777 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5778 | PyObject * |
| 5779 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5781 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5782 | if (!PyUnicode_Check(unicode)) { |
| 5783 | PyErr_BadArgument(); |
| 5784 | return NULL; |
| 5785 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5786 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5787 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5788 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5789 | } |
| 5790 | |
| 5791 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5792 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5793 | PyObject * |
| 5794 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5795 | Py_ssize_t size, |
| 5796 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5797 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5798 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5799 | Py_ssize_t startinpos; |
| 5800 | Py_ssize_t endinpos; |
| 5801 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5802 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5803 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5804 | const char *end; |
| 5805 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5806 | PyObject *errorHandler = NULL; |
| 5807 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5808 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5809 | /* Escaped strings will always be longer than the resulting |
| 5810 | 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] | 5811 | length after conversion to the true value. (But decoding error |
| 5812 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5813 | v = _PyUnicode_New(size); |
| 5814 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5815 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5816 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5817 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5818 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5819 | end = s + size; |
| 5820 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5821 | unsigned char c; |
| 5822 | Py_UCS4 x; |
| 5823 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5824 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5825 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5826 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5827 | if (*s != '\\') { |
| 5828 | *p++ = (unsigned char)*s++; |
| 5829 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5830 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5831 | startinpos = s-starts; |
| 5832 | |
| 5833 | /* \u-escapes are only interpreted iff the number of leading |
| 5834 | backslashes if odd */ |
| 5835 | bs = s; |
| 5836 | for (;s < end;) { |
| 5837 | if (*s != '\\') |
| 5838 | break; |
| 5839 | *p++ = (unsigned char)*s++; |
| 5840 | } |
| 5841 | if (((s - bs) & 1) == 0 || |
| 5842 | s >= end || |
| 5843 | (*s != 'u' && *s != 'U')) { |
| 5844 | continue; |
| 5845 | } |
| 5846 | p--; |
| 5847 | count = *s=='u' ? 4 : 8; |
| 5848 | s++; |
| 5849 | |
| 5850 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 5851 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5852 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5853 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5854 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5855 | endinpos = s-starts; |
| 5856 | if (unicode_decode_call_errorhandler( |
| 5857 | errors, &errorHandler, |
| 5858 | "rawunicodeescape", "truncated \\uXXXX", |
| 5859 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5860 | &v, &outpos, &p)) |
| 5861 | goto onError; |
| 5862 | goto nextByte; |
| 5863 | } |
| 5864 | x = (x<<4) & ~0xF; |
| 5865 | if (c >= '0' && c <= '9') |
| 5866 | x += c - '0'; |
| 5867 | else if (c >= 'a' && c <= 'f') |
| 5868 | x += 10 + c - 'a'; |
| 5869 | else |
| 5870 | x += 10 + c - 'A'; |
| 5871 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5872 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5873 | /* UCS-2 character */ |
| 5874 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5875 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5876 | /* UCS-4 character. Either store directly, or as |
| 5877 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5878 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5879 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5880 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5881 | x -= 0x10000L; |
| 5882 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 5883 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5884 | #endif |
| 5885 | } else { |
| 5886 | endinpos = s-starts; |
| 5887 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5888 | if (unicode_decode_call_errorhandler( |
| 5889 | errors, &errorHandler, |
| 5890 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5891 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5892 | &v, &outpos, &p)) |
| 5893 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5894 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5895 | nextByte: |
| 5896 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5898 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5899 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5900 | Py_XDECREF(errorHandler); |
| 5901 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5902 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5903 | Py_DECREF(v); |
| 5904 | return NULL; |
| 5905 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5906 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5907 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5908 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5909 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5910 | Py_XDECREF(errorHandler); |
| 5911 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 | return NULL; |
| 5913 | } |
| 5914 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5915 | PyObject * |
| 5916 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5917 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5918 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5919 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | char *p; |
| 5921 | char *q; |
| 5922 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5923 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5924 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5925 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5926 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5927 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5928 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5929 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5930 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5931 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5932 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5933 | if (repr == NULL) |
| 5934 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5935 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5936 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5937 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5938 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5939 | while (size-- > 0) { |
| 5940 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5941 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5942 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5943 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5944 | *p++ = '\\'; |
| 5945 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5946 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 5947 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 5948 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 5949 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 5950 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5951 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5952 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5953 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5954 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5955 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5956 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5957 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5958 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 5959 | Py_UNICODE ch2; |
| 5960 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5961 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5962 | ch2 = *s++; |
| 5963 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5964 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5965 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5966 | *p++ = '\\'; |
| 5967 | *p++ = 'U'; |
| 5968 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 5969 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 5970 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 5971 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 5972 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 5973 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 5974 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 5975 | *p++ = hexdigits[ucs & 0xf]; |
| 5976 | continue; |
| 5977 | } |
| 5978 | /* Fall through: isolated surrogates are copied as-is */ |
| 5979 | s--; |
| 5980 | size++; |
| 5981 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5982 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5983 | /* Map 16-bit characters to '\uxxxx' */ |
| 5984 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5985 | *p++ = '\\'; |
| 5986 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5987 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5988 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5989 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5990 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5991 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5992 | /* Copy everything else as-is */ |
| 5993 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5994 | *p++ = (char) ch; |
| 5995 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5996 | size = p - q; |
| 5997 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5998 | assert(size > 0); |
| 5999 | if (_PyBytes_Resize(&repr, size) < 0) |
| 6000 | return NULL; |
| 6001 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6002 | } |
| 6003 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6004 | PyObject * |
| 6005 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6006 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6007 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6008 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6009 | PyErr_BadArgument(); |
| 6010 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6011 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6012 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6013 | PyUnicode_GET_SIZE(unicode)); |
| 6014 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6015 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | } |
| 6017 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6018 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6019 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6020 | PyObject * |
| 6021 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6022 | Py_ssize_t size, |
| 6023 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6024 | { |
| 6025 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6026 | Py_ssize_t startinpos; |
| 6027 | Py_ssize_t endinpos; |
| 6028 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6029 | PyUnicodeObject *v; |
| 6030 | Py_UNICODE *p; |
| 6031 | const char *end; |
| 6032 | const char *reason; |
| 6033 | PyObject *errorHandler = NULL; |
| 6034 | PyObject *exc = NULL; |
| 6035 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6036 | #ifdef Py_UNICODE_WIDE |
| 6037 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 6038 | #endif |
| 6039 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6040 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6041 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 6042 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6043 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6044 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 6045 | as string was created with the old API. */ |
| 6046 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6047 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6048 | p = PyUnicode_AS_UNICODE(v); |
| 6049 | end = s + size; |
| 6050 | |
| 6051 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6052 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6053 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6054 | some malformed UCS-4 data. */ |
| 6055 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6056 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6057 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6058 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6059 | end-s < Py_UNICODE_SIZE |
| 6060 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6061 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6062 | startinpos = s - starts; |
| 6063 | if (end-s < Py_UNICODE_SIZE) { |
| 6064 | endinpos = end-starts; |
| 6065 | reason = "truncated input"; |
| 6066 | } |
| 6067 | else { |
| 6068 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6069 | reason = "illegal code point (> 0x10FFFF)"; |
| 6070 | } |
| 6071 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6072 | if (unicode_decode_call_errorhandler( |
| 6073 | errors, &errorHandler, |
| 6074 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6075 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6076 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6077 | goto onError; |
| 6078 | } |
| 6079 | } |
| 6080 | else { |
| 6081 | p++; |
| 6082 | s += Py_UNICODE_SIZE; |
| 6083 | } |
| 6084 | } |
| 6085 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6086 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6087 | goto onError; |
| 6088 | Py_XDECREF(errorHandler); |
| 6089 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6090 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6091 | Py_DECREF(v); |
| 6092 | return NULL; |
| 6093 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6094 | return (PyObject *)v; |
| 6095 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6096 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6097 | Py_XDECREF(v); |
| 6098 | Py_XDECREF(errorHandler); |
| 6099 | Py_XDECREF(exc); |
| 6100 | return NULL; |
| 6101 | } |
| 6102 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6103 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6104 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6105 | PyObject * |
| 6106 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6107 | Py_ssize_t size, |
| 6108 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6110 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6111 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6112 | } |
| 6113 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6114 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6115 | static void |
| 6116 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6117 | const char *encoding, |
| 6118 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6119 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6120 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6121 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6122 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6123 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6124 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6125 | } |
| 6126 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6127 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6128 | goto onError; |
| 6129 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6130 | goto onError; |
| 6131 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6132 | goto onError; |
| 6133 | return; |
| 6134 | onError: |
| 6135 | Py_DECREF(*exceptionObject); |
| 6136 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | } |
| 6138 | } |
| 6139 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6140 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6141 | static void |
| 6142 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6143 | const char *encoding, |
| 6144 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6145 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6146 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6147 | { |
| 6148 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6149 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6150 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6151 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | /* error handling callback helper: |
| 6155 | build arguments, call the callback and check the arguments, |
| 6156 | put the result into newpos and return the replacement string, which |
| 6157 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6158 | static PyObject * |
| 6159 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6160 | PyObject **errorHandler, |
| 6161 | const char *encoding, const char *reason, |
| 6162 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 6163 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6164 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6165 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6166 | static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6167 | |
| 6168 | PyObject *restuple; |
| 6169 | PyObject *resunicode; |
| 6170 | |
| 6171 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6172 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6173 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6174 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6178 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6179 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6180 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6181 | |
| 6182 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6183 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6184 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6185 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6186 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6187 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6188 | Py_DECREF(restuple); |
| 6189 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6190 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6191 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6192 | &resunicode, newpos)) { |
| 6193 | Py_DECREF(restuple); |
| 6194 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6195 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6196 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6197 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6198 | Py_DECREF(restuple); |
| 6199 | return NULL; |
| 6200 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6201 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6202 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6203 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6204 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6205 | Py_DECREF(restuple); |
| 6206 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6207 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6208 | Py_INCREF(resunicode); |
| 6209 | Py_DECREF(restuple); |
| 6210 | return resunicode; |
| 6211 | } |
| 6212 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6213 | static PyObject * |
| 6214 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6215 | Py_ssize_t size, |
| 6216 | const char *errors, |
| 6217 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6218 | { |
| 6219 | /* output object */ |
| 6220 | PyObject *res; |
| 6221 | /* pointers to the beginning and end+1 of input */ |
| 6222 | const Py_UNICODE *startp = p; |
| 6223 | const Py_UNICODE *endp = p + size; |
| 6224 | /* pointer to the beginning of the unencodable characters */ |
| 6225 | /* const Py_UNICODE *badp = NULL; */ |
| 6226 | /* pointer into the output */ |
| 6227 | char *str; |
| 6228 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6229 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6230 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6231 | 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] | 6232 | PyObject *errorHandler = NULL; |
| 6233 | PyObject *exc = NULL; |
| 6234 | /* the following variable is used for caching string comparisons |
| 6235 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6236 | int known_errorHandler = -1; |
| 6237 | |
| 6238 | /* allocate enough for a simple encoding without |
| 6239 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6240 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6241 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6242 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6243 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6244 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6245 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6246 | ressize = size; |
| 6247 | |
| 6248 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6249 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6250 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6251 | /* can we encode this? */ |
| 6252 | if (c<limit) { |
| 6253 | /* no overflow check, because we know that the space is enough */ |
| 6254 | *str++ = (char)c; |
| 6255 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6256 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6257 | else { |
| 6258 | Py_ssize_t unicodepos = p-startp; |
| 6259 | Py_ssize_t requiredsize; |
| 6260 | PyObject *repunicode; |
| 6261 | Py_ssize_t repsize; |
| 6262 | Py_ssize_t newpos; |
| 6263 | Py_ssize_t respos; |
| 6264 | Py_UNICODE *uni2; |
| 6265 | /* startpos for collecting unencodable chars */ |
| 6266 | const Py_UNICODE *collstart = p; |
| 6267 | const Py_UNICODE *collend = p; |
| 6268 | /* find all unecodable characters */ |
| 6269 | while ((collend < endp) && ((*collend)>=limit)) |
| 6270 | ++collend; |
| 6271 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6272 | if (known_errorHandler==-1) { |
| 6273 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6274 | known_errorHandler = 1; |
| 6275 | else if (!strcmp(errors, "replace")) |
| 6276 | known_errorHandler = 2; |
| 6277 | else if (!strcmp(errors, "ignore")) |
| 6278 | known_errorHandler = 3; |
| 6279 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6280 | known_errorHandler = 4; |
| 6281 | else |
| 6282 | known_errorHandler = 0; |
| 6283 | } |
| 6284 | switch (known_errorHandler) { |
| 6285 | case 1: /* strict */ |
| 6286 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 6287 | goto onError; |
| 6288 | case 2: /* replace */ |
| 6289 | while (collstart++<collend) |
| 6290 | *str++ = '?'; /* fall through */ |
| 6291 | case 3: /* ignore */ |
| 6292 | p = collend; |
| 6293 | break; |
| 6294 | case 4: /* xmlcharrefreplace */ |
| 6295 | respos = str - PyBytes_AS_STRING(res); |
| 6296 | /* determine replacement size (temporarily (mis)uses p) */ |
| 6297 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 6298 | if (*p<10) |
| 6299 | repsize += 2+1+1; |
| 6300 | else if (*p<100) |
| 6301 | repsize += 2+2+1; |
| 6302 | else if (*p<1000) |
| 6303 | repsize += 2+3+1; |
| 6304 | else if (*p<10000) |
| 6305 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6306 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6307 | else |
| 6308 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6309 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6310 | else if (*p<100000) |
| 6311 | repsize += 2+5+1; |
| 6312 | else if (*p<1000000) |
| 6313 | repsize += 2+6+1; |
| 6314 | else |
| 6315 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6316 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6317 | } |
| 6318 | requiredsize = respos+repsize+(endp-collend); |
| 6319 | if (requiredsize > ressize) { |
| 6320 | if (requiredsize<2*ressize) |
| 6321 | requiredsize = 2*ressize; |
| 6322 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6323 | goto onError; |
| 6324 | str = PyBytes_AS_STRING(res) + respos; |
| 6325 | ressize = requiredsize; |
| 6326 | } |
| 6327 | /* generate replacement (temporarily (mis)uses p) */ |
| 6328 | for (p = collstart; p < collend; ++p) { |
| 6329 | str += sprintf(str, "&#%d;", (int)*p); |
| 6330 | } |
| 6331 | p = collend; |
| 6332 | break; |
| 6333 | default: |
| 6334 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6335 | encoding, reason, startp, size, &exc, |
| 6336 | collstart-startp, collend-startp, &newpos); |
| 6337 | if (repunicode == NULL) |
| 6338 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6339 | if (PyBytes_Check(repunicode)) { |
| 6340 | /* Directly copy bytes result to output. */ |
| 6341 | repsize = PyBytes_Size(repunicode); |
| 6342 | if (repsize > 1) { |
| 6343 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6344 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6345 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6346 | Py_DECREF(repunicode); |
| 6347 | goto onError; |
| 6348 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6349 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6350 | ressize += repsize-1; |
| 6351 | } |
| 6352 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6353 | str += repsize; |
| 6354 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6355 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6356 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6357 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6358 | /* need more space? (at least enough for what we |
| 6359 | have+the replacement+the rest of the string, so |
| 6360 | we won't have to check space for encodable characters) */ |
| 6361 | respos = str - PyBytes_AS_STRING(res); |
| 6362 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6363 | requiredsize = respos+repsize+(endp-collend); |
| 6364 | if (requiredsize > ressize) { |
| 6365 | if (requiredsize<2*ressize) |
| 6366 | requiredsize = 2*ressize; |
| 6367 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6368 | Py_DECREF(repunicode); |
| 6369 | goto onError; |
| 6370 | } |
| 6371 | str = PyBytes_AS_STRING(res) + respos; |
| 6372 | ressize = requiredsize; |
| 6373 | } |
| 6374 | /* check if there is anything unencodable in the replacement |
| 6375 | and copy it to the output */ |
| 6376 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6377 | c = *uni2; |
| 6378 | if (c >= limit) { |
| 6379 | raise_encode_exception(&exc, encoding, startp, size, |
| 6380 | unicodepos, unicodepos+1, reason); |
| 6381 | Py_DECREF(repunicode); |
| 6382 | goto onError; |
| 6383 | } |
| 6384 | *str = (char)c; |
| 6385 | } |
| 6386 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6387 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6388 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6389 | } |
| 6390 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6391 | /* Resize if we allocated to much */ |
| 6392 | size = str - PyBytes_AS_STRING(res); |
| 6393 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6394 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6395 | if (_PyBytes_Resize(&res, size) < 0) |
| 6396 | goto onError; |
| 6397 | } |
| 6398 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6399 | Py_XDECREF(errorHandler); |
| 6400 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6401 | return res; |
| 6402 | |
| 6403 | onError: |
| 6404 | Py_XDECREF(res); |
| 6405 | Py_XDECREF(errorHandler); |
| 6406 | Py_XDECREF(exc); |
| 6407 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6408 | } |
| 6409 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6410 | PyObject * |
| 6411 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6412 | Py_ssize_t size, |
| 6413 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6414 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6415 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6418 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6419 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6420 | { |
| 6421 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6422 | PyErr_BadArgument(); |
| 6423 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6424 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6425 | if (PyUnicode_READY(unicode) == -1) |
| 6426 | return NULL; |
| 6427 | /* Fast path: if it is a one-byte string, construct |
| 6428 | bytes object directly. */ |
| 6429 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6430 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6431 | PyUnicode_GET_LENGTH(unicode)); |
| 6432 | /* Non-Latin-1 characters present. Defer to above function to |
| 6433 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6434 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6435 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6436 | errors); |
| 6437 | } |
| 6438 | |
| 6439 | PyObject* |
| 6440 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6441 | { |
| 6442 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6443 | } |
| 6444 | |
| 6445 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6446 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6447 | PyObject * |
| 6448 | PyUnicode_DecodeASCII(const char *s, |
| 6449 | Py_ssize_t size, |
| 6450 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6451 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6452 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6453 | PyUnicodeObject *v; |
| 6454 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6455 | Py_ssize_t startinpos; |
| 6456 | Py_ssize_t endinpos; |
| 6457 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6458 | const char *e; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6459 | unsigned char* d; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6460 | PyObject *errorHandler = NULL; |
| 6461 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6462 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6463 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6464 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6465 | if (size == 1 && *(unsigned char*)s < 128) |
| 6466 | return PyUnicode_FromOrdinal(*(unsigned char*)s); |
| 6467 | |
| 6468 | /* Fast path. Assume the input actually *is* ASCII, and allocate |
| 6469 | a single-block Unicode object with that assumption. If there is |
| 6470 | an error, drop the object and start over. */ |
| 6471 | v = (PyUnicodeObject*)PyUnicode_New(size, 127); |
| 6472 | if (v == NULL) |
| 6473 | goto onError; |
| 6474 | d = PyUnicode_1BYTE_DATA(v); |
| 6475 | for (i = 0; i < size; i++) { |
| 6476 | unsigned char ch = ((unsigned char*)s)[i]; |
| 6477 | if (ch < 128) |
| 6478 | d[i] = ch; |
| 6479 | else |
| 6480 | break; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6481 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6482 | if (i == size) |
| 6483 | return (PyObject*)v; |
| 6484 | Py_DECREF(v); /* start over */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6486 | v = _PyUnicode_New(size); |
| 6487 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6488 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6489 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6490 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6491 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6492 | e = s + size; |
| 6493 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6494 | register unsigned char c = (unsigned char)*s; |
| 6495 | if (c < 128) { |
| 6496 | *p++ = c; |
| 6497 | ++s; |
| 6498 | } |
| 6499 | else { |
| 6500 | startinpos = s-starts; |
| 6501 | endinpos = startinpos + 1; |
| 6502 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 6503 | if (unicode_decode_call_errorhandler( |
| 6504 | errors, &errorHandler, |
| 6505 | "ascii", "ordinal not in range(128)", |
| 6506 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6507 | &v, &outpos, &p)) |
| 6508 | goto onError; |
| 6509 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6510 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6511 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6512 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6513 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6514 | Py_XDECREF(errorHandler); |
| 6515 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6516 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6517 | Py_DECREF(v); |
| 6518 | return NULL; |
| 6519 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6520 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6521 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6522 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6523 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6524 | Py_XDECREF(errorHandler); |
| 6525 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6526 | return NULL; |
| 6527 | } |
| 6528 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6529 | PyObject * |
| 6530 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6531 | Py_ssize_t size, |
| 6532 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6533 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6534 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6537 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6538 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6539 | { |
| 6540 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6541 | PyErr_BadArgument(); |
| 6542 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6543 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6544 | if (PyUnicode_READY(unicode) == -1) |
| 6545 | return NULL; |
| 6546 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6547 | directly. Else defer to above function to raise the exception. */ |
| 6548 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6549 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6550 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6551 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6552 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6553 | errors); |
| 6554 | } |
| 6555 | |
| 6556 | PyObject * |
| 6557 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6558 | { |
| 6559 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6560 | } |
| 6561 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6562 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6563 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6564 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6565 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6566 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6567 | #define NEED_RETRY |
| 6568 | #endif |
| 6569 | |
| 6570 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6571 | a) it assumes an incomplete character consists of a single byte, and |
| 6572 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6573 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6574 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6575 | static int |
| 6576 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6577 | { |
| 6578 | const char *curr = s + offset; |
| 6579 | |
| 6580 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6581 | const char *prev = CharPrev(s, curr); |
| 6582 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6583 | } |
| 6584 | return 0; |
| 6585 | } |
| 6586 | |
| 6587 | /* |
| 6588 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6589 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6590 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6591 | static int |
| 6592 | decode_mbcs(PyUnicodeObject **v, |
| 6593 | const char *s, /* MBCS string */ |
| 6594 | int size, /* sizeof MBCS string */ |
| 6595 | int final, |
| 6596 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6597 | { |
| 6598 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6599 | Py_ssize_t n; |
| 6600 | DWORD usize; |
| 6601 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6602 | |
| 6603 | assert(size >= 0); |
| 6604 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6605 | /* check and handle 'errors' arg */ |
| 6606 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6607 | flags = MB_ERR_INVALID_CHARS; |
| 6608 | else if (strcmp(errors, "ignore")==0) |
| 6609 | flags = 0; |
| 6610 | else { |
| 6611 | PyErr_Format(PyExc_ValueError, |
| 6612 | "mbcs encoding does not support errors='%s'", |
| 6613 | errors); |
| 6614 | return -1; |
| 6615 | } |
| 6616 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6617 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6618 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6619 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6620 | |
| 6621 | /* First get the size of the result */ |
| 6622 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6623 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6624 | if (usize==0) |
| 6625 | goto mbcs_decode_error; |
| 6626 | } else |
| 6627 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6628 | |
| 6629 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6630 | /* Create unicode object */ |
| 6631 | *v = _PyUnicode_New(usize); |
| 6632 | if (*v == NULL) |
| 6633 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6634 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6635 | } |
| 6636 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6637 | /* Extend unicode object */ |
| 6638 | n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 6639 | if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6640 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6641 | } |
| 6642 | |
| 6643 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6644 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6645 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6646 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6647 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6648 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6649 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6650 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6651 | |
| 6652 | mbcs_decode_error: |
| 6653 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6654 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6655 | windows error |
| 6656 | */ |
| 6657 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6658 | /* Ideally, we should get reason from FormatMessage - this |
| 6659 | is the Windows 2000 English version of the message |
| 6660 | */ |
| 6661 | PyObject *exc = NULL; |
| 6662 | const char *reason = "No mapping for the Unicode character exists " |
| 6663 | "in the target multi-byte code page."; |
| 6664 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6665 | if (exc != NULL) { |
| 6666 | PyCodec_StrictErrors(exc); |
| 6667 | Py_DECREF(exc); |
| 6668 | } |
| 6669 | } else { |
| 6670 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6671 | } |
| 6672 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6673 | } |
| 6674 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6675 | PyObject * |
| 6676 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6677 | Py_ssize_t size, |
| 6678 | const char *errors, |
| 6679 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6680 | { |
| 6681 | PyUnicodeObject *v = NULL; |
| 6682 | int done; |
| 6683 | |
| 6684 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6685 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6686 | |
| 6687 | #ifdef NEED_RETRY |
| 6688 | retry: |
| 6689 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6690 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6691 | else |
| 6692 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6693 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6694 | |
| 6695 | if (done < 0) { |
| 6696 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6697 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6698 | } |
| 6699 | |
| 6700 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6701 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6702 | |
| 6703 | #ifdef NEED_RETRY |
| 6704 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6705 | s += done; |
| 6706 | size -= done; |
| 6707 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6708 | } |
| 6709 | #endif |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6710 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6711 | Py_DECREF(v); |
| 6712 | return NULL; |
| 6713 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6714 | return (PyObject *)v; |
| 6715 | } |
| 6716 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6717 | PyObject * |
| 6718 | PyUnicode_DecodeMBCS(const char *s, |
| 6719 | Py_ssize_t size, |
| 6720 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6721 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6722 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6723 | } |
| 6724 | |
| 6725 | /* |
| 6726 | * Convert unicode into string object (MBCS). |
| 6727 | * Returns 0 if succeed, -1 otherwise. |
| 6728 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6729 | static int |
| 6730 | encode_mbcs(PyObject **repr, |
| 6731 | const Py_UNICODE *p, /* unicode */ |
| 6732 | int size, /* size of unicode */ |
| 6733 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6734 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6735 | BOOL usedDefaultChar = FALSE; |
| 6736 | BOOL *pusedDefaultChar; |
| 6737 | int mbcssize; |
| 6738 | Py_ssize_t n; |
| 6739 | PyObject *exc = NULL; |
| 6740 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6741 | |
| 6742 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6743 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6744 | /* check and handle 'errors' arg */ |
| 6745 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 6746 | flags = WC_NO_BEST_FIT_CHARS; |
| 6747 | pusedDefaultChar = &usedDefaultChar; |
| 6748 | } else if (strcmp(errors, "replace")==0) { |
| 6749 | flags = 0; |
| 6750 | pusedDefaultChar = NULL; |
| 6751 | } else { |
| 6752 | PyErr_Format(PyExc_ValueError, |
| 6753 | "mbcs encoding does not support errors='%s'", |
| 6754 | errors); |
| 6755 | return -1; |
| 6756 | } |
| 6757 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6758 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6759 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6760 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 6761 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6762 | if (mbcssize == 0) { |
| 6763 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6764 | return -1; |
| 6765 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6766 | /* If we used a default char, then we failed! */ |
| 6767 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6768 | goto mbcs_encode_error; |
| 6769 | } else { |
| 6770 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6771 | } |
| 6772 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6773 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6774 | /* Create string object */ |
| 6775 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 6776 | if (*repr == NULL) |
| 6777 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6778 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6779 | } |
| 6780 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6781 | /* Extend string object */ |
| 6782 | n = PyBytes_Size(*repr); |
| 6783 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 6784 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6785 | } |
| 6786 | |
| 6787 | /* Do the conversion */ |
| 6788 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6789 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6790 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 6791 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6792 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6793 | return -1; |
| 6794 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6795 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6796 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6797 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6798 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6799 | |
| 6800 | mbcs_encode_error: |
| 6801 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 6802 | Py_XDECREF(exc); |
| 6803 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6804 | } |
| 6805 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6806 | PyObject * |
| 6807 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 6808 | Py_ssize_t size, |
| 6809 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6810 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6811 | PyObject *repr = NULL; |
| 6812 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 6813 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6814 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6815 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6816 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6817 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6818 | else |
| 6819 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6820 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6821 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6822 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6823 | Py_XDECREF(repr); |
| 6824 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6825 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6826 | |
| 6827 | #ifdef NEED_RETRY |
| 6828 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6829 | p += INT_MAX; |
| 6830 | size -= INT_MAX; |
| 6831 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6832 | } |
| 6833 | #endif |
| 6834 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6835 | return repr; |
| 6836 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6837 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6838 | PyObject * |
| 6839 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6840 | { |
| 6841 | if (!PyUnicode_Check(unicode)) { |
| 6842 | PyErr_BadArgument(); |
| 6843 | return NULL; |
| 6844 | } |
| 6845 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6846 | PyUnicode_GET_SIZE(unicode), |
| 6847 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6848 | } |
| 6849 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6850 | #undef NEED_RETRY |
| 6851 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6852 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6854 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 6855 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6856 | PyObject * |
| 6857 | PyUnicode_DecodeCharmap(const char *s, |
| 6858 | Py_ssize_t size, |
| 6859 | PyObject *mapping, |
| 6860 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6861 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6862 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6863 | Py_ssize_t startinpos; |
| 6864 | Py_ssize_t endinpos; |
| 6865 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6866 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6867 | PyUnicodeObject *v; |
| 6868 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6869 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6870 | PyObject *errorHandler = NULL; |
| 6871 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6872 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6873 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6874 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6875 | /* Default to Latin-1 */ |
| 6876 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6877 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6878 | |
| 6879 | v = _PyUnicode_New(size); |
| 6880 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6881 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6882 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6883 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6884 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6885 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6886 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6887 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 6888 | maplen = PyUnicode_GET_SIZE(mapping); |
| 6889 | while (s < e) { |
| 6890 | unsigned char ch = *s; |
| 6891 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6892 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6893 | if (ch < maplen) |
| 6894 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6896 | if (x == 0xfffe) { |
| 6897 | /* undefined mapping */ |
| 6898 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6899 | startinpos = s-starts; |
| 6900 | endinpos = startinpos+1; |
| 6901 | if (unicode_decode_call_errorhandler( |
| 6902 | errors, &errorHandler, |
| 6903 | "charmap", "character maps to <undefined>", |
| 6904 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6905 | &v, &outpos, &p)) { |
| 6906 | goto onError; |
| 6907 | } |
| 6908 | continue; |
| 6909 | } |
| 6910 | *p++ = x; |
| 6911 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6912 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6913 | } |
| 6914 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6915 | while (s < e) { |
| 6916 | unsigned char ch = *s; |
| 6917 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6918 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6919 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 6920 | w = PyLong_FromLong((long)ch); |
| 6921 | if (w == NULL) |
| 6922 | goto onError; |
| 6923 | x = PyObject_GetItem(mapping, w); |
| 6924 | Py_DECREF(w); |
| 6925 | if (x == NULL) { |
| 6926 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6927 | /* No mapping found means: mapping is undefined. */ |
| 6928 | PyErr_Clear(); |
| 6929 | x = Py_None; |
| 6930 | Py_INCREF(x); |
| 6931 | } else |
| 6932 | goto onError; |
| 6933 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6934 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6935 | /* Apply mapping */ |
| 6936 | if (PyLong_Check(x)) { |
| 6937 | long value = PyLong_AS_LONG(x); |
| 6938 | if (value < 0 || value > 65535) { |
| 6939 | PyErr_SetString(PyExc_TypeError, |
| 6940 | "character mapping must be in range(65536)"); |
| 6941 | Py_DECREF(x); |
| 6942 | goto onError; |
| 6943 | } |
| 6944 | *p++ = (Py_UNICODE)value; |
| 6945 | } |
| 6946 | else if (x == Py_None) { |
| 6947 | /* undefined mapping */ |
| 6948 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6949 | startinpos = s-starts; |
| 6950 | endinpos = startinpos+1; |
| 6951 | if (unicode_decode_call_errorhandler( |
| 6952 | errors, &errorHandler, |
| 6953 | "charmap", "character maps to <undefined>", |
| 6954 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6955 | &v, &outpos, &p)) { |
| 6956 | Py_DECREF(x); |
| 6957 | goto onError; |
| 6958 | } |
| 6959 | Py_DECREF(x); |
| 6960 | continue; |
| 6961 | } |
| 6962 | else if (PyUnicode_Check(x)) { |
| 6963 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6964 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6965 | if (targetsize == 1) |
| 6966 | /* 1-1 mapping */ |
| 6967 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6968 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6969 | else if (targetsize > 1) { |
| 6970 | /* 1-n mapping */ |
| 6971 | if (targetsize > extrachars) { |
| 6972 | /* resize first */ |
| 6973 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 6974 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 6975 | (targetsize << 2); |
| 6976 | extrachars += needed; |
| 6977 | /* XXX overflow detection missing */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6978 | if (PyUnicode_Resize((PyObject**)&v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6979 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 6980 | Py_DECREF(x); |
| 6981 | goto onError; |
| 6982 | } |
| 6983 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 6984 | } |
| 6985 | Py_UNICODE_COPY(p, |
| 6986 | PyUnicode_AS_UNICODE(x), |
| 6987 | targetsize); |
| 6988 | p += targetsize; |
| 6989 | extrachars -= targetsize; |
| 6990 | } |
| 6991 | /* 1-0 mapping: skip the character */ |
| 6992 | } |
| 6993 | else { |
| 6994 | /* wrong return value */ |
| 6995 | PyErr_SetString(PyExc_TypeError, |
| 6996 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6997 | Py_DECREF(x); |
| 6998 | goto onError; |
| 6999 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7000 | Py_DECREF(x); |
| 7001 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7002 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7003 | } |
| 7004 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 7005 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7006 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7007 | Py_XDECREF(errorHandler); |
| 7008 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7009 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7010 | Py_DECREF(v); |
| 7011 | return NULL; |
| 7012 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7013 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7014 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7015 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7016 | Py_XDECREF(errorHandler); |
| 7017 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7018 | Py_XDECREF(v); |
| 7019 | return NULL; |
| 7020 | } |
| 7021 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7022 | /* Charmap encoding: the lookup table */ |
| 7023 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7024 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7025 | PyObject_HEAD |
| 7026 | unsigned char level1[32]; |
| 7027 | int count2, count3; |
| 7028 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7029 | }; |
| 7030 | |
| 7031 | static PyObject* |
| 7032 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7033 | { |
| 7034 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7035 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7036 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7037 | } |
| 7038 | |
| 7039 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7040 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7041 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7042 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7043 | }; |
| 7044 | |
| 7045 | static void |
| 7046 | encoding_map_dealloc(PyObject* o) |
| 7047 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7048 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7049 | } |
| 7050 | |
| 7051 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7052 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7053 | "EncodingMap", /*tp_name*/ |
| 7054 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7055 | 0, /*tp_itemsize*/ |
| 7056 | /* methods */ |
| 7057 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7058 | 0, /*tp_print*/ |
| 7059 | 0, /*tp_getattr*/ |
| 7060 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7061 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7062 | 0, /*tp_repr*/ |
| 7063 | 0, /*tp_as_number*/ |
| 7064 | 0, /*tp_as_sequence*/ |
| 7065 | 0, /*tp_as_mapping*/ |
| 7066 | 0, /*tp_hash*/ |
| 7067 | 0, /*tp_call*/ |
| 7068 | 0, /*tp_str*/ |
| 7069 | 0, /*tp_getattro*/ |
| 7070 | 0, /*tp_setattro*/ |
| 7071 | 0, /*tp_as_buffer*/ |
| 7072 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7073 | 0, /*tp_doc*/ |
| 7074 | 0, /*tp_traverse*/ |
| 7075 | 0, /*tp_clear*/ |
| 7076 | 0, /*tp_richcompare*/ |
| 7077 | 0, /*tp_weaklistoffset*/ |
| 7078 | 0, /*tp_iter*/ |
| 7079 | 0, /*tp_iternext*/ |
| 7080 | encoding_map_methods, /*tp_methods*/ |
| 7081 | 0, /*tp_members*/ |
| 7082 | 0, /*tp_getset*/ |
| 7083 | 0, /*tp_base*/ |
| 7084 | 0, /*tp_dict*/ |
| 7085 | 0, /*tp_descr_get*/ |
| 7086 | 0, /*tp_descr_set*/ |
| 7087 | 0, /*tp_dictoffset*/ |
| 7088 | 0, /*tp_init*/ |
| 7089 | 0, /*tp_alloc*/ |
| 7090 | 0, /*tp_new*/ |
| 7091 | 0, /*tp_free*/ |
| 7092 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7093 | }; |
| 7094 | |
| 7095 | PyObject* |
| 7096 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7097 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7098 | PyObject *result; |
| 7099 | struct encoding_map *mresult; |
| 7100 | int i; |
| 7101 | int need_dict = 0; |
| 7102 | unsigned char level1[32]; |
| 7103 | unsigned char level2[512]; |
| 7104 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7105 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7106 | int kind; |
| 7107 | void *data; |
| 7108 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7109 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7110 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7111 | PyErr_BadArgument(); |
| 7112 | return NULL; |
| 7113 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7114 | kind = PyUnicode_KIND(string); |
| 7115 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7116 | memset(level1, 0xFF, sizeof level1); |
| 7117 | memset(level2, 0xFF, sizeof level2); |
| 7118 | |
| 7119 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7120 | or if there are non-BMP characters, we need to use |
| 7121 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7122 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7123 | need_dict = 1; |
| 7124 | for (i = 1; i < 256; i++) { |
| 7125 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7126 | ch = PyUnicode_READ(kind, data, i); |
| 7127 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7128 | need_dict = 1; |
| 7129 | break; |
| 7130 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7131 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7132 | /* unmapped character */ |
| 7133 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7134 | l1 = ch >> 11; |
| 7135 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7136 | if (level1[l1] == 0xFF) |
| 7137 | level1[l1] = count2++; |
| 7138 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7139 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7140 | } |
| 7141 | |
| 7142 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7143 | need_dict = 1; |
| 7144 | |
| 7145 | if (need_dict) { |
| 7146 | PyObject *result = PyDict_New(); |
| 7147 | PyObject *key, *value; |
| 7148 | if (!result) |
| 7149 | return NULL; |
| 7150 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7151 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7152 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7153 | if (!key || !value) |
| 7154 | goto failed1; |
| 7155 | if (PyDict_SetItem(result, key, value) == -1) |
| 7156 | goto failed1; |
| 7157 | Py_DECREF(key); |
| 7158 | Py_DECREF(value); |
| 7159 | } |
| 7160 | return result; |
| 7161 | failed1: |
| 7162 | Py_XDECREF(key); |
| 7163 | Py_XDECREF(value); |
| 7164 | Py_DECREF(result); |
| 7165 | return NULL; |
| 7166 | } |
| 7167 | |
| 7168 | /* Create a three-level trie */ |
| 7169 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7170 | 16*count2 + 128*count3 - 1); |
| 7171 | if (!result) |
| 7172 | return PyErr_NoMemory(); |
| 7173 | PyObject_Init(result, &EncodingMapType); |
| 7174 | mresult = (struct encoding_map*)result; |
| 7175 | mresult->count2 = count2; |
| 7176 | mresult->count3 = count3; |
| 7177 | mlevel1 = mresult->level1; |
| 7178 | mlevel2 = mresult->level23; |
| 7179 | mlevel3 = mresult->level23 + 16*count2; |
| 7180 | memcpy(mlevel1, level1, 32); |
| 7181 | memset(mlevel2, 0xFF, 16*count2); |
| 7182 | memset(mlevel3, 0, 128*count3); |
| 7183 | count3 = 0; |
| 7184 | for (i = 1; i < 256; i++) { |
| 7185 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7186 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7187 | /* unmapped character */ |
| 7188 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7189 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7190 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7191 | i2 = 16*mlevel1[o1] + o2; |
| 7192 | if (mlevel2[i2] == 0xFF) |
| 7193 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7194 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7195 | i3 = 128*mlevel2[i2] + o3; |
| 7196 | mlevel3[i3] = i; |
| 7197 | } |
| 7198 | return result; |
| 7199 | } |
| 7200 | |
| 7201 | static int |
| 7202 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7203 | { |
| 7204 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7205 | int l1 = c>>11; |
| 7206 | int l2 = (c>>7) & 0xF; |
| 7207 | int l3 = c & 0x7F; |
| 7208 | int i; |
| 7209 | |
| 7210 | #ifdef Py_UNICODE_WIDE |
| 7211 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7212 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7213 | } |
| 7214 | #endif |
| 7215 | if (c == 0) |
| 7216 | return 0; |
| 7217 | /* level 1*/ |
| 7218 | i = map->level1[l1]; |
| 7219 | if (i == 0xFF) { |
| 7220 | return -1; |
| 7221 | } |
| 7222 | /* level 2*/ |
| 7223 | i = map->level23[16*i+l2]; |
| 7224 | if (i == 0xFF) { |
| 7225 | return -1; |
| 7226 | } |
| 7227 | /* level 3 */ |
| 7228 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7229 | if (i == 0) { |
| 7230 | return -1; |
| 7231 | } |
| 7232 | return i; |
| 7233 | } |
| 7234 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7235 | /* Lookup the character ch in the mapping. If the character |
| 7236 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7237 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7238 | static PyObject * |
| 7239 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7240 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7241 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7242 | PyObject *x; |
| 7243 | |
| 7244 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7245 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7246 | x = PyObject_GetItem(mapping, w); |
| 7247 | Py_DECREF(w); |
| 7248 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7249 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7250 | /* No mapping found means: mapping is undefined. */ |
| 7251 | PyErr_Clear(); |
| 7252 | x = Py_None; |
| 7253 | Py_INCREF(x); |
| 7254 | return x; |
| 7255 | } else |
| 7256 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7257 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7258 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7259 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7260 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7261 | long value = PyLong_AS_LONG(x); |
| 7262 | if (value < 0 || value > 255) { |
| 7263 | PyErr_SetString(PyExc_TypeError, |
| 7264 | "character mapping must be in range(256)"); |
| 7265 | Py_DECREF(x); |
| 7266 | return NULL; |
| 7267 | } |
| 7268 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7269 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7270 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7271 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7272 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7273 | /* wrong return value */ |
| 7274 | PyErr_Format(PyExc_TypeError, |
| 7275 | "character mapping must return integer, bytes or None, not %.400s", |
| 7276 | x->ob_type->tp_name); |
| 7277 | Py_DECREF(x); |
| 7278 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7279 | } |
| 7280 | } |
| 7281 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7282 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7283 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7284 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7285 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7286 | /* exponentially overallocate to minimize reallocations */ |
| 7287 | if (requiredsize < 2*outsize) |
| 7288 | requiredsize = 2*outsize; |
| 7289 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7290 | return -1; |
| 7291 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7292 | } |
| 7293 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7294 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7295 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7296 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7297 | /* 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] | 7298 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7299 | space is available. Return a new reference to the object that |
| 7300 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7301 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7302 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7303 | static charmapencode_result |
| 7304 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 7305 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7306 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7307 | PyObject *rep; |
| 7308 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7309 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7310 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7311 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7312 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7313 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7314 | if (res == -1) |
| 7315 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7316 | if (outsize<requiredsize) |
| 7317 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7318 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7319 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7320 | outstart[(*outpos)++] = (char)res; |
| 7321 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7322 | } |
| 7323 | |
| 7324 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7325 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7326 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7327 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7328 | Py_DECREF(rep); |
| 7329 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7330 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7331 | if (PyLong_Check(rep)) { |
| 7332 | Py_ssize_t requiredsize = *outpos+1; |
| 7333 | if (outsize<requiredsize) |
| 7334 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7335 | Py_DECREF(rep); |
| 7336 | return enc_EXCEPTION; |
| 7337 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7338 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7339 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7340 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7341 | else { |
| 7342 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7343 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7344 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7345 | if (outsize<requiredsize) |
| 7346 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7347 | Py_DECREF(rep); |
| 7348 | return enc_EXCEPTION; |
| 7349 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7350 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7351 | memcpy(outstart + *outpos, repchars, repsize); |
| 7352 | *outpos += repsize; |
| 7353 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7354 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7355 | Py_DECREF(rep); |
| 7356 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7357 | } |
| 7358 | |
| 7359 | /* handle an error in PyUnicode_EncodeCharmap |
| 7360 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7361 | static int |
| 7362 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7363 | const Py_UNICODE *p, Py_ssize_t size, Py_ssize_t *inpos, PyObject *mapping, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7364 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7365 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7366 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7367 | { |
| 7368 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7369 | Py_ssize_t repsize; |
| 7370 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7371 | Py_UNICODE *uni2; |
| 7372 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7373 | Py_ssize_t collstartpos = *inpos; |
| 7374 | Py_ssize_t collendpos = *inpos+1; |
| 7375 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7376 | char *encoding = "charmap"; |
| 7377 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7378 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7379 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7380 | /* find all unencodable characters */ |
| 7381 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7382 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7383 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7384 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 7385 | if (res != -1) |
| 7386 | break; |
| 7387 | ++collendpos; |
| 7388 | continue; |
| 7389 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7390 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7391 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 7392 | if (rep==NULL) |
| 7393 | return -1; |
| 7394 | else if (rep!=Py_None) { |
| 7395 | Py_DECREF(rep); |
| 7396 | break; |
| 7397 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7398 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7399 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7400 | } |
| 7401 | /* cache callback name lookup |
| 7402 | * (if not done yet, i.e. it's the first error) */ |
| 7403 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7404 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7405 | *known_errorHandler = 1; |
| 7406 | else if (!strcmp(errors, "replace")) |
| 7407 | *known_errorHandler = 2; |
| 7408 | else if (!strcmp(errors, "ignore")) |
| 7409 | *known_errorHandler = 3; |
| 7410 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7411 | *known_errorHandler = 4; |
| 7412 | else |
| 7413 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7414 | } |
| 7415 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7416 | case 1: /* strict */ |
| 7417 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7418 | return -1; |
| 7419 | case 2: /* replace */ |
| 7420 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7421 | x = charmapencode_output('?', mapping, res, respos); |
| 7422 | if (x==enc_EXCEPTION) { |
| 7423 | return -1; |
| 7424 | } |
| 7425 | else if (x==enc_FAILED) { |
| 7426 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7427 | return -1; |
| 7428 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7429 | } |
| 7430 | /* fall through */ |
| 7431 | case 3: /* ignore */ |
| 7432 | *inpos = collendpos; |
| 7433 | break; |
| 7434 | case 4: /* xmlcharrefreplace */ |
| 7435 | /* generate replacement (temporarily (mis)uses p) */ |
| 7436 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7437 | char buffer[2+29+1+1]; |
| 7438 | char *cp; |
| 7439 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7440 | for (cp = buffer; *cp; ++cp) { |
| 7441 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7442 | if (x==enc_EXCEPTION) |
| 7443 | return -1; |
| 7444 | else if (x==enc_FAILED) { |
| 7445 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7446 | return -1; |
| 7447 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7448 | } |
| 7449 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7450 | *inpos = collendpos; |
| 7451 | break; |
| 7452 | default: |
| 7453 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7454 | encoding, reason, p, size, exceptionObject, |
| 7455 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7456 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7457 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7458 | if (PyBytes_Check(repunicode)) { |
| 7459 | /* Directly copy bytes result to output. */ |
| 7460 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7461 | Py_ssize_t requiredsize; |
| 7462 | repsize = PyBytes_Size(repunicode); |
| 7463 | requiredsize = *respos + repsize; |
| 7464 | if (requiredsize > outsize) |
| 7465 | /* Make room for all additional bytes. */ |
| 7466 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7467 | Py_DECREF(repunicode); |
| 7468 | return -1; |
| 7469 | } |
| 7470 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7471 | PyBytes_AsString(repunicode), repsize); |
| 7472 | *respos += repsize; |
| 7473 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7474 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7475 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7476 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7477 | /* generate replacement */ |
| 7478 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7479 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7480 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7481 | if (x==enc_EXCEPTION) { |
| 7482 | return -1; |
| 7483 | } |
| 7484 | else if (x==enc_FAILED) { |
| 7485 | Py_DECREF(repunicode); |
| 7486 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7487 | return -1; |
| 7488 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7489 | } |
| 7490 | *inpos = newpos; |
| 7491 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7492 | } |
| 7493 | return 0; |
| 7494 | } |
| 7495 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7496 | PyObject * |
| 7497 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7498 | Py_ssize_t size, |
| 7499 | PyObject *mapping, |
| 7500 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7501 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7502 | /* output object */ |
| 7503 | PyObject *res = NULL; |
| 7504 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7505 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7506 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7507 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7508 | PyObject *errorHandler = NULL; |
| 7509 | PyObject *exc = NULL; |
| 7510 | /* the following variable is used for caching string comparisons |
| 7511 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7512 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7513 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7514 | |
| 7515 | /* Default to Latin-1 */ |
| 7516 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7517 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7518 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7519 | /* allocate enough for a simple encoding without |
| 7520 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7521 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7522 | if (res == NULL) |
| 7523 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7524 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7525 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7526 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7527 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7528 | /* try to encode it */ |
| 7529 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7530 | if (x==enc_EXCEPTION) /* error */ |
| 7531 | goto onError; |
| 7532 | if (x==enc_FAILED) { /* unencodable character */ |
| 7533 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7534 | &exc, |
| 7535 | &known_errorHandler, &errorHandler, errors, |
| 7536 | &res, &respos)) { |
| 7537 | goto onError; |
| 7538 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7539 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7540 | else |
| 7541 | /* done with this character => adjust input position */ |
| 7542 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7543 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7544 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7545 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7546 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7547 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7548 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7549 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7550 | Py_XDECREF(exc); |
| 7551 | Py_XDECREF(errorHandler); |
| 7552 | return res; |
| 7553 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7554 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7555 | Py_XDECREF(res); |
| 7556 | Py_XDECREF(exc); |
| 7557 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7558 | return NULL; |
| 7559 | } |
| 7560 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7561 | PyObject * |
| 7562 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7563 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7564 | { |
| 7565 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7566 | PyErr_BadArgument(); |
| 7567 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7568 | } |
| 7569 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7570 | PyUnicode_GET_SIZE(unicode), |
| 7571 | mapping, |
| 7572 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7573 | } |
| 7574 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7575 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7576 | static void |
| 7577 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7578 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7579 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7580 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7581 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7582 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7583 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7584 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7585 | } |
| 7586 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7587 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7588 | goto onError; |
| 7589 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7590 | goto onError; |
| 7591 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7592 | goto onError; |
| 7593 | return; |
| 7594 | onError: |
| 7595 | Py_DECREF(*exceptionObject); |
| 7596 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7597 | } |
| 7598 | } |
| 7599 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7600 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7601 | static void |
| 7602 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7603 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7604 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7605 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7606 | { |
| 7607 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7608 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7609 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7610 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7611 | } |
| 7612 | |
| 7613 | /* error handling callback helper: |
| 7614 | build arguments, call the callback and check the arguments, |
| 7615 | put the result into newpos and return the replacement string, which |
| 7616 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7617 | static PyObject * |
| 7618 | unicode_translate_call_errorhandler(const char *errors, |
| 7619 | PyObject **errorHandler, |
| 7620 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7621 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7622 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7623 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7624 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7625 | 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] | 7626 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7627 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7628 | PyObject *restuple; |
| 7629 | PyObject *resunicode; |
| 7630 | |
| 7631 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7632 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7633 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7634 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7635 | } |
| 7636 | |
| 7637 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7638 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7639 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7640 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7641 | |
| 7642 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7643 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7644 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7645 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7646 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7647 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7648 | Py_DECREF(restuple); |
| 7649 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7650 | } |
| 7651 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7652 | &resunicode, &i_newpos)) { |
| 7653 | Py_DECREF(restuple); |
| 7654 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7655 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7656 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7657 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7658 | else |
| 7659 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7660 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7661 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7662 | Py_DECREF(restuple); |
| 7663 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7664 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7665 | Py_INCREF(resunicode); |
| 7666 | Py_DECREF(restuple); |
| 7667 | return resunicode; |
| 7668 | } |
| 7669 | |
| 7670 | /* Lookup the character ch in the mapping and put the result in result, |
| 7671 | which must be decrefed by the caller. |
| 7672 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7673 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7674 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7675 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7676 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7677 | PyObject *x; |
| 7678 | |
| 7679 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7680 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7681 | x = PyObject_GetItem(mapping, w); |
| 7682 | Py_DECREF(w); |
| 7683 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7684 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7685 | /* No mapping found means: use 1:1 mapping. */ |
| 7686 | PyErr_Clear(); |
| 7687 | *result = NULL; |
| 7688 | return 0; |
| 7689 | } else |
| 7690 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7691 | } |
| 7692 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7693 | *result = x; |
| 7694 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7695 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7696 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7697 | long value = PyLong_AS_LONG(x); |
| 7698 | long max = PyUnicode_GetMax(); |
| 7699 | if (value < 0 || value > max) { |
| 7700 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7701 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7702 | Py_DECREF(x); |
| 7703 | return -1; |
| 7704 | } |
| 7705 | *result = x; |
| 7706 | return 0; |
| 7707 | } |
| 7708 | else if (PyUnicode_Check(x)) { |
| 7709 | *result = x; |
| 7710 | return 0; |
| 7711 | } |
| 7712 | else { |
| 7713 | /* wrong return value */ |
| 7714 | PyErr_SetString(PyExc_TypeError, |
| 7715 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7716 | Py_DECREF(x); |
| 7717 | return -1; |
| 7718 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7719 | } |
| 7720 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7721 | if not reallocate and adjust various state variables. |
| 7722 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7723 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7724 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7725 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7726 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7727 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7728 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7729 | /* exponentially overallocate to minimize reallocations */ |
| 7730 | if (requiredsize < 2 * oldsize) |
| 7731 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7732 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7733 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7734 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7735 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7736 | } |
| 7737 | return 0; |
| 7738 | } |
| 7739 | /* lookup the character, put the result in the output string and adjust |
| 7740 | various state variables. Return a new reference to the object that |
| 7741 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 7742 | undefined (in which case no character was written). |
| 7743 | The called must decref result. |
| 7744 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7745 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7746 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 7747 | PyObject *mapping, Py_UCS4 **output, |
| 7748 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7749 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7750 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7751 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 7752 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7753 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7754 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7755 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7756 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7757 | } |
| 7758 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7759 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7760 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7761 | /* 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] | 7762 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7763 | } |
| 7764 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7765 | Py_ssize_t repsize; |
| 7766 | if (PyUnicode_READY(*res) == -1) |
| 7767 | return -1; |
| 7768 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7769 | if (repsize==1) { |
| 7770 | /* 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] | 7771 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7772 | } |
| 7773 | else if (repsize!=0) { |
| 7774 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7775 | Py_ssize_t requiredsize = *opos + |
| 7776 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7777 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7778 | Py_ssize_t i; |
| 7779 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7780 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7781 | for(i = 0; i < repsize; i++) |
| 7782 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7783 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7784 | } |
| 7785 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7786 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7787 | return 0; |
| 7788 | } |
| 7789 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7790 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7791 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 7792 | PyObject *mapping, |
| 7793 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7794 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7795 | /* input object */ |
| 7796 | char *idata; |
| 7797 | Py_ssize_t size, i; |
| 7798 | int kind; |
| 7799 | /* output buffer */ |
| 7800 | Py_UCS4 *output = NULL; |
| 7801 | Py_ssize_t osize; |
| 7802 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7803 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7804 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7805 | char *reason = "character maps to <undefined>"; |
| 7806 | PyObject *errorHandler = NULL; |
| 7807 | PyObject *exc = NULL; |
| 7808 | /* the following variable is used for caching string comparisons |
| 7809 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7810 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7811 | int known_errorHandler = -1; |
| 7812 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7813 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7814 | PyErr_BadArgument(); |
| 7815 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7816 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7817 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7818 | if (PyUnicode_READY(input) == -1) |
| 7819 | return NULL; |
| 7820 | idata = (char*)PyUnicode_DATA(input); |
| 7821 | kind = PyUnicode_KIND(input); |
| 7822 | size = PyUnicode_GET_LENGTH(input); |
| 7823 | i = 0; |
| 7824 | |
| 7825 | if (size == 0) { |
| 7826 | Py_INCREF(input); |
| 7827 | return input; |
| 7828 | } |
| 7829 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7830 | /* allocate enough for a simple 1:1 translation without |
| 7831 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7832 | osize = size; |
| 7833 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 7834 | opos = 0; |
| 7835 | if (output == NULL) { |
| 7836 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7837 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7838 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7839 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7840 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7841 | /* try to encode it */ |
| 7842 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7843 | if (charmaptranslate_output(input, i, mapping, |
| 7844 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7845 | Py_XDECREF(x); |
| 7846 | goto onError; |
| 7847 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7848 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7849 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7850 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7851 | else { /* untranslatable character */ |
| 7852 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 7853 | Py_ssize_t repsize; |
| 7854 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7855 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7856 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7857 | Py_ssize_t collstart = i; |
| 7858 | Py_ssize_t collend = i+1; |
| 7859 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7860 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7861 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7862 | while (collend < size) { |
| 7863 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7864 | goto onError; |
| 7865 | Py_XDECREF(x); |
| 7866 | if (x!=Py_None) |
| 7867 | break; |
| 7868 | ++collend; |
| 7869 | } |
| 7870 | /* cache callback name lookup |
| 7871 | * (if not done yet, i.e. it's the first error) */ |
| 7872 | if (known_errorHandler==-1) { |
| 7873 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7874 | known_errorHandler = 1; |
| 7875 | else if (!strcmp(errors, "replace")) |
| 7876 | known_errorHandler = 2; |
| 7877 | else if (!strcmp(errors, "ignore")) |
| 7878 | known_errorHandler = 3; |
| 7879 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7880 | known_errorHandler = 4; |
| 7881 | else |
| 7882 | known_errorHandler = 0; |
| 7883 | } |
| 7884 | switch (known_errorHandler) { |
| 7885 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7886 | raise_translate_exception(&exc, input, collstart, |
| 7887 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7888 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7889 | case 2: /* replace */ |
| 7890 | /* 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] | 7891 | for (coll = collstart; coll<collend; coll++) |
| 7892 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7893 | /* fall through */ |
| 7894 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7895 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7896 | break; |
| 7897 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7898 | /* generate replacement (temporarily (mis)uses i) */ |
| 7899 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7900 | char buffer[2+29+1+1]; |
| 7901 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7902 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 7903 | if (charmaptranslate_makespace(&output, &osize, |
| 7904 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7905 | goto onError; |
| 7906 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7907 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7908 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7909 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7910 | break; |
| 7911 | default: |
| 7912 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7913 | reason, input, &exc, |
| 7914 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7915 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7916 | goto onError; |
| 7917 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7918 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 7919 | if (charmaptranslate_makespace(&output, &osize, |
| 7920 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7921 | Py_DECREF(repunicode); |
| 7922 | goto onError; |
| 7923 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7924 | for (uni2 = 0; repsize-->0; ++uni2) |
| 7925 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 7926 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7927 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7928 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7929 | } |
| 7930 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7931 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 7932 | if (!res) |
| 7933 | goto onError; |
| 7934 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7935 | Py_XDECREF(exc); |
| 7936 | Py_XDECREF(errorHandler); |
| 7937 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7938 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7939 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7940 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7941 | Py_XDECREF(exc); |
| 7942 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7943 | return NULL; |
| 7944 | } |
| 7945 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7946 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 7947 | PyObject * |
| 7948 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 7949 | Py_ssize_t size, |
| 7950 | PyObject *mapping, |
| 7951 | const char *errors) |
| 7952 | { |
| 7953 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7954 | if (!unicode) |
| 7955 | return NULL; |
| 7956 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 7957 | } |
| 7958 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7959 | PyObject * |
| 7960 | PyUnicode_Translate(PyObject *str, |
| 7961 | PyObject *mapping, |
| 7962 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7963 | { |
| 7964 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7965 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7966 | str = PyUnicode_FromObject(str); |
| 7967 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7968 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7969 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7970 | Py_DECREF(str); |
| 7971 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7972 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7973 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7974 | Py_XDECREF(str); |
| 7975 | return NULL; |
| 7976 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7977 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7978 | static Py_UCS4 |
| 7979 | fix_decimal_and_space_to_ascii(PyUnicodeObject *self) |
| 7980 | { |
| 7981 | /* No need to call PyUnicode_READY(self) because this function is only |
| 7982 | called as a callback from fixup() which does it already. */ |
| 7983 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 7984 | const int kind = PyUnicode_KIND(self); |
| 7985 | void *data = PyUnicode_DATA(self); |
| 7986 | Py_UCS4 maxchar = 0, ch, fixed; |
| 7987 | Py_ssize_t i; |
| 7988 | |
| 7989 | for (i = 0; i < len; ++i) { |
| 7990 | ch = PyUnicode_READ(kind, data, i); |
| 7991 | fixed = 0; |
| 7992 | if (ch > 127) { |
| 7993 | if (Py_UNICODE_ISSPACE(ch)) |
| 7994 | fixed = ' '; |
| 7995 | else { |
| 7996 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7997 | if (decimal >= 0) |
| 7998 | fixed = '0' + decimal; |
| 7999 | } |
| 8000 | if (fixed != 0) { |
| 8001 | if (fixed > maxchar) |
| 8002 | maxchar = fixed; |
| 8003 | PyUnicode_WRITE(kind, data, i, fixed); |
| 8004 | } |
| 8005 | else if (ch > maxchar) |
| 8006 | maxchar = ch; |
| 8007 | } |
| 8008 | else if (ch > maxchar) |
| 8009 | maxchar = ch; |
| 8010 | } |
| 8011 | |
| 8012 | return maxchar; |
| 8013 | } |
| 8014 | |
| 8015 | PyObject * |
| 8016 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8017 | { |
| 8018 | if (!PyUnicode_Check(unicode)) { |
| 8019 | PyErr_BadInternalCall(); |
| 8020 | return NULL; |
| 8021 | } |
| 8022 | if (PyUnicode_READY(unicode) == -1) |
| 8023 | return NULL; |
| 8024 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8025 | /* If the string is already ASCII, just return the same string */ |
| 8026 | Py_INCREF(unicode); |
| 8027 | return unicode; |
| 8028 | } |
| 8029 | return fixup((PyUnicodeObject *)unicode, fix_decimal_and_space_to_ascii); |
| 8030 | } |
| 8031 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8032 | PyObject * |
| 8033 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8034 | Py_ssize_t length) |
| 8035 | { |
| 8036 | PyObject *result; |
| 8037 | Py_UNICODE *p; /* write pointer into result */ |
| 8038 | Py_ssize_t i; |
| 8039 | /* Copy to a new string */ |
| 8040 | result = (PyObject *)_PyUnicode_New(length); |
| 8041 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8042 | if (result == NULL) |
| 8043 | return result; |
| 8044 | p = PyUnicode_AS_UNICODE(result); |
| 8045 | /* Iterate over code points */ |
| 8046 | for (i = 0; i < length; i++) { |
| 8047 | Py_UNICODE ch =s[i]; |
| 8048 | if (ch > 127) { |
| 8049 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8050 | if (decimal >= 0) |
| 8051 | p[i] = '0' + decimal; |
| 8052 | } |
| 8053 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8054 | if (PyUnicode_READY((PyUnicodeObject*)result) == -1) { |
| 8055 | Py_DECREF(result); |
| 8056 | return NULL; |
| 8057 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8058 | return result; |
| 8059 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8060 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8061 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8062 | int |
| 8063 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8064 | Py_ssize_t length, |
| 8065 | char *output, |
| 8066 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8067 | { |
| 8068 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8069 | PyObject *errorHandler = NULL; |
| 8070 | PyObject *exc = NULL; |
| 8071 | const char *encoding = "decimal"; |
| 8072 | const char *reason = "invalid decimal Unicode string"; |
| 8073 | /* the following variable is used for caching string comparisons |
| 8074 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8075 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8076 | |
| 8077 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8078 | PyErr_BadArgument(); |
| 8079 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8080 | } |
| 8081 | |
| 8082 | p = s; |
| 8083 | end = s + length; |
| 8084 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8085 | register Py_UNICODE ch = *p; |
| 8086 | int decimal; |
| 8087 | PyObject *repunicode; |
| 8088 | Py_ssize_t repsize; |
| 8089 | Py_ssize_t newpos; |
| 8090 | Py_UNICODE *uni2; |
| 8091 | Py_UNICODE *collstart; |
| 8092 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8093 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8094 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8095 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8096 | ++p; |
| 8097 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8098 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8099 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8100 | if (decimal >= 0) { |
| 8101 | *output++ = '0' + decimal; |
| 8102 | ++p; |
| 8103 | continue; |
| 8104 | } |
| 8105 | if (0 < ch && ch < 256) { |
| 8106 | *output++ = (char)ch; |
| 8107 | ++p; |
| 8108 | continue; |
| 8109 | } |
| 8110 | /* All other characters are considered unencodable */ |
| 8111 | collstart = p; |
| 8112 | collend = p+1; |
| 8113 | while (collend < end) { |
| 8114 | if ((0 < *collend && *collend < 256) || |
| 8115 | !Py_UNICODE_ISSPACE(*collend) || |
| 8116 | Py_UNICODE_TODECIMAL(*collend)) |
| 8117 | break; |
| 8118 | } |
| 8119 | /* cache callback name lookup |
| 8120 | * (if not done yet, i.e. it's the first error) */ |
| 8121 | if (known_errorHandler==-1) { |
| 8122 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8123 | known_errorHandler = 1; |
| 8124 | else if (!strcmp(errors, "replace")) |
| 8125 | known_errorHandler = 2; |
| 8126 | else if (!strcmp(errors, "ignore")) |
| 8127 | known_errorHandler = 3; |
| 8128 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8129 | known_errorHandler = 4; |
| 8130 | else |
| 8131 | known_errorHandler = 0; |
| 8132 | } |
| 8133 | switch (known_errorHandler) { |
| 8134 | case 1: /* strict */ |
| 8135 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8136 | goto onError; |
| 8137 | case 2: /* replace */ |
| 8138 | for (p = collstart; p < collend; ++p) |
| 8139 | *output++ = '?'; |
| 8140 | /* fall through */ |
| 8141 | case 3: /* ignore */ |
| 8142 | p = collend; |
| 8143 | break; |
| 8144 | case 4: /* xmlcharrefreplace */ |
| 8145 | /* generate replacement (temporarily (mis)uses p) */ |
| 8146 | for (p = collstart; p < collend; ++p) |
| 8147 | output += sprintf(output, "&#%d;", (int)*p); |
| 8148 | p = collend; |
| 8149 | break; |
| 8150 | default: |
| 8151 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 8152 | encoding, reason, s, length, &exc, |
| 8153 | collstart-s, collend-s, &newpos); |
| 8154 | if (repunicode == NULL) |
| 8155 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8156 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8157 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8158 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8159 | Py_DECREF(repunicode); |
| 8160 | goto onError; |
| 8161 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8162 | /* generate replacement */ |
| 8163 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8164 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8165 | Py_UNICODE ch = *uni2; |
| 8166 | if (Py_UNICODE_ISSPACE(ch)) |
| 8167 | *output++ = ' '; |
| 8168 | else { |
| 8169 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8170 | if (decimal >= 0) |
| 8171 | *output++ = '0' + decimal; |
| 8172 | else if (0 < ch && ch < 256) |
| 8173 | *output++ = (char)ch; |
| 8174 | else { |
| 8175 | Py_DECREF(repunicode); |
| 8176 | raise_encode_exception(&exc, encoding, |
| 8177 | s, length, collstart-s, collend-s, reason); |
| 8178 | goto onError; |
| 8179 | } |
| 8180 | } |
| 8181 | } |
| 8182 | p = s + newpos; |
| 8183 | Py_DECREF(repunicode); |
| 8184 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8185 | } |
| 8186 | /* 0-terminate the output string */ |
| 8187 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8188 | Py_XDECREF(exc); |
| 8189 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8190 | return 0; |
| 8191 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8192 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8193 | Py_XDECREF(exc); |
| 8194 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8195 | return -1; |
| 8196 | } |
| 8197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8198 | /* --- Helpers ------------------------------------------------------------ */ |
| 8199 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8200 | #include "stringlib/ucs1lib.h" |
| 8201 | #include "stringlib/fastsearch.h" |
| 8202 | #include "stringlib/partition.h" |
| 8203 | #include "stringlib/split.h" |
| 8204 | #include "stringlib/count.h" |
| 8205 | #include "stringlib/find.h" |
| 8206 | #include "stringlib/localeutil.h" |
| 8207 | #include "stringlib/undef.h" |
| 8208 | |
| 8209 | #include "stringlib/ucs2lib.h" |
| 8210 | #include "stringlib/fastsearch.h" |
| 8211 | #include "stringlib/partition.h" |
| 8212 | #include "stringlib/split.h" |
| 8213 | #include "stringlib/count.h" |
| 8214 | #include "stringlib/find.h" |
| 8215 | #include "stringlib/localeutil.h" |
| 8216 | #include "stringlib/undef.h" |
| 8217 | |
| 8218 | #include "stringlib/ucs4lib.h" |
| 8219 | #include "stringlib/fastsearch.h" |
| 8220 | #include "stringlib/partition.h" |
| 8221 | #include "stringlib/split.h" |
| 8222 | #include "stringlib/count.h" |
| 8223 | #include "stringlib/find.h" |
| 8224 | #include "stringlib/localeutil.h" |
| 8225 | #include "stringlib/undef.h" |
| 8226 | |
| 8227 | static Py_ssize_t |
| 8228 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, |
| 8229 | const Py_UCS1*, Py_ssize_t, |
| 8230 | Py_ssize_t, Py_ssize_t), |
| 8231 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 8232 | const Py_UCS2*, Py_ssize_t, |
| 8233 | Py_ssize_t, Py_ssize_t), |
| 8234 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 8235 | const Py_UCS4*, Py_ssize_t, |
| 8236 | Py_ssize_t, Py_ssize_t), |
| 8237 | PyObject* s1, PyObject* s2, |
| 8238 | Py_ssize_t start, |
| 8239 | Py_ssize_t end) |
| 8240 | { |
| 8241 | int kind1, kind2, kind; |
| 8242 | void *buf1, *buf2; |
| 8243 | Py_ssize_t len1, len2, result; |
| 8244 | |
| 8245 | kind1 = PyUnicode_KIND(s1); |
| 8246 | kind2 = PyUnicode_KIND(s2); |
| 8247 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8248 | buf1 = PyUnicode_DATA(s1); |
| 8249 | buf2 = PyUnicode_DATA(s2); |
| 8250 | if (kind1 != kind) |
| 8251 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8252 | if (!buf1) |
| 8253 | return -2; |
| 8254 | if (kind2 != kind) |
| 8255 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8256 | if (!buf2) { |
| 8257 | if (kind1 != kind) PyMem_Free(buf1); |
| 8258 | return -2; |
| 8259 | } |
| 8260 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8261 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8262 | |
| 8263 | switch(kind) { |
| 8264 | case PyUnicode_1BYTE_KIND: |
| 8265 | result = ucs1(buf1, len1, buf2, len2, start, end); |
| 8266 | break; |
| 8267 | case PyUnicode_2BYTE_KIND: |
| 8268 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 8269 | break; |
| 8270 | case PyUnicode_4BYTE_KIND: |
| 8271 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 8272 | break; |
| 8273 | default: |
| 8274 | assert(0); result = -2; |
| 8275 | } |
| 8276 | |
| 8277 | if (kind1 != kind) |
| 8278 | PyMem_Free(buf1); |
| 8279 | if (kind2 != kind) |
| 8280 | PyMem_Free(buf2); |
| 8281 | |
| 8282 | return result; |
| 8283 | } |
| 8284 | |
| 8285 | Py_ssize_t |
| 8286 | _PyUnicode_InsertThousandsGrouping(int kind, void *data, |
| 8287 | Py_ssize_t n_buffer, |
| 8288 | void *digits, Py_ssize_t n_digits, |
| 8289 | Py_ssize_t min_width, |
| 8290 | const char *grouping, |
| 8291 | const char *thousands_sep) |
| 8292 | { |
| 8293 | switch(kind) { |
| 8294 | case PyUnicode_1BYTE_KIND: |
| 8295 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 8296 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8297 | min_width, grouping, thousands_sep); |
| 8298 | case PyUnicode_2BYTE_KIND: |
| 8299 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 8300 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 8301 | min_width, grouping, thousands_sep); |
| 8302 | case PyUnicode_4BYTE_KIND: |
| 8303 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 8304 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 8305 | min_width, grouping, thousands_sep); |
| 8306 | } |
| 8307 | assert(0); |
| 8308 | return -1; |
| 8309 | } |
| 8310 | |
| 8311 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8312 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8313 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8314 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8315 | #include "stringlib/count.h" |
| 8316 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8317 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8318 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8319 | #define ADJUST_INDICES(start, end, len) \ |
| 8320 | if (end > len) \ |
| 8321 | end = len; \ |
| 8322 | else if (end < 0) { \ |
| 8323 | end += len; \ |
| 8324 | if (end < 0) \ |
| 8325 | end = 0; \ |
| 8326 | } \ |
| 8327 | if (start < 0) { \ |
| 8328 | start += len; \ |
| 8329 | if (start < 0) \ |
| 8330 | start = 0; \ |
| 8331 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8332 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8333 | Py_ssize_t |
| 8334 | PyUnicode_Count(PyObject *str, |
| 8335 | PyObject *substr, |
| 8336 | Py_ssize_t start, |
| 8337 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8338 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8339 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8340 | PyUnicodeObject* str_obj; |
| 8341 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8342 | int kind1, kind2, kind; |
| 8343 | void *buf1 = NULL, *buf2 = NULL; |
| 8344 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8345 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8346 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8347 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8348 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8349 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8350 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8351 | Py_DECREF(str_obj); |
| 8352 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8353 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8354 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8355 | kind1 = PyUnicode_KIND(str_obj); |
| 8356 | kind2 = PyUnicode_KIND(sub_obj); |
| 8357 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8358 | buf1 = PyUnicode_DATA(str_obj); |
| 8359 | if (kind1 != kind) |
| 8360 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 8361 | if (!buf1) |
| 8362 | goto onError; |
| 8363 | buf2 = PyUnicode_DATA(sub_obj); |
| 8364 | if (kind2 != kind) |
| 8365 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 8366 | if (!buf2) |
| 8367 | goto onError; |
| 8368 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8369 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8370 | |
| 8371 | ADJUST_INDICES(start, end, len1); |
| 8372 | switch(kind) { |
| 8373 | case PyUnicode_1BYTE_KIND: |
| 8374 | result = ucs1lib_count( |
| 8375 | ((Py_UCS1*)buf1) + start, end - start, |
| 8376 | buf2, len2, PY_SSIZE_T_MAX |
| 8377 | ); |
| 8378 | break; |
| 8379 | case PyUnicode_2BYTE_KIND: |
| 8380 | result = ucs2lib_count( |
| 8381 | ((Py_UCS2*)buf1) + start, end - start, |
| 8382 | buf2, len2, PY_SSIZE_T_MAX |
| 8383 | ); |
| 8384 | break; |
| 8385 | case PyUnicode_4BYTE_KIND: |
| 8386 | result = ucs4lib_count( |
| 8387 | ((Py_UCS4*)buf1) + start, end - start, |
| 8388 | buf2, len2, PY_SSIZE_T_MAX |
| 8389 | ); |
| 8390 | break; |
| 8391 | default: |
| 8392 | assert(0); result = 0; |
| 8393 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8394 | |
| 8395 | Py_DECREF(sub_obj); |
| 8396 | Py_DECREF(str_obj); |
| 8397 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8398 | if (kind1 != kind) |
| 8399 | PyMem_Free(buf1); |
| 8400 | if (kind2 != kind) |
| 8401 | PyMem_Free(buf2); |
| 8402 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8403 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8404 | onError: |
| 8405 | Py_DECREF(sub_obj); |
| 8406 | Py_DECREF(str_obj); |
| 8407 | if (kind1 != kind && buf1) |
| 8408 | PyMem_Free(buf1); |
| 8409 | if (kind2 != kind && buf2) |
| 8410 | PyMem_Free(buf2); |
| 8411 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8412 | } |
| 8413 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8414 | Py_ssize_t |
| 8415 | PyUnicode_Find(PyObject *str, |
| 8416 | PyObject *sub, |
| 8417 | Py_ssize_t start, |
| 8418 | Py_ssize_t end, |
| 8419 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8420 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8421 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8422 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8423 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8424 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8425 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8426 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8427 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8428 | Py_DECREF(str); |
| 8429 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8430 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8431 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8432 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8433 | result = any_find_slice( |
| 8434 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 8435 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8436 | ); |
| 8437 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8438 | result = any_find_slice( |
| 8439 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 8440 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8441 | ); |
| 8442 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8443 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8444 | Py_DECREF(sub); |
| 8445 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8446 | return result; |
| 8447 | } |
| 8448 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8449 | Py_ssize_t |
| 8450 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8451 | Py_ssize_t start, Py_ssize_t end, |
| 8452 | int direction) |
| 8453 | { |
| 8454 | char *result; |
| 8455 | int kind; |
| 8456 | if (PyUnicode_READY(str) == -1) |
| 8457 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8458 | if (start < 0 || end < 0) { |
| 8459 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8460 | return -2; |
| 8461 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8462 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8463 | end = PyUnicode_GET_LENGTH(str); |
| 8464 | kind = PyUnicode_KIND(str); |
| 8465 | result = findchar(PyUnicode_1BYTE_DATA(str) |
| 8466 | + PyUnicode_KIND_SIZE(kind, start), |
| 8467 | kind, |
| 8468 | end-start, ch, direction); |
| 8469 | if (!result) |
| 8470 | return -1; |
| 8471 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8472 | } |
| 8473 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8474 | static int |
| 8475 | tailmatch(PyUnicodeObject *self, |
| 8476 | PyUnicodeObject *substring, |
| 8477 | Py_ssize_t start, |
| 8478 | Py_ssize_t end, |
| 8479 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8480 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8481 | int kind_self; |
| 8482 | int kind_sub; |
| 8483 | void *data_self; |
| 8484 | void *data_sub; |
| 8485 | Py_ssize_t offset; |
| 8486 | Py_ssize_t i; |
| 8487 | Py_ssize_t end_sub; |
| 8488 | |
| 8489 | if (PyUnicode_READY(self) == -1 || |
| 8490 | PyUnicode_READY(substring) == -1) |
| 8491 | return 0; |
| 8492 | |
| 8493 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8494 | return 1; |
| 8495 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8496 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8497 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8498 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8499 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8500 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8501 | kind_self = PyUnicode_KIND(self); |
| 8502 | data_self = PyUnicode_DATA(self); |
| 8503 | kind_sub = PyUnicode_KIND(substring); |
| 8504 | data_sub = PyUnicode_DATA(substring); |
| 8505 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8506 | |
| 8507 | if (direction > 0) |
| 8508 | offset = end; |
| 8509 | else |
| 8510 | offset = start; |
| 8511 | |
| 8512 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8513 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8514 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8515 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8516 | /* If both are of the same kind, memcmp is sufficient */ |
| 8517 | if (kind_self == kind_sub) { |
| 8518 | return ! memcmp((char *)data_self + |
| 8519 | (offset * PyUnicode_CHARACTER_SIZE(substring)), |
| 8520 | data_sub, |
| 8521 | PyUnicode_GET_LENGTH(substring) * |
| 8522 | PyUnicode_CHARACTER_SIZE(substring)); |
| 8523 | } |
| 8524 | /* otherwise we have to compare each character by first accesing it */ |
| 8525 | else { |
| 8526 | /* We do not need to compare 0 and len(substring)-1 because |
| 8527 | the if statement above ensured already that they are equal |
| 8528 | when we end up here. */ |
| 8529 | // TODO: honor direction and do a forward or backwards search |
| 8530 | for (i = 1; i < end_sub; ++i) { |
| 8531 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8532 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8533 | return 0; |
| 8534 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8535 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8536 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8537 | } |
| 8538 | |
| 8539 | return 0; |
| 8540 | } |
| 8541 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8542 | Py_ssize_t |
| 8543 | PyUnicode_Tailmatch(PyObject *str, |
| 8544 | PyObject *substr, |
| 8545 | Py_ssize_t start, |
| 8546 | Py_ssize_t end, |
| 8547 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8548 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8549 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8550 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8551 | str = PyUnicode_FromObject(str); |
| 8552 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8553 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8554 | substr = PyUnicode_FromObject(substr); |
| 8555 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8556 | Py_DECREF(str); |
| 8557 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8558 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8559 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8560 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8561 | (PyUnicodeObject *)substr, |
| 8562 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8563 | Py_DECREF(str); |
| 8564 | Py_DECREF(substr); |
| 8565 | return result; |
| 8566 | } |
| 8567 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8568 | /* Apply fixfct filter to the Unicode object self and return a |
| 8569 | reference to the modified object */ |
| 8570 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8571 | static PyObject * |
| 8572 | fixup(PyUnicodeObject *self, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8573 | Py_UCS4 (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8574 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8575 | PyObject *u; |
| 8576 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8577 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8578 | if (PyUnicode_READY(self) == -1) |
| 8579 | return NULL; |
| 8580 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8581 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8582 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8583 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8584 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8585 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8586 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
| 8587 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8588 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8589 | /* fix functions return the new maximum character in a string, |
| 8590 | if the kind of the resulting unicode object does not change, |
| 8591 | everything is fine. Otherwise we need to change the string kind |
| 8592 | and re-run the fix function. */ |
| 8593 | maxchar_new = fixfct((PyUnicodeObject*)u); |
| 8594 | if (maxchar_new == 0) |
| 8595 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8596 | else if (maxchar_new <= 127) |
| 8597 | maxchar_new = 127; |
| 8598 | else if (maxchar_new <= 255) |
| 8599 | maxchar_new = 255; |
| 8600 | else if (maxchar_new <= 65535) |
| 8601 | maxchar_new = 65535; |
| 8602 | else |
| 8603 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8604 | |
| 8605 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8606 | /* fixfct should return TRUE if it modified the buffer. If |
| 8607 | FALSE, return a reference to the original buffer instead |
| 8608 | (to save space, not time) */ |
| 8609 | Py_INCREF(self); |
| 8610 | Py_DECREF(u); |
| 8611 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8612 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8613 | else if (maxchar_new == maxchar_old) { |
| 8614 | return u; |
| 8615 | } |
| 8616 | else { |
| 8617 | /* In case the maximum character changed, we need to |
| 8618 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8619 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8620 | if (v == NULL) { |
| 8621 | Py_DECREF(u); |
| 8622 | return NULL; |
| 8623 | } |
| 8624 | if (maxchar_new > maxchar_old) { |
| 8625 | /* If the maxchar increased so that the kind changed, not all |
| 8626 | characters are representable anymore and we need to fix the |
| 8627 | string again. This only happens in very few cases. */ |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8628 | if (PyUnicode_CopyCharacters(v, 0, |
| 8629 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8630 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8631 | { |
| 8632 | Py_DECREF(u); |
| 8633 | return NULL; |
| 8634 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8635 | maxchar_old = fixfct((PyUnicodeObject*)v); |
| 8636 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8637 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8638 | else { |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8639 | if (PyUnicode_CopyCharacters(v, 0, |
| 8640 | u, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8641 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8642 | { |
| 8643 | Py_DECREF(u); |
| 8644 | return NULL; |
| 8645 | } |
| 8646 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8647 | |
| 8648 | Py_DECREF(u); |
| 8649 | return v; |
| 8650 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8651 | } |
| 8652 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8653 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8654 | fixupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8655 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8656 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8657 | called as a callback from fixup() which does it already. */ |
| 8658 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8659 | const int kind = PyUnicode_KIND(self); |
| 8660 | void *data = PyUnicode_DATA(self); |
| 8661 | int touched = 0; |
| 8662 | Py_UCS4 maxchar = 0; |
| 8663 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8664 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8665 | for (i = 0; i < len; ++i) { |
| 8666 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8667 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8668 | if (up != ch) { |
| 8669 | if (up > maxchar) |
| 8670 | maxchar = up; |
| 8671 | PyUnicode_WRITE(kind, data, i, up); |
| 8672 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8673 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8674 | else if (ch > maxchar) |
| 8675 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8676 | } |
| 8677 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8678 | if (touched) |
| 8679 | return maxchar; |
| 8680 | else |
| 8681 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8682 | } |
| 8683 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8684 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8685 | fixlower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8686 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8687 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8688 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8689 | const int kind = PyUnicode_KIND(self); |
| 8690 | void *data = PyUnicode_DATA(self); |
| 8691 | int touched = 0; |
| 8692 | Py_UCS4 maxchar = 0; |
| 8693 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8694 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8695 | for(i = 0; i < len; ++i) { |
| 8696 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8697 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8698 | if (lo != ch) { |
| 8699 | if (lo > maxchar) |
| 8700 | maxchar = lo; |
| 8701 | PyUnicode_WRITE(kind, data, i, lo); |
| 8702 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8703 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8704 | else if (ch > maxchar) |
| 8705 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8706 | } |
| 8707 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8708 | if (touched) |
| 8709 | return maxchar; |
| 8710 | else |
| 8711 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8712 | } |
| 8713 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8714 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8715 | fixswapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8716 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8717 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8718 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8719 | const int kind = PyUnicode_KIND(self); |
| 8720 | void *data = PyUnicode_DATA(self); |
| 8721 | int touched = 0; |
| 8722 | Py_UCS4 maxchar = 0; |
| 8723 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8724 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8725 | for(i = 0; i < len; ++i) { |
| 8726 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8727 | Py_UCS4 nu = 0; |
| 8728 | |
| 8729 | if (Py_UNICODE_ISUPPER(ch)) |
| 8730 | nu = Py_UNICODE_TOLOWER(ch); |
| 8731 | else if (Py_UNICODE_ISLOWER(ch)) |
| 8732 | nu = Py_UNICODE_TOUPPER(ch); |
| 8733 | |
| 8734 | if (nu != 0) { |
| 8735 | if (nu > maxchar) |
| 8736 | maxchar = nu; |
| 8737 | PyUnicode_WRITE(kind, data, i, nu); |
| 8738 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8739 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8740 | else if (ch > maxchar) |
| 8741 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8742 | } |
| 8743 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8744 | if (touched) |
| 8745 | return maxchar; |
| 8746 | else |
| 8747 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8748 | } |
| 8749 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8750 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8751 | fixcapitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8752 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8753 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8754 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8755 | const int kind = PyUnicode_KIND(self); |
| 8756 | void *data = PyUnicode_DATA(self); |
| 8757 | int touched = 0; |
| 8758 | Py_UCS4 maxchar = 0; |
| 8759 | Py_ssize_t i = 0; |
| 8760 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8761 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8762 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8763 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8764 | |
| 8765 | ch = PyUnicode_READ(kind, data, i); |
| 8766 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 8767 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 8768 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 8769 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8770 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8771 | ++i; |
| 8772 | for(; i < len; ++i) { |
| 8773 | ch = PyUnicode_READ(kind, data, i); |
| 8774 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 8775 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8776 | if (lo > maxchar) |
| 8777 | maxchar = lo; |
| 8778 | PyUnicode_WRITE(kind, data, i, lo); |
| 8779 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8780 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8781 | else if (ch > maxchar) |
| 8782 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8783 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8784 | |
| 8785 | if (touched) |
| 8786 | return maxchar; |
| 8787 | else |
| 8788 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8789 | } |
| 8790 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8791 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8792 | fixtitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8793 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8794 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8795 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8796 | const int kind = PyUnicode_KIND(self); |
| 8797 | void *data = PyUnicode_DATA(self); |
| 8798 | Py_UCS4 maxchar = 0; |
| 8799 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8800 | int previous_is_cased; |
| 8801 | |
| 8802 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8803 | if (len == 1) { |
| 8804 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8805 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 8806 | if (ti != ch) { |
| 8807 | PyUnicode_WRITE(kind, data, i, ti); |
| 8808 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8809 | } |
| 8810 | else |
| 8811 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8812 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8813 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8814 | for(; i < len; ++i) { |
| 8815 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8816 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8817 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8818 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8819 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8820 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8821 | nu = Py_UNICODE_TOTITLE(ch); |
| 8822 | |
| 8823 | if (nu > maxchar) |
| 8824 | maxchar = nu; |
| 8825 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8826 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8827 | if (Py_UNICODE_ISLOWER(ch) || |
| 8828 | Py_UNICODE_ISUPPER(ch) || |
| 8829 | Py_UNICODE_ISTITLE(ch)) |
| 8830 | previous_is_cased = 1; |
| 8831 | else |
| 8832 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8833 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8834 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8835 | } |
| 8836 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8837 | PyObject * |
| 8838 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8839 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8840 | PyObject *sep = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8841 | Py_ssize_t seplen = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8842 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8843 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8844 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 8845 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8846 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8847 | Py_ssize_t sz, i, res_offset; |
| 8848 | Py_UCS4 maxchar = 0; |
| 8849 | Py_UCS4 item_maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8850 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8851 | fseq = PySequence_Fast(seq, ""); |
| 8852 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8853 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8854 | } |
| 8855 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8856 | /* NOTE: the following code can't call back into Python code, |
| 8857 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8858 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8859 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8860 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 8861 | /* If empty sequence, return u"". */ |
| 8862 | if (seqlen == 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8863 | res = PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8864 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8865 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8866 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8867 | /* If singleton sequence with an exact Unicode, return that. */ |
| 8868 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8869 | item = items[0]; |
| 8870 | if (PyUnicode_CheckExact(item)) { |
| 8871 | Py_INCREF(item); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8872 | res = item; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8873 | goto Done; |
| 8874 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8875 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8876 | else { |
| 8877 | /* Set up sep and seplen */ |
| 8878 | if (separator == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8879 | /* fall back to a blank space separator */ |
| 8880 | sep = PyUnicode_FromOrdinal(' '); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8881 | if (!sep) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8882 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8883 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8884 | else { |
| 8885 | if (!PyUnicode_Check(separator)) { |
| 8886 | PyErr_Format(PyExc_TypeError, |
| 8887 | "separator: expected str instance," |
| 8888 | " %.80s found", |
| 8889 | Py_TYPE(separator)->tp_name); |
| 8890 | goto onError; |
| 8891 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8892 | if (PyUnicode_READY(separator)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8893 | goto onError; |
| 8894 | sep = separator; |
| 8895 | seplen = PyUnicode_GET_LENGTH(separator); |
| 8896 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 8897 | /* inc refcount to keep this code path symetric with the |
| 8898 | above case of a blank separator */ |
| 8899 | Py_INCREF(sep); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8900 | } |
| 8901 | } |
| 8902 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8903 | /* There are at least two things to join, or else we have a subclass |
| 8904 | * of str in the sequence. |
| 8905 | * Do a pre-pass to figure out the total amount of space we'll |
| 8906 | * need (sz), and see whether all argument are strings. |
| 8907 | */ |
| 8908 | sz = 0; |
| 8909 | for (i = 0; i < seqlen; i++) { |
| 8910 | const Py_ssize_t old_sz = sz; |
| 8911 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8912 | if (!PyUnicode_Check(item)) { |
| 8913 | PyErr_Format(PyExc_TypeError, |
| 8914 | "sequence item %zd: expected str instance," |
| 8915 | " %.80s found", |
| 8916 | i, Py_TYPE(item)->tp_name); |
| 8917 | goto onError; |
| 8918 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8919 | if (PyUnicode_READY(item) == -1) |
| 8920 | goto onError; |
| 8921 | sz += PyUnicode_GET_LENGTH(item); |
| 8922 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
| 8923 | if (item_maxchar > maxchar) |
| 8924 | maxchar = item_maxchar; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8925 | if (i != 0) |
| 8926 | sz += seplen; |
| 8927 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 8928 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8929 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8930 | goto onError; |
| 8931 | } |
| 8932 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8933 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8934 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8935 | if (res == NULL) |
| 8936 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8937 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8938 | /* Catenate everything. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8939 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8940 | Py_ssize_t itemlen, copied; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8941 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8942 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8943 | if (i && seplen != 0) { |
| 8944 | copied = PyUnicode_CopyCharacters(res, res_offset, |
| 8945 | sep, 0, seplen); |
| 8946 | if (copied < 0) |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8947 | goto onError; |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8948 | #ifdef Py_DEBUG |
| 8949 | res_offset += copied; |
| 8950 | #else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8951 | res_offset += seplen; |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8952 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8953 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8954 | itemlen = PyUnicode_GET_LENGTH(item); |
| 8955 | if (itemlen != 0) { |
| 8956 | copied = PyUnicode_CopyCharacters(res, res_offset, |
| 8957 | item, 0, itemlen); |
| 8958 | if (copied < 0) |
| 8959 | goto onError; |
| 8960 | #ifdef Py_DEBUG |
| 8961 | res_offset += copied; |
| 8962 | #else |
| 8963 | res_offset += itemlen; |
| 8964 | #endif |
| 8965 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8966 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8967 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8968 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8969 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8970 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8971 | Py_XDECREF(sep); |
| 8972 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8973 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8974 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8975 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8976 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8977 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8978 | return NULL; |
| 8979 | } |
| 8980 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8981 | #define FILL(kind, data, value, start, length) \ |
| 8982 | do { \ |
| 8983 | Py_ssize_t i_ = 0; \ |
| 8984 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 8985 | switch ((kind)) { \ |
| 8986 | case PyUnicode_1BYTE_KIND: { \ |
| 8987 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 8988 | memset(to_, (unsigned char)value, length); \ |
| 8989 | break; \ |
| 8990 | } \ |
| 8991 | case PyUnicode_2BYTE_KIND: { \ |
| 8992 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 8993 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8994 | break; \ |
| 8995 | } \ |
| 8996 | default: { \ |
| 8997 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 8998 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8999 | break; \ |
| 9000 | } \ |
| 9001 | } \ |
| 9002 | } while (0) |
| 9003 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9004 | static PyUnicodeObject * |
| 9005 | pad(PyUnicodeObject *self, |
| 9006 | Py_ssize_t left, |
| 9007 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9008 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9009 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9010 | PyObject *u; |
| 9011 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9012 | int kind; |
| 9013 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9014 | |
| 9015 | if (left < 0) |
| 9016 | left = 0; |
| 9017 | if (right < 0) |
| 9018 | right = 0; |
| 9019 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9020 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9021 | Py_INCREF(self); |
| 9022 | return self; |
| 9023 | } |
| 9024 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9025 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9026 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9027 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9028 | return NULL; |
| 9029 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9030 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9031 | if (fill > maxchar) |
| 9032 | maxchar = fill; |
| 9033 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9034 | if (!u) |
| 9035 | return NULL; |
| 9036 | |
| 9037 | kind = PyUnicode_KIND(u); |
| 9038 | data = PyUnicode_DATA(u); |
| 9039 | if (left) |
| 9040 | FILL(kind, data, fill, 0, left); |
| 9041 | if (right) |
| 9042 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9043 | if (PyUnicode_CopyCharacters(u, left, |
| 9044 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9045 | _PyUnicode_LENGTH(self)) < 0) |
| 9046 | { |
| 9047 | Py_DECREF(u); |
| 9048 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9049 | } |
| 9050 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9051 | return (PyUnicodeObject*)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9052 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9053 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9054 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9055 | PyObject * |
| 9056 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9057 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9058 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9059 | |
| 9060 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9061 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9062 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9063 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9064 | switch(PyUnicode_KIND(string)) { |
| 9065 | case PyUnicode_1BYTE_KIND: |
| 9066 | list = ucs1lib_splitlines( |
| 9067 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9068 | PyUnicode_GET_LENGTH(string), keepends); |
| 9069 | break; |
| 9070 | case PyUnicode_2BYTE_KIND: |
| 9071 | list = ucs2lib_splitlines( |
| 9072 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 9073 | PyUnicode_GET_LENGTH(string), keepends); |
| 9074 | break; |
| 9075 | case PyUnicode_4BYTE_KIND: |
| 9076 | list = ucs4lib_splitlines( |
| 9077 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 9078 | PyUnicode_GET_LENGTH(string), keepends); |
| 9079 | break; |
| 9080 | default: |
| 9081 | assert(0); |
| 9082 | list = 0; |
| 9083 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9084 | Py_DECREF(string); |
| 9085 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9086 | } |
| 9087 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9088 | static PyObject * |
| 9089 | split(PyUnicodeObject *self, |
| 9090 | PyUnicodeObject *substring, |
| 9091 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9092 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9093 | int kind1, kind2, kind; |
| 9094 | void *buf1, *buf2; |
| 9095 | Py_ssize_t len1, len2; |
| 9096 | PyObject* out; |
| 9097 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9098 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9099 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9100 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9101 | if (PyUnicode_READY(self) == -1) |
| 9102 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9103 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9104 | if (substring == NULL) |
| 9105 | switch(PyUnicode_KIND(self)) { |
| 9106 | case PyUnicode_1BYTE_KIND: |
| 9107 | return ucs1lib_split_whitespace( |
| 9108 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9109 | PyUnicode_GET_LENGTH(self), maxcount |
| 9110 | ); |
| 9111 | case PyUnicode_2BYTE_KIND: |
| 9112 | return ucs2lib_split_whitespace( |
| 9113 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9114 | PyUnicode_GET_LENGTH(self), maxcount |
| 9115 | ); |
| 9116 | case PyUnicode_4BYTE_KIND: |
| 9117 | return ucs4lib_split_whitespace( |
| 9118 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9119 | PyUnicode_GET_LENGTH(self), maxcount |
| 9120 | ); |
| 9121 | default: |
| 9122 | assert(0); |
| 9123 | return NULL; |
| 9124 | } |
| 9125 | |
| 9126 | if (PyUnicode_READY(substring) == -1) |
| 9127 | return NULL; |
| 9128 | |
| 9129 | kind1 = PyUnicode_KIND(self); |
| 9130 | kind2 = PyUnicode_KIND(substring); |
| 9131 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9132 | buf1 = PyUnicode_DATA(self); |
| 9133 | buf2 = PyUnicode_DATA(substring); |
| 9134 | if (kind1 != kind) |
| 9135 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9136 | if (!buf1) |
| 9137 | return NULL; |
| 9138 | if (kind2 != kind) |
| 9139 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9140 | if (!buf2) { |
| 9141 | if (kind1 != kind) PyMem_Free(buf1); |
| 9142 | return NULL; |
| 9143 | } |
| 9144 | len1 = PyUnicode_GET_LENGTH(self); |
| 9145 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9146 | |
| 9147 | switch(kind) { |
| 9148 | case PyUnicode_1BYTE_KIND: |
| 9149 | out = ucs1lib_split( |
| 9150 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9151 | break; |
| 9152 | case PyUnicode_2BYTE_KIND: |
| 9153 | out = ucs2lib_split( |
| 9154 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9155 | break; |
| 9156 | case PyUnicode_4BYTE_KIND: |
| 9157 | out = ucs4lib_split( |
| 9158 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9159 | break; |
| 9160 | default: |
| 9161 | out = NULL; |
| 9162 | } |
| 9163 | if (kind1 != kind) |
| 9164 | PyMem_Free(buf1); |
| 9165 | if (kind2 != kind) |
| 9166 | PyMem_Free(buf2); |
| 9167 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9168 | } |
| 9169 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9170 | static PyObject * |
| 9171 | rsplit(PyUnicodeObject *self, |
| 9172 | PyUnicodeObject *substring, |
| 9173 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9174 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9175 | int kind1, kind2, kind; |
| 9176 | void *buf1, *buf2; |
| 9177 | Py_ssize_t len1, len2; |
| 9178 | PyObject* out; |
| 9179 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9180 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9181 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9182 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9183 | if (PyUnicode_READY(self) == -1) |
| 9184 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9185 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9186 | if (substring == NULL) |
| 9187 | switch(PyUnicode_KIND(self)) { |
| 9188 | case PyUnicode_1BYTE_KIND: |
| 9189 | return ucs1lib_rsplit_whitespace( |
| 9190 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9191 | PyUnicode_GET_LENGTH(self), maxcount |
| 9192 | ); |
| 9193 | case PyUnicode_2BYTE_KIND: |
| 9194 | return ucs2lib_rsplit_whitespace( |
| 9195 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9196 | PyUnicode_GET_LENGTH(self), maxcount |
| 9197 | ); |
| 9198 | case PyUnicode_4BYTE_KIND: |
| 9199 | return ucs4lib_rsplit_whitespace( |
| 9200 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9201 | PyUnicode_GET_LENGTH(self), maxcount |
| 9202 | ); |
| 9203 | default: |
| 9204 | assert(0); |
| 9205 | return NULL; |
| 9206 | } |
| 9207 | |
| 9208 | if (PyUnicode_READY(substring) == -1) |
| 9209 | return NULL; |
| 9210 | |
| 9211 | kind1 = PyUnicode_KIND(self); |
| 9212 | kind2 = PyUnicode_KIND(substring); |
| 9213 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9214 | buf1 = PyUnicode_DATA(self); |
| 9215 | buf2 = PyUnicode_DATA(substring); |
| 9216 | if (kind1 != kind) |
| 9217 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9218 | if (!buf1) |
| 9219 | return NULL; |
| 9220 | if (kind2 != kind) |
| 9221 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9222 | if (!buf2) { |
| 9223 | if (kind1 != kind) PyMem_Free(buf1); |
| 9224 | return NULL; |
| 9225 | } |
| 9226 | len1 = PyUnicode_GET_LENGTH(self); |
| 9227 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9228 | |
| 9229 | switch(kind) { |
| 9230 | case PyUnicode_1BYTE_KIND: |
| 9231 | out = ucs1lib_rsplit( |
| 9232 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9233 | break; |
| 9234 | case PyUnicode_2BYTE_KIND: |
| 9235 | out = ucs2lib_rsplit( |
| 9236 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9237 | break; |
| 9238 | case PyUnicode_4BYTE_KIND: |
| 9239 | out = ucs4lib_rsplit( |
| 9240 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9241 | break; |
| 9242 | default: |
| 9243 | out = NULL; |
| 9244 | } |
| 9245 | if (kind1 != kind) |
| 9246 | PyMem_Free(buf1); |
| 9247 | if (kind2 != kind) |
| 9248 | PyMem_Free(buf2); |
| 9249 | return out; |
| 9250 | } |
| 9251 | |
| 9252 | static Py_ssize_t |
| 9253 | anylib_find(int kind, void *buf1, Py_ssize_t len1, |
| 9254 | void *buf2, Py_ssize_t len2, Py_ssize_t offset) |
| 9255 | { |
| 9256 | switch(kind) { |
| 9257 | case PyUnicode_1BYTE_KIND: |
| 9258 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
| 9259 | case PyUnicode_2BYTE_KIND: |
| 9260 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9261 | case PyUnicode_4BYTE_KIND: |
| 9262 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9263 | } |
| 9264 | assert(0); |
| 9265 | return -1; |
| 9266 | } |
| 9267 | |
| 9268 | static Py_ssize_t |
| 9269 | anylib_count(int kind, void* sbuf, Py_ssize_t slen, |
| 9270 | void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) |
| 9271 | { |
| 9272 | switch(kind) { |
| 9273 | case PyUnicode_1BYTE_KIND: |
| 9274 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9275 | case PyUnicode_2BYTE_KIND: |
| 9276 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9277 | case PyUnicode_4BYTE_KIND: |
| 9278 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9279 | } |
| 9280 | assert(0); |
| 9281 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9282 | } |
| 9283 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9284 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9285 | replace(PyObject *self, PyObject *str1, |
| 9286 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9287 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9288 | PyObject *u; |
| 9289 | char *sbuf = PyUnicode_DATA(self); |
| 9290 | char *buf1 = PyUnicode_DATA(str1); |
| 9291 | char *buf2 = PyUnicode_DATA(str2); |
| 9292 | int srelease = 0, release1 = 0, release2 = 0; |
| 9293 | int skind = PyUnicode_KIND(self); |
| 9294 | int kind1 = PyUnicode_KIND(str1); |
| 9295 | int kind2 = PyUnicode_KIND(str2); |
| 9296 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9297 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9298 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9299 | |
| 9300 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9301 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9302 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9303 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9304 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9305 | if (skind < kind1) |
| 9306 | /* substring too wide to be present */ |
| 9307 | goto nothing; |
| 9308 | |
| 9309 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 9310 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9311 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9312 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9313 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9314 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9315 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9316 | Py_UCS4 u1, u2, maxchar; |
| 9317 | int mayshrink, rkind; |
| 9318 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 9319 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 9320 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9321 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9322 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 9323 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9324 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 9325 | result string. */ |
| 9326 | mayshrink = maxchar > 127; |
| 9327 | if (u2 > maxchar) { |
| 9328 | maxchar = u2; |
| 9329 | mayshrink = 0; |
| 9330 | } |
| 9331 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9332 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9333 | goto error; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9334 | if (PyUnicode_CopyCharacters(u, 0, |
| 9335 | (PyObject*)self, 0, slen) < 0) |
| 9336 | { |
| 9337 | Py_DECREF(u); |
| 9338 | return NULL; |
| 9339 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9340 | rkind = PyUnicode_KIND(u); |
| 9341 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 9342 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9343 | if (--maxcount < 0) |
| 9344 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9345 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9346 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9347 | if (mayshrink) { |
| 9348 | PyObject *tmp = u; |
| 9349 | u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp), |
| 9350 | PyUnicode_GET_LENGTH(tmp)); |
| 9351 | Py_DECREF(tmp); |
| 9352 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9353 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9354 | int rkind = skind; |
| 9355 | char *res; |
| 9356 | if (kind1 < rkind) { |
| 9357 | /* widen substring */ |
| 9358 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9359 | if (!buf1) goto error; |
| 9360 | release1 = 1; |
| 9361 | } |
| 9362 | i = anylib_find(rkind, sbuf, slen, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9363 | if (i < 0) |
| 9364 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9365 | if (rkind > kind2) { |
| 9366 | /* widen replacement */ |
| 9367 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9368 | if (!buf2) goto error; |
| 9369 | release2 = 1; |
| 9370 | } |
| 9371 | else if (rkind < kind2) { |
| 9372 | /* widen self and buf1 */ |
| 9373 | rkind = kind2; |
| 9374 | if (release1) PyMem_Free(buf1); |
| 9375 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9376 | if (!sbuf) goto error; |
| 9377 | srelease = 1; |
| 9378 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9379 | if (!buf1) goto error; |
| 9380 | release1 = 1; |
| 9381 | } |
| 9382 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen)); |
| 9383 | if (!res) { |
| 9384 | PyErr_NoMemory(); |
| 9385 | goto error; |
| 9386 | } |
| 9387 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9388 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9389 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9390 | buf2, |
| 9391 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9392 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9393 | |
| 9394 | while ( --maxcount > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9395 | i = anylib_find(rkind, sbuf+PyUnicode_KIND_SIZE(rkind, i), |
| 9396 | slen-i, |
| 9397 | buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9398 | if (i == -1) |
| 9399 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9400 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9401 | buf2, |
| 9402 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9403 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9404 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9405 | |
| 9406 | u = PyUnicode_FromKindAndData(rkind, res, slen); |
| 9407 | PyMem_Free(res); |
| 9408 | if (!u) goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9409 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9410 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9411 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9412 | Py_ssize_t n, i, j, ires; |
| 9413 | Py_ssize_t product, new_size; |
| 9414 | int rkind = skind; |
| 9415 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9416 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9417 | if (kind1 < rkind) { |
| 9418 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9419 | if (!buf1) goto error; |
| 9420 | release1 = 1; |
| 9421 | } |
| 9422 | n = anylib_count(rkind, sbuf, slen, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9423 | if (n == 0) |
| 9424 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9425 | if (kind2 < rkind) { |
| 9426 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9427 | if (!buf2) goto error; |
| 9428 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9429 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9430 | else if (kind2 > rkind) { |
| 9431 | rkind = kind2; |
| 9432 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9433 | if (!sbuf) goto error; |
| 9434 | srelease = 1; |
| 9435 | if (release1) PyMem_Free(buf1); |
| 9436 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9437 | if (!buf1) goto error; |
| 9438 | release1 = 1; |
| 9439 | } |
| 9440 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9441 | PyUnicode_GET_LENGTH(str1))); */ |
| 9442 | product = n * (len2-len1); |
| 9443 | if ((product / (len2-len1)) != n) { |
| 9444 | PyErr_SetString(PyExc_OverflowError, |
| 9445 | "replace string is too long"); |
| 9446 | goto error; |
| 9447 | } |
| 9448 | new_size = slen + product; |
| 9449 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9450 | PyErr_SetString(PyExc_OverflowError, |
| 9451 | "replace string is too long"); |
| 9452 | goto error; |
| 9453 | } |
| 9454 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size)); |
| 9455 | if (!res) |
| 9456 | goto error; |
| 9457 | ires = i = 0; |
| 9458 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9459 | while (n-- > 0) { |
| 9460 | /* look for next match */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9461 | j = anylib_find(rkind, |
| 9462 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9463 | slen-i, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9464 | if (j == -1) |
| 9465 | break; |
| 9466 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9467 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9468 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9469 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9470 | PyUnicode_KIND_SIZE(rkind, j-i)); |
| 9471 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9472 | } |
| 9473 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9474 | if (len2 > 0) { |
| 9475 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9476 | buf2, |
| 9477 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9478 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9479 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9480 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9481 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9482 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9483 | /* copy tail [i:] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9484 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9485 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9486 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9487 | } else { |
| 9488 | /* interleave */ |
| 9489 | while (n > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9490 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9491 | buf2, |
| 9492 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9493 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9494 | if (--n <= 0) |
| 9495 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9496 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9497 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9498 | PyUnicode_KIND_SIZE(rkind, 1)); |
| 9499 | ires++; |
| 9500 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9501 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9502 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9503 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9504 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9505 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9506 | u = PyUnicode_FromKindAndData(rkind, res, new_size); |
Martin v. Löwis | 0b1d348 | 2011-10-01 16:35:40 +0200 | [diff] [blame] | 9507 | PyMem_Free(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9508 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9509 | if (srelease) |
| 9510 | PyMem_FREE(sbuf); |
| 9511 | if (release1) |
| 9512 | PyMem_FREE(buf1); |
| 9513 | if (release2) |
| 9514 | PyMem_FREE(buf2); |
| 9515 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9516 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9517 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9518 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9519 | if (srelease) |
| 9520 | PyMem_FREE(sbuf); |
| 9521 | if (release1) |
| 9522 | PyMem_FREE(buf1); |
| 9523 | if (release2) |
| 9524 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9525 | if (PyUnicode_CheckExact(self)) { |
| 9526 | Py_INCREF(self); |
| 9527 | return (PyObject *) self; |
| 9528 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9529 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9530 | error: |
| 9531 | if (srelease && sbuf) |
| 9532 | PyMem_FREE(sbuf); |
| 9533 | if (release1 && buf1) |
| 9534 | PyMem_FREE(buf1); |
| 9535 | if (release2 && buf2) |
| 9536 | PyMem_FREE(buf2); |
| 9537 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9538 | } |
| 9539 | |
| 9540 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9541 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9542 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9543 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9544 | \n\ |
| 9545 | 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] | 9546 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9547 | |
| 9548 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9549 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9550 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9551 | return fixup(self, fixtitle); |
| 9552 | } |
| 9553 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9554 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9555 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9556 | \n\ |
| 9557 | 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] | 9558 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9559 | |
| 9560 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9561 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9562 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9563 | return fixup(self, fixcapitalize); |
| 9564 | } |
| 9565 | |
| 9566 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9567 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9568 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9569 | \n\ |
| 9570 | Apply .capitalize() to all words in S and return the result with\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9571 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9572 | |
| 9573 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9574 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9575 | { |
| 9576 | PyObject *list; |
| 9577 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9578 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9580 | /* Split into words */ |
| 9581 | list = split(self, NULL, -1); |
| 9582 | if (!list) |
| 9583 | return NULL; |
| 9584 | |
| 9585 | /* Capitalize each word */ |
| 9586 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9587 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9588 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9589 | if (item == NULL) |
| 9590 | goto onError; |
| 9591 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9592 | PyList_SET_ITEM(list, i, item); |
| 9593 | } |
| 9594 | |
| 9595 | /* Join the words to form a new string */ |
| 9596 | item = PyUnicode_Join(NULL, list); |
| 9597 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9598 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9599 | Py_DECREF(list); |
| 9600 | return (PyObject *)item; |
| 9601 | } |
| 9602 | #endif |
| 9603 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9604 | /* Argument converter. Coerces to a single unicode character */ |
| 9605 | |
| 9606 | static int |
| 9607 | convert_uc(PyObject *obj, void *addr) |
| 9608 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9609 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9610 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9611 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9612 | uniobj = PyUnicode_FromObject(obj); |
| 9613 | if (uniobj == NULL) { |
| 9614 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9615 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9616 | return 0; |
| 9617 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9618 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9619 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9620 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9621 | Py_DECREF(uniobj); |
| 9622 | return 0; |
| 9623 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9624 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9625 | Py_DECREF(uniobj); |
| 9626 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9627 | } |
| 9628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9629 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9630 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9631 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9632 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9633 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9634 | |
| 9635 | static PyObject * |
| 9636 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 9637 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9638 | Py_ssize_t marg, left; |
| 9639 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9640 | Py_UCS4 fillchar = ' '; |
| 9641 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9642 | 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] | 9643 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9644 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9645 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9646 | return NULL; |
| 9647 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9648 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9649 | Py_INCREF(self); |
| 9650 | return (PyObject*) self; |
| 9651 | } |
| 9652 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9653 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9654 | left = marg / 2 + (marg & width & 1); |
| 9655 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9656 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9657 | } |
| 9658 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9659 | #if 0 |
| 9660 | |
| 9661 | /* This code should go into some future Unicode collation support |
| 9662 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9663 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9664 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9665 | /* speedy UTF-16 code point order comparison */ |
| 9666 | /* gleaned from: */ |
| 9667 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 9668 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9669 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9670 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9671 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9672 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 9673 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9674 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9675 | }; |
| 9676 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9677 | static int |
| 9678 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9679 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9680 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9681 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9682 | Py_UNICODE *s1 = str1->str; |
| 9683 | Py_UNICODE *s2 = str2->str; |
| 9684 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9685 | len1 = str1->_base._base.length; |
| 9686 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9688 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9689 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9690 | |
| 9691 | c1 = *s1++; |
| 9692 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9693 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9694 | if (c1 > (1<<11) * 26) |
| 9695 | c1 += utf16Fixup[c1>>11]; |
| 9696 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9697 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9698 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9699 | |
| 9700 | if (c1 != c2) |
| 9701 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9702 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9703 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9704 | } |
| 9705 | |
| 9706 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9707 | } |
| 9708 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9709 | #else |
| 9710 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9711 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 9712 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9713 | static int |
| 9714 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9715 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9716 | int kind1, kind2; |
| 9717 | void *data1, *data2; |
| 9718 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9719 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9720 | kind1 = PyUnicode_KIND(str1); |
| 9721 | kind2 = PyUnicode_KIND(str2); |
| 9722 | data1 = PyUnicode_DATA(str1); |
| 9723 | data2 = PyUnicode_DATA(str2); |
| 9724 | len1 = PyUnicode_GET_LENGTH(str1); |
| 9725 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9726 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9727 | for (i = 0; i < len1 && i < len2; ++i) { |
| 9728 | Py_UCS4 c1, c2; |
| 9729 | c1 = PyUnicode_READ(kind1, data1, i); |
| 9730 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9731 | |
| 9732 | if (c1 != c2) |
| 9733 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9734 | } |
| 9735 | |
| 9736 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9737 | } |
| 9738 | |
| 9739 | #endif |
| 9740 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9741 | int |
| 9742 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9743 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9744 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9745 | if (PyUnicode_READY(left) == -1 || |
| 9746 | PyUnicode_READY(right) == -1) |
| 9747 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9748 | return unicode_compare((PyUnicodeObject *)left, |
| 9749 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9750 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9751 | PyErr_Format(PyExc_TypeError, |
| 9752 | "Can't compare %.100s and %.100s", |
| 9753 | left->ob_type->tp_name, |
| 9754 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9755 | return -1; |
| 9756 | } |
| 9757 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9758 | int |
| 9759 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 9760 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9761 | Py_ssize_t i; |
| 9762 | int kind; |
| 9763 | void *data; |
| 9764 | Py_UCS4 chr; |
| 9765 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 9766 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9767 | if (PyUnicode_READY(uni) == -1) |
| 9768 | return -1; |
| 9769 | kind = PyUnicode_KIND(uni); |
| 9770 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9771 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9772 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 9773 | if (chr != str[i]) |
| 9774 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 9775 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 9776 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9777 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9778 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9779 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9780 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9781 | return 0; |
| 9782 | } |
| 9783 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9784 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9785 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9786 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9787 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9788 | PyObject * |
| 9789 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9790 | { |
| 9791 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9792 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9793 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9794 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9795 | if (PyUnicode_READY(left) == -1 || |
| 9796 | PyUnicode_READY(right) == -1) |
| 9797 | return NULL; |
| 9798 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 9799 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9800 | if (op == Py_EQ) { |
| 9801 | Py_INCREF(Py_False); |
| 9802 | return Py_False; |
| 9803 | } |
| 9804 | if (op == Py_NE) { |
| 9805 | Py_INCREF(Py_True); |
| 9806 | return Py_True; |
| 9807 | } |
| 9808 | } |
| 9809 | if (left == right) |
| 9810 | result = 0; |
| 9811 | else |
| 9812 | result = unicode_compare((PyUnicodeObject *)left, |
| 9813 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9814 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9815 | /* Convert the return value to a Boolean */ |
| 9816 | switch (op) { |
| 9817 | case Py_EQ: |
| 9818 | v = TEST_COND(result == 0); |
| 9819 | break; |
| 9820 | case Py_NE: |
| 9821 | v = TEST_COND(result != 0); |
| 9822 | break; |
| 9823 | case Py_LE: |
| 9824 | v = TEST_COND(result <= 0); |
| 9825 | break; |
| 9826 | case Py_GE: |
| 9827 | v = TEST_COND(result >= 0); |
| 9828 | break; |
| 9829 | case Py_LT: |
| 9830 | v = TEST_COND(result == -1); |
| 9831 | break; |
| 9832 | case Py_GT: |
| 9833 | v = TEST_COND(result == 1); |
| 9834 | break; |
| 9835 | default: |
| 9836 | PyErr_BadArgument(); |
| 9837 | return NULL; |
| 9838 | } |
| 9839 | Py_INCREF(v); |
| 9840 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9841 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9842 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 9843 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9844 | } |
| 9845 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9846 | int |
| 9847 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9848 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9849 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9850 | int kind1, kind2, kind; |
| 9851 | void *buf1, *buf2; |
| 9852 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9853 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9854 | |
| 9855 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9856 | sub = PyUnicode_FromObject(element); |
| 9857 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9858 | PyErr_Format(PyExc_TypeError, |
| 9859 | "'in <string>' requires string as left operand, not %s", |
| 9860 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9861 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9862 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9863 | if (PyUnicode_READY(sub) == -1) |
| 9864 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9865 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9866 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9867 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9868 | Py_DECREF(sub); |
| 9869 | return -1; |
| 9870 | } |
| 9871 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9872 | kind1 = PyUnicode_KIND(str); |
| 9873 | kind2 = PyUnicode_KIND(sub); |
| 9874 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9875 | buf1 = PyUnicode_DATA(str); |
| 9876 | buf2 = PyUnicode_DATA(sub); |
| 9877 | if (kind1 != kind) |
| 9878 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 9879 | if (!buf1) { |
| 9880 | Py_DECREF(sub); |
| 9881 | return -1; |
| 9882 | } |
| 9883 | if (kind2 != kind) |
| 9884 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 9885 | if (!buf2) { |
| 9886 | Py_DECREF(sub); |
| 9887 | if (kind1 != kind) PyMem_Free(buf1); |
| 9888 | return -1; |
| 9889 | } |
| 9890 | len1 = PyUnicode_GET_LENGTH(str); |
| 9891 | len2 = PyUnicode_GET_LENGTH(sub); |
| 9892 | |
| 9893 | switch(kind) { |
| 9894 | case PyUnicode_1BYTE_KIND: |
| 9895 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9896 | break; |
| 9897 | case PyUnicode_2BYTE_KIND: |
| 9898 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9899 | break; |
| 9900 | case PyUnicode_4BYTE_KIND: |
| 9901 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9902 | break; |
| 9903 | default: |
| 9904 | result = -1; |
| 9905 | assert(0); |
| 9906 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9907 | |
| 9908 | Py_DECREF(str); |
| 9909 | Py_DECREF(sub); |
| 9910 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9911 | if (kind1 != kind) |
| 9912 | PyMem_Free(buf1); |
| 9913 | if (kind2 != kind) |
| 9914 | PyMem_Free(buf2); |
| 9915 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9916 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9917 | } |
| 9918 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9919 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 9920 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9921 | PyObject * |
| 9922 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9923 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9924 | PyObject *u = NULL, *v = NULL, *w; |
| 9925 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9926 | |
| 9927 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9928 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9929 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9930 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9931 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9932 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9933 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9934 | |
| 9935 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9936 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9937 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9938 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9939 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9940 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9941 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9942 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9943 | } |
| 9944 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9945 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 9946 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9947 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9948 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9949 | w = PyUnicode_New( |
| 9950 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 9951 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9952 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9953 | goto onError; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9954 | if (PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)) < 0) |
| 9955 | goto onError; |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9956 | if (PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9957 | v, 0, |
| 9958 | PyUnicode_GET_LENGTH(v)) < 0) |
| 9959 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9960 | Py_DECREF(u); |
| 9961 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9962 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9963 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9964 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9965 | Py_XDECREF(u); |
| 9966 | Py_XDECREF(v); |
| 9967 | return NULL; |
| 9968 | } |
| 9969 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame^] | 9970 | static void |
| 9971 | unicode_append_inplace(PyObject **p_left, PyObject *right) |
| 9972 | { |
| 9973 | Py_ssize_t left_len, right_len, new_len; |
| 9974 | #ifdef Py_DEBUG |
| 9975 | Py_ssize_t copied; |
| 9976 | #endif |
| 9977 | |
| 9978 | assert(PyUnicode_IS_READY(*p_left)); |
| 9979 | assert(PyUnicode_IS_READY(right)); |
| 9980 | |
| 9981 | left_len = PyUnicode_GET_LENGTH(*p_left); |
| 9982 | right_len = PyUnicode_GET_LENGTH(right); |
| 9983 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
| 9984 | PyErr_SetString(PyExc_OverflowError, |
| 9985 | "strings are too large to concat"); |
| 9986 | goto error; |
| 9987 | } |
| 9988 | new_len = left_len + right_len; |
| 9989 | |
| 9990 | /* Now we own the last reference to 'left', so we can resize it |
| 9991 | * in-place. |
| 9992 | */ |
| 9993 | if (unicode_resize(p_left, new_len) != 0) { |
| 9994 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 9995 | * deallocated so it cannot be put back into |
| 9996 | * 'variable'. The MemoryError is raised when there |
| 9997 | * is no value in 'variable', which might (very |
| 9998 | * remotely) be a cause of incompatibilities. |
| 9999 | */ |
| 10000 | goto error; |
| 10001 | } |
| 10002 | /* copy 'right' into the newly allocated area of 'left' */ |
| 10003 | #ifdef Py_DEBUG |
| 10004 | copied = PyUnicode_CopyCharacters(*p_left, left_len, |
| 10005 | right, 0, |
| 10006 | right_len); |
| 10007 | assert(0 <= copied); |
| 10008 | #else |
| 10009 | PyUnicode_CopyCharacters(*p_left, left_len, right, 0, right_len); |
| 10010 | #endif |
| 10011 | return; |
| 10012 | |
| 10013 | error: |
| 10014 | Py_DECREF(*p_left); |
| 10015 | *p_left = NULL; |
| 10016 | } |
| 10017 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10018 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10019 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10020 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10021 | PyObject *left, *res; |
| 10022 | |
| 10023 | if (p_left == NULL) { |
| 10024 | if (!PyErr_Occurred()) |
| 10025 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10026 | return; |
| 10027 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10028 | left = *p_left; |
| 10029 | if (right == NULL || !PyUnicode_Check(left)) { |
| 10030 | if (!PyErr_Occurred()) |
| 10031 | PyErr_BadInternalCall(); |
| 10032 | goto error; |
| 10033 | } |
| 10034 | |
| 10035 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 10036 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 10037 | && unicode_resizable(left) |
| 10038 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 10039 | || _PyUnicode_WSTR(left) != NULL)) |
| 10040 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10041 | if (PyUnicode_READY(left)) |
| 10042 | goto error; |
| 10043 | if (PyUnicode_READY(right)) |
| 10044 | goto error; |
| 10045 | |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame^] | 10046 | /* Don't resize for ascii += latin1. Convert ascii to latin1 requires |
| 10047 | to change the structure size, but characters are stored just after |
| 10048 | the structure, and so it requires to move all charactres which is |
| 10049 | not so different than duplicating the string. */ |
| 10050 | if (!(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right))) |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10051 | { |
Victor Stinner | b092365 | 2011-10-04 01:17:31 +0200 | [diff] [blame^] | 10052 | unicode_append_inplace(p_left, right); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10053 | return; |
| 10054 | } |
| 10055 | } |
| 10056 | |
| 10057 | res = PyUnicode_Concat(left, right); |
| 10058 | if (res == NULL) |
| 10059 | goto error; |
| 10060 | Py_DECREF(left); |
| 10061 | *p_left = res; |
| 10062 | return; |
| 10063 | |
| 10064 | error: |
| 10065 | Py_DECREF(*p_left); |
| 10066 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10067 | } |
| 10068 | |
| 10069 | void |
| 10070 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10071 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10072 | PyUnicode_Append(pleft, right); |
| 10073 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10074 | } |
| 10075 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10076 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10077 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10078 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10079 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10080 | 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] | 10081 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10082 | |
| 10083 | static PyObject * |
| 10084 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 10085 | { |
| 10086 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10087 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10088 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10089 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10090 | int kind1, kind2, kind; |
| 10091 | void *buf1, *buf2; |
| 10092 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10093 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10094 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10095 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10096 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10097 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10098 | kind1 = PyUnicode_KIND(self); |
| 10099 | kind2 = PyUnicode_KIND(substring); |
| 10100 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10101 | buf1 = PyUnicode_DATA(self); |
| 10102 | buf2 = PyUnicode_DATA(substring); |
| 10103 | if (kind1 != kind) |
| 10104 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10105 | if (!buf1) { |
| 10106 | Py_DECREF(substring); |
| 10107 | return NULL; |
| 10108 | } |
| 10109 | if (kind2 != kind) |
| 10110 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10111 | if (!buf2) { |
| 10112 | Py_DECREF(substring); |
| 10113 | if (kind1 != kind) PyMem_Free(buf1); |
| 10114 | return NULL; |
| 10115 | } |
| 10116 | len1 = PyUnicode_GET_LENGTH(self); |
| 10117 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10118 | |
| 10119 | ADJUST_INDICES(start, end, len1); |
| 10120 | switch(kind) { |
| 10121 | case PyUnicode_1BYTE_KIND: |
| 10122 | iresult = ucs1lib_count( |
| 10123 | ((Py_UCS1*)buf1) + start, end - start, |
| 10124 | buf2, len2, PY_SSIZE_T_MAX |
| 10125 | ); |
| 10126 | break; |
| 10127 | case PyUnicode_2BYTE_KIND: |
| 10128 | iresult = ucs2lib_count( |
| 10129 | ((Py_UCS2*)buf1) + start, end - start, |
| 10130 | buf2, len2, PY_SSIZE_T_MAX |
| 10131 | ); |
| 10132 | break; |
| 10133 | case PyUnicode_4BYTE_KIND: |
| 10134 | iresult = ucs4lib_count( |
| 10135 | ((Py_UCS4*)buf1) + start, end - start, |
| 10136 | buf2, len2, PY_SSIZE_T_MAX |
| 10137 | ); |
| 10138 | break; |
| 10139 | default: |
| 10140 | assert(0); iresult = 0; |
| 10141 | } |
| 10142 | |
| 10143 | result = PyLong_FromSsize_t(iresult); |
| 10144 | |
| 10145 | if (kind1 != kind) |
| 10146 | PyMem_Free(buf1); |
| 10147 | if (kind2 != kind) |
| 10148 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10149 | |
| 10150 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10151 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10152 | return result; |
| 10153 | } |
| 10154 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10155 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10156 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10157 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10158 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10159 | 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] | 10160 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10161 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10162 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10163 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10164 | |
| 10165 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10166 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10167 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10168 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10169 | char *encoding = NULL; |
| 10170 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10171 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10172 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10173 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10174 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 10175 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10176 | } |
| 10177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10178 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10179 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10180 | \n\ |
| 10181 | 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] | 10182 | 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] | 10183 | |
| 10184 | static PyObject* |
| 10185 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 10186 | { |
| 10187 | Py_UNICODE *e; |
| 10188 | Py_UNICODE *p; |
| 10189 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10190 | Py_UNICODE *qe; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10191 | Py_ssize_t i, j, incr, wstr_length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10192 | PyUnicodeObject *u; |
| 10193 | int tabsize = 8; |
| 10194 | |
| 10195 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10196 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10197 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10198 | if (PyUnicode_AsUnicodeAndSize((PyObject *)self, &wstr_length) == NULL) |
| 10199 | return NULL; |
| 10200 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10201 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10202 | i = 0; /* chars up to and including most recent \n or \r */ |
| 10203 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10204 | e = _PyUnicode_WSTR(self) + wstr_length; /* end of input */ |
| 10205 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10206 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10207 | if (tabsize > 0) { |
| 10208 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 10209 | if (j > PY_SSIZE_T_MAX - incr) |
| 10210 | goto overflow1; |
| 10211 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10212 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10213 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10214 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10215 | if (j > PY_SSIZE_T_MAX - 1) |
| 10216 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10217 | j++; |
| 10218 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10219 | if (i > PY_SSIZE_T_MAX - j) |
| 10220 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10221 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10222 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10223 | } |
| 10224 | } |
| 10225 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10226 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10227 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10228 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10229 | /* Second pass: create output string and fill it */ |
| 10230 | u = _PyUnicode_New(i + j); |
| 10231 | if (!u) |
| 10232 | return NULL; |
| 10233 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10234 | j = 0; /* same as in first pass */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10235 | q = _PyUnicode_WSTR(u); /* next output char */ |
| 10236 | qe = _PyUnicode_WSTR(u) + PyUnicode_GET_SIZE(u); /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10237 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10238 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10239 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10240 | if (tabsize > 0) { |
| 10241 | i = tabsize - (j % tabsize); |
| 10242 | j += i; |
| 10243 | while (i--) { |
| 10244 | if (q >= qe) |
| 10245 | goto overflow2; |
| 10246 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10247 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10248 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10249 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10250 | else { |
| 10251 | if (q >= qe) |
| 10252 | goto overflow2; |
| 10253 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10254 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10255 | if (*p == '\n' || *p == '\r') |
| 10256 | j = 0; |
| 10257 | } |
| 10258 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 10259 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10260 | Py_DECREF(u); |
| 10261 | return NULL; |
| 10262 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10263 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10264 | |
| 10265 | overflow2: |
| 10266 | Py_DECREF(u); |
| 10267 | overflow1: |
| 10268 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10269 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10270 | } |
| 10271 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10272 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10273 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10274 | \n\ |
| 10275 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10276 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10277 | arguments start and end are interpreted as in slice notation.\n\ |
| 10278 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10279 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10280 | |
| 10281 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10282 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10283 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10284 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10285 | Py_ssize_t start; |
| 10286 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10287 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10288 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10289 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10290 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10291 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10292 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10293 | if (PyUnicode_READY(self) == -1) |
| 10294 | return NULL; |
| 10295 | if (PyUnicode_READY(substring) == -1) |
| 10296 | return NULL; |
| 10297 | |
| 10298 | result = any_find_slice( |
| 10299 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 10300 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10301 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10302 | |
| 10303 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10304 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10305 | if (result == -2) |
| 10306 | return NULL; |
| 10307 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10308 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10309 | } |
| 10310 | |
| 10311 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10312 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10313 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10314 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 10315 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10316 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10317 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10318 | } |
| 10319 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10320 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10321 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10322 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 10323 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10324 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10325 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10326 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10327 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10328 | if (_PyUnicode_HASH(self) != -1) |
| 10329 | return _PyUnicode_HASH(self); |
| 10330 | if (PyUnicode_READY(self) == -1) |
| 10331 | return -1; |
| 10332 | len = PyUnicode_GET_LENGTH(self); |
| 10333 | |
| 10334 | /* The hash function as a macro, gets expanded three times below. */ |
| 10335 | #define HASH(P) \ |
| 10336 | x = (Py_uhash_t)*P << 7; \ |
| 10337 | while (--len >= 0) \ |
| 10338 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 10339 | |
| 10340 | switch (PyUnicode_KIND(self)) { |
| 10341 | case PyUnicode_1BYTE_KIND: { |
| 10342 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10343 | HASH(c); |
| 10344 | break; |
| 10345 | } |
| 10346 | case PyUnicode_2BYTE_KIND: { |
| 10347 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10348 | HASH(s); |
| 10349 | break; |
| 10350 | } |
| 10351 | default: { |
| 10352 | Py_UCS4 *l; |
| 10353 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10354 | "Impossible switch case in unicode_hash"); |
| 10355 | l = PyUnicode_4BYTE_DATA(self); |
| 10356 | HASH(l); |
| 10357 | break; |
| 10358 | } |
| 10359 | } |
| 10360 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 10361 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10362 | if (x == -1) |
| 10363 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10364 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10365 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10366 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10367 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10368 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10369 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10370 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10371 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10372 | 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] | 10373 | |
| 10374 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10375 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10376 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10377 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10378 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10379 | Py_ssize_t start; |
| 10380 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10381 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10382 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10383 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10384 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10385 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10386 | if (PyUnicode_READY(self) == -1) |
| 10387 | return NULL; |
| 10388 | if (PyUnicode_READY(substring) == -1) |
| 10389 | return NULL; |
| 10390 | |
| 10391 | result = any_find_slice( |
| 10392 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 10393 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10394 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10395 | |
| 10396 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10397 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10398 | if (result == -2) |
| 10399 | return NULL; |
| 10400 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10401 | if (result < 0) { |
| 10402 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10403 | return NULL; |
| 10404 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10405 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10406 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10407 | } |
| 10408 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10409 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10410 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10411 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10412 | 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] | 10413 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10414 | |
| 10415 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10416 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10417 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10418 | Py_ssize_t i, length; |
| 10419 | int kind; |
| 10420 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10421 | int cased; |
| 10422 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10423 | if (PyUnicode_READY(self) == -1) |
| 10424 | return NULL; |
| 10425 | length = PyUnicode_GET_LENGTH(self); |
| 10426 | kind = PyUnicode_KIND(self); |
| 10427 | data = PyUnicode_DATA(self); |
| 10428 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10429 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10430 | if (length == 1) |
| 10431 | return PyBool_FromLong( |
| 10432 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10433 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10434 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10435 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10436 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10437 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10438 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10439 | for (i = 0; i < length; i++) { |
| 10440 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10441 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10442 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10443 | return PyBool_FromLong(0); |
| 10444 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 10445 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10446 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10447 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10448 | } |
| 10449 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10450 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10451 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10452 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10453 | 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] | 10454 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10455 | |
| 10456 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10457 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10458 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10459 | Py_ssize_t i, length; |
| 10460 | int kind; |
| 10461 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10462 | int cased; |
| 10463 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10464 | if (PyUnicode_READY(self) == -1) |
| 10465 | return NULL; |
| 10466 | length = PyUnicode_GET_LENGTH(self); |
| 10467 | kind = PyUnicode_KIND(self); |
| 10468 | data = PyUnicode_DATA(self); |
| 10469 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10470 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10471 | if (length == 1) |
| 10472 | return PyBool_FromLong( |
| 10473 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10474 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10475 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10476 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10477 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10478 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10479 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10480 | for (i = 0; i < length; i++) { |
| 10481 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10482 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10483 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10484 | return PyBool_FromLong(0); |
| 10485 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 10486 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10487 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10488 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10489 | } |
| 10490 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10491 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10492 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10493 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10494 | Return True if S is a titlecased string and there is at least one\n\ |
| 10495 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 10496 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 10497 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10498 | |
| 10499 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10500 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10501 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10502 | Py_ssize_t i, length; |
| 10503 | int kind; |
| 10504 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10505 | int cased, previous_is_cased; |
| 10506 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10507 | if (PyUnicode_READY(self) == -1) |
| 10508 | return NULL; |
| 10509 | length = PyUnicode_GET_LENGTH(self); |
| 10510 | kind = PyUnicode_KIND(self); |
| 10511 | data = PyUnicode_DATA(self); |
| 10512 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10513 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10514 | if (length == 1) { |
| 10515 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10516 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10517 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10518 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10519 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10520 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10521 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10522 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10523 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10524 | cased = 0; |
| 10525 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10526 | for (i = 0; i < length; i++) { |
| 10527 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10528 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10529 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10530 | if (previous_is_cased) |
| 10531 | return PyBool_FromLong(0); |
| 10532 | previous_is_cased = 1; |
| 10533 | cased = 1; |
| 10534 | } |
| 10535 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10536 | if (!previous_is_cased) |
| 10537 | return PyBool_FromLong(0); |
| 10538 | previous_is_cased = 1; |
| 10539 | cased = 1; |
| 10540 | } |
| 10541 | else |
| 10542 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10543 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10544 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10545 | } |
| 10546 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10547 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10548 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10549 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10550 | Return True if all characters in S are whitespace\n\ |
| 10551 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10552 | |
| 10553 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10554 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10555 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10556 | Py_ssize_t i, length; |
| 10557 | int kind; |
| 10558 | void *data; |
| 10559 | |
| 10560 | if (PyUnicode_READY(self) == -1) |
| 10561 | return NULL; |
| 10562 | length = PyUnicode_GET_LENGTH(self); |
| 10563 | kind = PyUnicode_KIND(self); |
| 10564 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10565 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10566 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10567 | if (length == 1) |
| 10568 | return PyBool_FromLong( |
| 10569 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10570 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10571 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10572 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10573 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10574 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10575 | for (i = 0; i < length; i++) { |
| 10576 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10577 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10578 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10579 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10580 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10581 | } |
| 10582 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10583 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10584 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10585 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10586 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10587 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10588 | |
| 10589 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10590 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10591 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10592 | Py_ssize_t i, length; |
| 10593 | int kind; |
| 10594 | void *data; |
| 10595 | |
| 10596 | if (PyUnicode_READY(self) == -1) |
| 10597 | return NULL; |
| 10598 | length = PyUnicode_GET_LENGTH(self); |
| 10599 | kind = PyUnicode_KIND(self); |
| 10600 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10601 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10602 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10603 | if (length == 1) |
| 10604 | return PyBool_FromLong( |
| 10605 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10606 | |
| 10607 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10608 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10609 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10610 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10611 | for (i = 0; i < length; i++) { |
| 10612 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10613 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10614 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10615 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10616 | } |
| 10617 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10618 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10619 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10620 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10621 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10622 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10623 | |
| 10624 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10625 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10626 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10627 | int kind; |
| 10628 | void *data; |
| 10629 | Py_ssize_t len, i; |
| 10630 | |
| 10631 | if (PyUnicode_READY(self) == -1) |
| 10632 | return NULL; |
| 10633 | |
| 10634 | kind = PyUnicode_KIND(self); |
| 10635 | data = PyUnicode_DATA(self); |
| 10636 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10637 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10638 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10639 | if (len == 1) { |
| 10640 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10641 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10642 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10643 | |
| 10644 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10645 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10646 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10647 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10648 | for (i = 0; i < len; i++) { |
| 10649 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10650 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10651 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10652 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10653 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10654 | } |
| 10655 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10656 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10657 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10658 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10659 | 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] | 10660 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10661 | |
| 10662 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10663 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10664 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10665 | Py_ssize_t i, length; |
| 10666 | int kind; |
| 10667 | void *data; |
| 10668 | |
| 10669 | if (PyUnicode_READY(self) == -1) |
| 10670 | return NULL; |
| 10671 | length = PyUnicode_GET_LENGTH(self); |
| 10672 | kind = PyUnicode_KIND(self); |
| 10673 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10674 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10675 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10676 | if (length == 1) |
| 10677 | return PyBool_FromLong( |
| 10678 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10679 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10680 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10681 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10682 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10683 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10684 | for (i = 0; i < length; i++) { |
| 10685 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10686 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10687 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10688 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10689 | } |
| 10690 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10691 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10692 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10693 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10694 | Return True if all characters in S are digits\n\ |
| 10695 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10696 | |
| 10697 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10698 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10699 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10700 | Py_ssize_t i, length; |
| 10701 | int kind; |
| 10702 | void *data; |
| 10703 | |
| 10704 | if (PyUnicode_READY(self) == -1) |
| 10705 | return NULL; |
| 10706 | length = PyUnicode_GET_LENGTH(self); |
| 10707 | kind = PyUnicode_KIND(self); |
| 10708 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10709 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10710 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10711 | if (length == 1) { |
| 10712 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10713 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 10714 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10715 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10716 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10717 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10718 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10719 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10720 | for (i = 0; i < length; i++) { |
| 10721 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10722 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10723 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10724 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10725 | } |
| 10726 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10727 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10728 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10729 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10730 | 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] | 10731 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10732 | |
| 10733 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10734 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10735 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10736 | Py_ssize_t i, length; |
| 10737 | int kind; |
| 10738 | void *data; |
| 10739 | |
| 10740 | if (PyUnicode_READY(self) == -1) |
| 10741 | return NULL; |
| 10742 | length = PyUnicode_GET_LENGTH(self); |
| 10743 | kind = PyUnicode_KIND(self); |
| 10744 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10745 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10746 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10747 | if (length == 1) |
| 10748 | return PyBool_FromLong( |
| 10749 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10750 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10751 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10752 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10753 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10754 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10755 | for (i = 0; i < length; i++) { |
| 10756 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10757 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10758 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10759 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10760 | } |
| 10761 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10762 | int |
| 10763 | PyUnicode_IsIdentifier(PyObject *self) |
| 10764 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10765 | int kind; |
| 10766 | void *data; |
| 10767 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10768 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10769 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10770 | if (PyUnicode_READY(self) == -1) { |
| 10771 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10772 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10773 | } |
| 10774 | |
| 10775 | /* Special case for empty strings */ |
| 10776 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10777 | return 0; |
| 10778 | kind = PyUnicode_KIND(self); |
| 10779 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10780 | |
| 10781 | /* PEP 3131 says that the first character must be in |
| 10782 | XID_Start and subsequent characters in XID_Continue, |
| 10783 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10784 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10785 | letters, digits, underscore). However, given the current |
| 10786 | definition of XID_Start and XID_Continue, it is sufficient |
| 10787 | to check just for these, except that _ must be allowed |
| 10788 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10789 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 10790 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10791 | return 0; |
| 10792 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 10793 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10794 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10795 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10796 | return 1; |
| 10797 | } |
| 10798 | |
| 10799 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10800 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10801 | \n\ |
| 10802 | Return True if S is a valid identifier according\n\ |
| 10803 | to the language definition."); |
| 10804 | |
| 10805 | static PyObject* |
| 10806 | unicode_isidentifier(PyObject *self) |
| 10807 | { |
| 10808 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 10809 | } |
| 10810 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10811 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10812 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10813 | \n\ |
| 10814 | Return True if all characters in S are considered\n\ |
| 10815 | printable in repr() or S is empty, False otherwise."); |
| 10816 | |
| 10817 | static PyObject* |
| 10818 | unicode_isprintable(PyObject *self) |
| 10819 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10820 | Py_ssize_t i, length; |
| 10821 | int kind; |
| 10822 | void *data; |
| 10823 | |
| 10824 | if (PyUnicode_READY(self) == -1) |
| 10825 | return NULL; |
| 10826 | length = PyUnicode_GET_LENGTH(self); |
| 10827 | kind = PyUnicode_KIND(self); |
| 10828 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10829 | |
| 10830 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10831 | if (length == 1) |
| 10832 | return PyBool_FromLong( |
| 10833 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10834 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10835 | for (i = 0; i < length; i++) { |
| 10836 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10837 | Py_RETURN_FALSE; |
| 10838 | } |
| 10839 | } |
| 10840 | Py_RETURN_TRUE; |
| 10841 | } |
| 10842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10843 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 10844 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10845 | \n\ |
| 10846 | 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] | 10847 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10848 | |
| 10849 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10850 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10851 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10852 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10853 | } |
| 10854 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10855 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10856 | unicode_length(PyUnicodeObject *self) |
| 10857 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10858 | if (PyUnicode_READY(self) == -1) |
| 10859 | return -1; |
| 10860 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10861 | } |
| 10862 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10863 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10864 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10865 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10866 | 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] | 10867 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10868 | |
| 10869 | static PyObject * |
| 10870 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 10871 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10872 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10873 | Py_UCS4 fillchar = ' '; |
| 10874 | |
| 10875 | if (PyUnicode_READY(self) == -1) |
| 10876 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10877 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10878 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10879 | return NULL; |
| 10880 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10881 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10882 | Py_INCREF(self); |
| 10883 | return (PyObject*) self; |
| 10884 | } |
| 10885 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10886 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10887 | } |
| 10888 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10889 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10890 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10891 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10892 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10893 | |
| 10894 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10895 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10896 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10897 | return fixup(self, fixlower); |
| 10898 | } |
| 10899 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10900 | #define LEFTSTRIP 0 |
| 10901 | #define RIGHTSTRIP 1 |
| 10902 | #define BOTHSTRIP 2 |
| 10903 | |
| 10904 | /* Arrays indexed by above */ |
| 10905 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 10906 | |
| 10907 | #define STRIPNAME(i) (stripformat[i]+3) |
| 10908 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10909 | /* externally visible for str.strip(unicode) */ |
| 10910 | PyObject * |
| 10911 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 10912 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10913 | void *data; |
| 10914 | int kind; |
| 10915 | Py_ssize_t i, j, len; |
| 10916 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10917 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10918 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 10919 | return NULL; |
| 10920 | |
| 10921 | kind = PyUnicode_KIND(self); |
| 10922 | data = PyUnicode_DATA(self); |
| 10923 | len = PyUnicode_GET_LENGTH(self); |
| 10924 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 10925 | PyUnicode_DATA(sepobj), |
| 10926 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10927 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10928 | i = 0; |
| 10929 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10930 | while (i < len && |
| 10931 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10932 | i++; |
| 10933 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10934 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10935 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10936 | j = len; |
| 10937 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10938 | do { |
| 10939 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10940 | } while (j >= i && |
| 10941 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10942 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10943 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10944 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10945 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10946 | } |
| 10947 | |
| 10948 | PyObject* |
| 10949 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 10950 | { |
| 10951 | unsigned char *data; |
| 10952 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10953 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10954 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10955 | if (PyUnicode_READY(self) == -1) |
| 10956 | return NULL; |
| 10957 | |
| 10958 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 10959 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10960 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10961 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10962 | if (PyUnicode_CheckExact(self)) { |
| 10963 | Py_INCREF(self); |
| 10964 | return self; |
| 10965 | } |
| 10966 | else |
| 10967 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10968 | } |
| 10969 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10970 | length = end - start; |
| 10971 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10972 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10973 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10974 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10975 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10976 | return NULL; |
| 10977 | } |
| 10978 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10979 | kind = PyUnicode_KIND(self); |
| 10980 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10981 | return PyUnicode_FromKindAndData(kind, |
| 10982 | data + PyUnicode_KIND_SIZE(kind, start), |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10983 | length); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10984 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10985 | |
| 10986 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10987 | do_strip(PyUnicodeObject *self, int striptype) |
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 | int kind; |
| 10990 | void *data; |
| 10991 | Py_ssize_t len, i, j; |
| 10992 | |
| 10993 | if (PyUnicode_READY(self) == -1) |
| 10994 | return NULL; |
| 10995 | |
| 10996 | kind = PyUnicode_KIND(self); |
| 10997 | data = PyUnicode_DATA(self); |
| 10998 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10999 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11000 | i = 0; |
| 11001 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11002 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11003 | i++; |
| 11004 | } |
| 11005 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11006 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11007 | j = len; |
| 11008 | if (striptype != LEFTSTRIP) { |
| 11009 | do { |
| 11010 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11011 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11012 | j++; |
| 11013 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11014 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 11015 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11016 | } |
| 11017 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11018 | |
| 11019 | static PyObject * |
| 11020 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 11021 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11022 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11023 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11024 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11025 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11026 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11027 | if (sep != NULL && sep != Py_None) { |
| 11028 | if (PyUnicode_Check(sep)) |
| 11029 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11030 | else { |
| 11031 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11032 | "%s arg must be None or str", |
| 11033 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11034 | return NULL; |
| 11035 | } |
| 11036 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11037 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11038 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11039 | } |
| 11040 | |
| 11041 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11042 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11043 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11044 | \n\ |
| 11045 | Return a copy of the string S with leading and trailing\n\ |
| 11046 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11047 | 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] | 11048 | |
| 11049 | static PyObject * |
| 11050 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 11051 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11052 | if (PyTuple_GET_SIZE(args) == 0) |
| 11053 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11054 | else |
| 11055 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11056 | } |
| 11057 | |
| 11058 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11059 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11060 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11061 | \n\ |
| 11062 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11063 | 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] | 11064 | |
| 11065 | static PyObject * |
| 11066 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 11067 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11068 | if (PyTuple_GET_SIZE(args) == 0) |
| 11069 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11070 | else |
| 11071 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11072 | } |
| 11073 | |
| 11074 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11075 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11076 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11077 | \n\ |
| 11078 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11079 | 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] | 11080 | |
| 11081 | static PyObject * |
| 11082 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 11083 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11084 | if (PyTuple_GET_SIZE(args) == 0) |
| 11085 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11086 | else |
| 11087 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11088 | } |
| 11089 | |
| 11090 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11091 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11092 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11093 | { |
| 11094 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11095 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11096 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11097 | if (len < 1) { |
| 11098 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11099 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11100 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11101 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11102 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11103 | /* no repeat, return original string */ |
| 11104 | Py_INCREF(str); |
| 11105 | return (PyObject*) str; |
| 11106 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11107 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11108 | if (PyUnicode_READY(str) == -1) |
| 11109 | return NULL; |
| 11110 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11111 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11112 | PyErr_SetString(PyExc_OverflowError, |
| 11113 | "repeated string is too long"); |
| 11114 | return NULL; |
| 11115 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11116 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11117 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11118 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11119 | if (!u) |
| 11120 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11121 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11122 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11123 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11124 | const int kind = PyUnicode_KIND(str); |
| 11125 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11126 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11127 | if (kind == PyUnicode_1BYTE_KIND) |
| 11128 | memset(to, (unsigned char)fill_char, len); |
| 11129 | else { |
| 11130 | for (n = 0; n < len; ++n) |
| 11131 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11132 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11133 | } |
| 11134 | else { |
| 11135 | /* number of characters copied this far */ |
| 11136 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
| 11137 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); |
| 11138 | char *to = (char *) PyUnicode_DATA(u); |
| 11139 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11140 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11141 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11142 | n = (done <= nchars-done) ? done : nchars-done; |
| 11143 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11144 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11145 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11146 | } |
| 11147 | |
| 11148 | return (PyObject*) u; |
| 11149 | } |
| 11150 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11151 | PyObject * |
| 11152 | PyUnicode_Replace(PyObject *obj, |
| 11153 | PyObject *subobj, |
| 11154 | PyObject *replobj, |
| 11155 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11156 | { |
| 11157 | PyObject *self; |
| 11158 | PyObject *str1; |
| 11159 | PyObject *str2; |
| 11160 | PyObject *result; |
| 11161 | |
| 11162 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11163 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11164 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11165 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11166 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11167 | Py_DECREF(self); |
| 11168 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11169 | } |
| 11170 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11171 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11172 | Py_DECREF(self); |
| 11173 | Py_DECREF(str1); |
| 11174 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11175 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11176 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11177 | Py_DECREF(self); |
| 11178 | Py_DECREF(str1); |
| 11179 | Py_DECREF(str2); |
| 11180 | return result; |
| 11181 | } |
| 11182 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11183 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11184 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11185 | \n\ |
| 11186 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11187 | old replaced by new. If the optional argument count is\n\ |
| 11188 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11189 | |
| 11190 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11191 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11192 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11193 | PyObject *str1; |
| 11194 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11195 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11196 | PyObject *result; |
| 11197 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11198 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11199 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11200 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11201 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11202 | str1 = PyUnicode_FromObject(str1); |
| 11203 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11204 | return NULL; |
| 11205 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11206 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11207 | Py_DECREF(str1); |
| 11208 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11209 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11210 | |
| 11211 | result = replace(self, str1, str2, maxcount); |
| 11212 | |
| 11213 | Py_DECREF(str1); |
| 11214 | Py_DECREF(str2); |
| 11215 | return result; |
| 11216 | } |
| 11217 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11218 | static PyObject * |
| 11219 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11220 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11221 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11222 | Py_ssize_t isize; |
| 11223 | Py_ssize_t osize, squote, dquote, i, o; |
| 11224 | Py_UCS4 max, quote; |
| 11225 | int ikind, okind; |
| 11226 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11227 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11228 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11229 | return NULL; |
| 11230 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11231 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11232 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11233 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11234 | /* Compute length of output, quote characters, and |
| 11235 | maximum character */ |
| 11236 | osize = 2; /* quotes */ |
| 11237 | max = 127; |
| 11238 | squote = dquote = 0; |
| 11239 | ikind = PyUnicode_KIND(unicode); |
| 11240 | for (i = 0; i < isize; i++) { |
| 11241 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11242 | switch (ch) { |
| 11243 | case '\'': squote++; osize++; break; |
| 11244 | case '"': dquote++; osize++; break; |
| 11245 | case '\\': case '\t': case '\r': case '\n': |
| 11246 | osize += 2; break; |
| 11247 | default: |
| 11248 | /* Fast-path ASCII */ |
| 11249 | if (ch < ' ' || ch == 0x7f) |
| 11250 | osize += 4; /* \xHH */ |
| 11251 | else if (ch < 0x7f) |
| 11252 | osize++; |
| 11253 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11254 | osize++; |
| 11255 | max = ch > max ? ch : max; |
| 11256 | } |
| 11257 | else if (ch < 0x100) |
| 11258 | osize += 4; /* \xHH */ |
| 11259 | else if (ch < 0x10000) |
| 11260 | osize += 6; /* \uHHHH */ |
| 11261 | else |
| 11262 | osize += 10; /* \uHHHHHHHH */ |
| 11263 | } |
| 11264 | } |
| 11265 | |
| 11266 | quote = '\''; |
| 11267 | if (squote) { |
| 11268 | if (dquote) |
| 11269 | /* Both squote and dquote present. Use squote, |
| 11270 | and escape them */ |
| 11271 | osize += squote; |
| 11272 | else |
| 11273 | quote = '"'; |
| 11274 | } |
| 11275 | |
| 11276 | repr = PyUnicode_New(osize, max); |
| 11277 | if (repr == NULL) |
| 11278 | return NULL; |
| 11279 | okind = PyUnicode_KIND(repr); |
| 11280 | odata = PyUnicode_DATA(repr); |
| 11281 | |
| 11282 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11283 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11284 | |
| 11285 | for (i = 0, o = 1; i < isize; i++) { |
| 11286 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11287 | |
| 11288 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11289 | if ((ch == quote) || (ch == '\\')) { |
| 11290 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11291 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11292 | continue; |
| 11293 | } |
| 11294 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11295 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11296 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11297 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11298 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11299 | } |
| 11300 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11301 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11302 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11303 | } |
| 11304 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11305 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11306 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11307 | } |
| 11308 | |
| 11309 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11310 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11311 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11312 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11313 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11314 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11315 | } |
| 11316 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11317 | /* Copy ASCII characters as-is */ |
| 11318 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11319 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11320 | } |
| 11321 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11322 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11323 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11324 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11325 | (categories Z* and C* except ASCII space) |
| 11326 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11327 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11328 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11329 | if (ch <= 0xff) { |
| 11330 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11331 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11332 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11333 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11334 | } |
| 11335 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11336 | else if (ch >= 0x10000) { |
| 11337 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11338 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11339 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 11340 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 11341 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 11342 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 11343 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11344 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11345 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11346 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11347 | } |
| 11348 | /* Map 16-bit characters to '\uxxxx' */ |
| 11349 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11350 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11351 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 11352 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11353 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11354 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11355 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11356 | } |
| 11357 | } |
| 11358 | /* Copy characters as-is */ |
| 11359 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11360 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11361 | } |
| 11362 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11363 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11364 | /* Closing quote already added at the beginning */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11365 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11366 | } |
| 11367 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11368 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11369 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11370 | \n\ |
| 11371 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11372 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11373 | arguments start and end are interpreted as in slice notation.\n\ |
| 11374 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11375 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11376 | |
| 11377 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11378 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11379 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11380 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11381 | Py_ssize_t start; |
| 11382 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11383 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11384 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11385 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11386 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11387 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11388 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11389 | if (PyUnicode_READY(self) == -1) |
| 11390 | return NULL; |
| 11391 | if (PyUnicode_READY(substring) == -1) |
| 11392 | return NULL; |
| 11393 | |
| 11394 | result = any_find_slice( |
| 11395 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 11396 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11397 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11398 | |
| 11399 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11400 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11401 | if (result == -2) |
| 11402 | return NULL; |
| 11403 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11404 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11405 | } |
| 11406 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11407 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11408 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11409 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11410 | 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] | 11411 | |
| 11412 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11413 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11414 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11415 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11416 | Py_ssize_t start; |
| 11417 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11418 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11419 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11420 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 11421 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11422 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11423 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11424 | if (PyUnicode_READY(self) == -1) |
| 11425 | return NULL; |
| 11426 | if (PyUnicode_READY(substring) == -1) |
| 11427 | return NULL; |
| 11428 | |
| 11429 | result = any_find_slice( |
| 11430 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 11431 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11432 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11433 | |
| 11434 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11435 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11436 | if (result == -2) |
| 11437 | return NULL; |
| 11438 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | if (result < 0) { |
| 11440 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11441 | return NULL; |
| 11442 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11443 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11444 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11445 | } |
| 11446 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11447 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11448 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11449 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11450 | 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] | 11451 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | |
| 11453 | static PyObject * |
| 11454 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 11455 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11456 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11457 | Py_UCS4 fillchar = ' '; |
| 11458 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11459 | 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] | 11460 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11461 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11462 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11463 | return NULL; |
| 11464 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11465 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11466 | Py_INCREF(self); |
| 11467 | return (PyObject*) self; |
| 11468 | } |
| 11469 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11470 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11471 | } |
| 11472 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11473 | PyObject * |
| 11474 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11475 | { |
| 11476 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11477 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11478 | s = PyUnicode_FromObject(s); |
| 11479 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11480 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11481 | if (sep != NULL) { |
| 11482 | sep = PyUnicode_FromObject(sep); |
| 11483 | if (sep == NULL) { |
| 11484 | Py_DECREF(s); |
| 11485 | return NULL; |
| 11486 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11487 | } |
| 11488 | |
| 11489 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11490 | |
| 11491 | Py_DECREF(s); |
| 11492 | Py_XDECREF(sep); |
| 11493 | return result; |
| 11494 | } |
| 11495 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11496 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11497 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11498 | \n\ |
| 11499 | Return a list of the words in S, using sep as the\n\ |
| 11500 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11501 | 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] | 11502 | whitespace string is a separator and empty strings are\n\ |
| 11503 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11504 | |
| 11505 | static PyObject* |
| 11506 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 11507 | { |
| 11508 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11509 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11510 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11511 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11512 | return NULL; |
| 11513 | |
| 11514 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11515 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11516 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11517 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11518 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11519 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11520 | } |
| 11521 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11522 | PyObject * |
| 11523 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11524 | { |
| 11525 | PyObject* str_obj; |
| 11526 | PyObject* sep_obj; |
| 11527 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11528 | int kind1, kind2, kind; |
| 11529 | void *buf1 = NULL, *buf2 = NULL; |
| 11530 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11531 | |
| 11532 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11533 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11534 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11535 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11536 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11537 | Py_DECREF(str_obj); |
| 11538 | return NULL; |
| 11539 | } |
| 11540 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11541 | kind1 = PyUnicode_KIND(str_in); |
| 11542 | kind2 = PyUnicode_KIND(sep_obj); |
| 11543 | kind = kind1 > kind2 ? kind1 : kind2; |
| 11544 | buf1 = PyUnicode_DATA(str_in); |
| 11545 | if (kind1 != kind) |
| 11546 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11547 | if (!buf1) |
| 11548 | goto onError; |
| 11549 | buf2 = PyUnicode_DATA(sep_obj); |
| 11550 | if (kind2 != kind) |
| 11551 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11552 | if (!buf2) |
| 11553 | goto onError; |
| 11554 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11555 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11556 | |
| 11557 | switch(PyUnicode_KIND(str_in)) { |
| 11558 | case PyUnicode_1BYTE_KIND: |
| 11559 | out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11560 | break; |
| 11561 | case PyUnicode_2BYTE_KIND: |
| 11562 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11563 | break; |
| 11564 | case PyUnicode_4BYTE_KIND: |
| 11565 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11566 | break; |
| 11567 | default: |
| 11568 | assert(0); |
| 11569 | out = 0; |
| 11570 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11571 | |
| 11572 | Py_DECREF(sep_obj); |
| 11573 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11574 | if (kind1 != kind) |
| 11575 | PyMem_Free(buf1); |
| 11576 | if (kind2 != kind) |
| 11577 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11578 | |
| 11579 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11580 | onError: |
| 11581 | Py_DECREF(sep_obj); |
| 11582 | Py_DECREF(str_obj); |
| 11583 | if (kind1 != kind && buf1) |
| 11584 | PyMem_Free(buf1); |
| 11585 | if (kind2 != kind && buf2) |
| 11586 | PyMem_Free(buf2); |
| 11587 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11588 | } |
| 11589 | |
| 11590 | |
| 11591 | PyObject * |
| 11592 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11593 | { |
| 11594 | PyObject* str_obj; |
| 11595 | PyObject* sep_obj; |
| 11596 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11597 | int kind1, kind2, kind; |
| 11598 | void *buf1 = NULL, *buf2 = NULL; |
| 11599 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11600 | |
| 11601 | str_obj = PyUnicode_FromObject(str_in); |
| 11602 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11603 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11604 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11605 | if (!sep_obj) { |
| 11606 | Py_DECREF(str_obj); |
| 11607 | return NULL; |
| 11608 | } |
| 11609 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11610 | kind1 = PyUnicode_KIND(str_in); |
| 11611 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11612 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11613 | buf1 = PyUnicode_DATA(str_in); |
| 11614 | if (kind1 != kind) |
| 11615 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11616 | if (!buf1) |
| 11617 | goto onError; |
| 11618 | buf2 = PyUnicode_DATA(sep_obj); |
| 11619 | if (kind2 != kind) |
| 11620 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11621 | if (!buf2) |
| 11622 | goto onError; |
| 11623 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11624 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11625 | |
| 11626 | switch(PyUnicode_KIND(str_in)) { |
| 11627 | case PyUnicode_1BYTE_KIND: |
| 11628 | out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11629 | break; |
| 11630 | case PyUnicode_2BYTE_KIND: |
| 11631 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11632 | break; |
| 11633 | case PyUnicode_4BYTE_KIND: |
| 11634 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11635 | break; |
| 11636 | default: |
| 11637 | assert(0); |
| 11638 | out = 0; |
| 11639 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11640 | |
| 11641 | Py_DECREF(sep_obj); |
| 11642 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11643 | if (kind1 != kind) |
| 11644 | PyMem_Free(buf1); |
| 11645 | if (kind2 != kind) |
| 11646 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11647 | |
| 11648 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11649 | onError: |
| 11650 | Py_DECREF(sep_obj); |
| 11651 | Py_DECREF(str_obj); |
| 11652 | if (kind1 != kind && buf1) |
| 11653 | PyMem_Free(buf1); |
| 11654 | if (kind2 != kind && buf2) |
| 11655 | PyMem_Free(buf2); |
| 11656 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11657 | } |
| 11658 | |
| 11659 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11660 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11661 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11662 | 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] | 11663 | 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] | 11664 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11665 | |
| 11666 | static PyObject* |
| 11667 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 11668 | { |
| 11669 | return PyUnicode_Partition((PyObject *)self, separator); |
| 11670 | } |
| 11671 | |
| 11672 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11673 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11674 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11675 | 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] | 11676 | 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] | 11677 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11678 | |
| 11679 | static PyObject* |
| 11680 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 11681 | { |
| 11682 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 11683 | } |
| 11684 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11685 | PyObject * |
| 11686 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11687 | { |
| 11688 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11689 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11690 | s = PyUnicode_FromObject(s); |
| 11691 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11692 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11693 | if (sep != NULL) { |
| 11694 | sep = PyUnicode_FromObject(sep); |
| 11695 | if (sep == NULL) { |
| 11696 | Py_DECREF(s); |
| 11697 | return NULL; |
| 11698 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11699 | } |
| 11700 | |
| 11701 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11702 | |
| 11703 | Py_DECREF(s); |
| 11704 | Py_XDECREF(sep); |
| 11705 | return result; |
| 11706 | } |
| 11707 | |
| 11708 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11709 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11710 | \n\ |
| 11711 | Return a list of the words in S, using sep as the\n\ |
| 11712 | delimiter string, starting at the end of the string and\n\ |
| 11713 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 11714 | splits are done. If sep is not specified, any whitespace string\n\ |
| 11715 | is a separator."); |
| 11716 | |
| 11717 | static PyObject* |
| 11718 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 11719 | { |
| 11720 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11721 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11722 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11723 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11724 | return NULL; |
| 11725 | |
| 11726 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11727 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11728 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11729 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11730 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11731 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11732 | } |
| 11733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11734 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11735 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11736 | \n\ |
| 11737 | 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] | 11738 | 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] | 11739 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11740 | |
| 11741 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11742 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11743 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11744 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11745 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11746 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11747 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 11748 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11749 | return NULL; |
| 11750 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11751 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11752 | } |
| 11753 | |
| 11754 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 11755 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11756 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 11757 | if (PyUnicode_CheckExact(self)) { |
| 11758 | Py_INCREF(self); |
| 11759 | return self; |
| 11760 | } else |
| 11761 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11762 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11763 | } |
| 11764 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11765 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11766 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11767 | \n\ |
| 11768 | 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] | 11769 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11770 | |
| 11771 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11772 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11773 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11774 | return fixup(self, fixswapcase); |
| 11775 | } |
| 11776 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11777 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11778 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11779 | \n\ |
| 11780 | Return a translation table usable for str.translate().\n\ |
| 11781 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 11782 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11783 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11784 | If there are two arguments, they must be strings of equal length, and\n\ |
| 11785 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 11786 | character at the same position in y. If there is a third argument, it\n\ |
| 11787 | must be a string, whose characters will be mapped to None in the result."); |
| 11788 | |
| 11789 | static PyObject* |
| 11790 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 11791 | { |
| 11792 | PyObject *x, *y = NULL, *z = NULL; |
| 11793 | PyObject *new = NULL, *key, *value; |
| 11794 | Py_ssize_t i = 0; |
| 11795 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11796 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11797 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 11798 | return NULL; |
| 11799 | new = PyDict_New(); |
| 11800 | if (!new) |
| 11801 | return NULL; |
| 11802 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11803 | int x_kind, y_kind, z_kind; |
| 11804 | void *x_data, *y_data, *z_data; |
| 11805 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11806 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11807 | if (!PyUnicode_Check(x)) { |
| 11808 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 11809 | "be a string if there is a second argument"); |
| 11810 | goto err; |
| 11811 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11812 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11813 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 11814 | "arguments must have equal length"); |
| 11815 | goto err; |
| 11816 | } |
| 11817 | /* 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] | 11818 | x_kind = PyUnicode_KIND(x); |
| 11819 | y_kind = PyUnicode_KIND(y); |
| 11820 | x_data = PyUnicode_DATA(x); |
| 11821 | y_data = PyUnicode_DATA(y); |
| 11822 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 11823 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 11824 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11825 | if (!key || !value) |
| 11826 | goto err; |
| 11827 | res = PyDict_SetItem(new, key, value); |
| 11828 | Py_DECREF(key); |
| 11829 | Py_DECREF(value); |
| 11830 | if (res < 0) |
| 11831 | goto err; |
| 11832 | } |
| 11833 | /* create entries for deleting chars in z */ |
| 11834 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11835 | z_kind = PyUnicode_KIND(z); |
| 11836 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11837 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11838 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11839 | if (!key) |
| 11840 | goto err; |
| 11841 | res = PyDict_SetItem(new, key, Py_None); |
| 11842 | Py_DECREF(key); |
| 11843 | if (res < 0) |
| 11844 | goto err; |
| 11845 | } |
| 11846 | } |
| 11847 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11848 | int kind; |
| 11849 | void *data; |
| 11850 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11851 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 11852 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11853 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 11854 | "to maketrans it must be a dict"); |
| 11855 | goto err; |
| 11856 | } |
| 11857 | /* copy entries into the new dict, converting string keys to int keys */ |
| 11858 | while (PyDict_Next(x, &i, &key, &value)) { |
| 11859 | if (PyUnicode_Check(key)) { |
| 11860 | /* convert string keys to integer keys */ |
| 11861 | PyObject *newkey; |
| 11862 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 11863 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 11864 | "table must be of length 1"); |
| 11865 | goto err; |
| 11866 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11867 | kind = PyUnicode_KIND(key); |
| 11868 | data = PyUnicode_DATA(key); |
| 11869 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11870 | if (!newkey) |
| 11871 | goto err; |
| 11872 | res = PyDict_SetItem(new, newkey, value); |
| 11873 | Py_DECREF(newkey); |
| 11874 | if (res < 0) |
| 11875 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11876 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11877 | /* just keep integer keys */ |
| 11878 | if (PyDict_SetItem(new, key, value) < 0) |
| 11879 | goto err; |
| 11880 | } else { |
| 11881 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 11882 | "be strings or integers"); |
| 11883 | goto err; |
| 11884 | } |
| 11885 | } |
| 11886 | } |
| 11887 | return new; |
| 11888 | err: |
| 11889 | Py_DECREF(new); |
| 11890 | return NULL; |
| 11891 | } |
| 11892 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11893 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11894 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11895 | \n\ |
| 11896 | Return a copy of the string S, where all characters have been mapped\n\ |
| 11897 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11898 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 11899 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 11900 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11901 | |
| 11902 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11903 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11904 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11905 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11906 | } |
| 11907 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11908 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11909 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11910 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11911 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11912 | |
| 11913 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11914 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11915 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11916 | return fixup(self, fixupper); |
| 11917 | } |
| 11918 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11919 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11920 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11921 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 11922 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 11923 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11924 | |
| 11925 | static PyObject * |
| 11926 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 11927 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11928 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11929 | PyUnicodeObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11930 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11931 | int kind; |
| 11932 | void *data; |
| 11933 | Py_UCS4 chr; |
| 11934 | |
| 11935 | if (PyUnicode_READY(self) == -1) |
| 11936 | return NULL; |
| 11937 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11938 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11939 | return NULL; |
| 11940 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11941 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 11942 | if (PyUnicode_CheckExact(self)) { |
| 11943 | Py_INCREF(self); |
| 11944 | return (PyObject*) self; |
| 11945 | } |
| 11946 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 11947 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11948 | } |
| 11949 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11950 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11951 | |
| 11952 | u = pad(self, fill, 0, '0'); |
| 11953 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11954 | if (u == NULL) |
| 11955 | return NULL; |
| 11956 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11957 | kind = PyUnicode_KIND(u); |
| 11958 | data = PyUnicode_DATA(u); |
| 11959 | chr = PyUnicode_READ(kind, data, fill); |
| 11960 | |
| 11961 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11962 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11963 | PyUnicode_WRITE(kind, data, 0, chr); |
| 11964 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11965 | } |
| 11966 | |
| 11967 | return (PyObject*) u; |
| 11968 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11969 | |
| 11970 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11971 | static PyObject * |
| 11972 | unicode__decimal2ascii(PyObject *self) |
| 11973 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11974 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11975 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11976 | #endif |
| 11977 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11978 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11979 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11980 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11981 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 11982 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11983 | With optional end, stop comparing S at that position.\n\ |
| 11984 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11985 | |
| 11986 | static PyObject * |
| 11987 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11988 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11989 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11990 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11991 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11992 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11993 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11994 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11995 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11996 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11997 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11998 | if (PyTuple_Check(subobj)) { |
| 11999 | Py_ssize_t i; |
| 12000 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12001 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12002 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12003 | if (substring == NULL) |
| 12004 | return NULL; |
| 12005 | result = tailmatch(self, substring, start, end, -1); |
| 12006 | Py_DECREF(substring); |
| 12007 | if (result) { |
| 12008 | Py_RETURN_TRUE; |
| 12009 | } |
| 12010 | } |
| 12011 | /* nothing matched */ |
| 12012 | Py_RETURN_FALSE; |
| 12013 | } |
| 12014 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12015 | if (substring == NULL) { |
| 12016 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12017 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 12018 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12019 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12020 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12021 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12022 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12023 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12024 | } |
| 12025 | |
| 12026 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12027 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12028 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12029 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12030 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12031 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12032 | With optional end, stop comparing S at that position.\n\ |
| 12033 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12034 | |
| 12035 | static PyObject * |
| 12036 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12037 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12038 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12039 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12040 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12041 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12042 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12043 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12044 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12045 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12046 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12047 | if (PyTuple_Check(subobj)) { |
| 12048 | Py_ssize_t i; |
| 12049 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12050 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12051 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12052 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12053 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12054 | result = tailmatch(self, substring, start, end, +1); |
| 12055 | Py_DECREF(substring); |
| 12056 | if (result) { |
| 12057 | Py_RETURN_TRUE; |
| 12058 | } |
| 12059 | } |
| 12060 | Py_RETURN_FALSE; |
| 12061 | } |
| 12062 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12063 | if (substring == NULL) { |
| 12064 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12065 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12066 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12067 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12068 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12069 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12070 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12071 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12072 | } |
| 12073 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12074 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12075 | |
| 12076 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12077 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12078 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12079 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12080 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12081 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12082 | PyDoc_STRVAR(format_map__doc__, |
| 12083 | "S.format_map(mapping) -> str\n\ |
| 12084 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12085 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12086 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12087 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12088 | static PyObject * |
| 12089 | unicode__format__(PyObject* self, PyObject* args) |
| 12090 | { |
| 12091 | PyObject *format_spec; |
| 12092 | |
| 12093 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12094 | return NULL; |
| 12095 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12096 | return _PyUnicode_FormatAdvanced(self, format_spec, 0, |
| 12097 | PyUnicode_GET_LENGTH(format_spec)); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12098 | } |
| 12099 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12100 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12101 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12102 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12103 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12104 | |
| 12105 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12106 | unicode__sizeof__(PyUnicodeObject *v) |
| 12107 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12108 | Py_ssize_t size; |
| 12109 | |
| 12110 | /* If it's a compact object, account for base structure + |
| 12111 | character data. */ |
| 12112 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12113 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12114 | else if (PyUnicode_IS_COMPACT(v)) |
| 12115 | size = sizeof(PyCompactUnicodeObject) + |
| 12116 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); |
| 12117 | else { |
| 12118 | /* If it is a two-block object, account for base object, and |
| 12119 | for character block if present. */ |
| 12120 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12121 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12122 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
| 12123 | PyUnicode_CHARACTER_SIZE(v); |
| 12124 | } |
| 12125 | /* 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] | 12126 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12127 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12128 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12129 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12130 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12131 | |
| 12132 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12133 | } |
| 12134 | |
| 12135 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12136 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12137 | |
| 12138 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12139 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12140 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12141 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12142 | if (!copy) |
| 12143 | return NULL; |
| 12144 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12145 | } |
| 12146 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12147 | static PyMethodDef unicode_methods[] = { |
| 12148 | |
| 12149 | /* Order is according to common usage: often used methods should |
| 12150 | appear first, since lookup is done sequentially. */ |
| 12151 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12152 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12153 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12154 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12155 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12156 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12157 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12158 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12159 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12160 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12161 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12162 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12163 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12164 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12165 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12166 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12167 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12168 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12169 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12170 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12171 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12172 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12173 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12174 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12175 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12176 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12177 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12178 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12179 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12180 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12181 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12182 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12183 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12184 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12185 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12186 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12187 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12188 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12189 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12190 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12191 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12192 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12193 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12194 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12195 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12196 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12197 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12198 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12199 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12200 | #endif |
| 12201 | |
| 12202 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12203 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12204 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12205 | #endif |
| 12206 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12207 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12208 | {NULL, NULL} |
| 12209 | }; |
| 12210 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12211 | static PyObject * |
| 12212 | unicode_mod(PyObject *v, PyObject *w) |
| 12213 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12214 | if (!PyUnicode_Check(v)) |
| 12215 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12216 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12217 | } |
| 12218 | |
| 12219 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12220 | 0, /*nb_add*/ |
| 12221 | 0, /*nb_subtract*/ |
| 12222 | 0, /*nb_multiply*/ |
| 12223 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12224 | }; |
| 12225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12226 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12227 | (lenfunc) unicode_length, /* sq_length */ |
| 12228 | PyUnicode_Concat, /* sq_concat */ |
| 12229 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12230 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12231 | 0, /* sq_slice */ |
| 12232 | 0, /* sq_ass_item */ |
| 12233 | 0, /* sq_ass_slice */ |
| 12234 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12235 | }; |
| 12236 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12237 | static PyObject* |
| 12238 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 12239 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12240 | if (PyUnicode_READY(self) == -1) |
| 12241 | return NULL; |
| 12242 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12243 | if (PyIndex_Check(item)) { |
| 12244 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12245 | if (i == -1 && PyErr_Occurred()) |
| 12246 | return NULL; |
| 12247 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12248 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12249 | return unicode_getitem((PyObject*)self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12250 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12251 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12252 | const Py_UNICODE* source_buf; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12253 | Py_UNICODE* result_buf; |
| 12254 | PyObject* result; |
| 12255 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12256 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12257 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12258 | return NULL; |
| 12259 | } |
| 12260 | |
| 12261 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12262 | return PyUnicode_New(0, 0); |
| 12263 | } else if (start == 0 && step == 1 && |
| 12264 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12265 | PyUnicode_CheckExact(self)) { |
| 12266 | Py_INCREF(self); |
| 12267 | return (PyObject *)self; |
| 12268 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12269 | return PyUnicode_Substring((PyObject*)self, |
| 12270 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12271 | } else { |
| 12272 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 12273 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 12274 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12275 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12276 | if (result_buf == NULL) |
| 12277 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12278 | |
| 12279 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 12280 | result_buf[i] = source_buf[cur]; |
| 12281 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12282 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12283 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 12284 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12285 | return result; |
| 12286 | } |
| 12287 | } else { |
| 12288 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 12289 | return NULL; |
| 12290 | } |
| 12291 | } |
| 12292 | |
| 12293 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12294 | (lenfunc)unicode_length, /* mp_length */ |
| 12295 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 12296 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12297 | }; |
| 12298 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12299 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12300 | /* Helpers for PyUnicode_Format() */ |
| 12301 | |
| 12302 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12303 | getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12304 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12305 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12306 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12307 | (*p_argidx)++; |
| 12308 | if (arglen < 0) |
| 12309 | return args; |
| 12310 | else |
| 12311 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12312 | } |
| 12313 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12314 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12315 | return NULL; |
| 12316 | } |
| 12317 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12318 | /* 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] | 12319 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12320 | static PyObject * |
| 12321 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12322 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12323 | char *p; |
| 12324 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12325 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12326 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12327 | x = PyFloat_AsDouble(v); |
| 12328 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12329 | return NULL; |
| 12330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12331 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12332 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12333 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12334 | p = PyOS_double_to_string(x, type, prec, |
| 12335 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12336 | if (p == NULL) |
| 12337 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12338 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12339 | PyMem_Free(p); |
| 12340 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12341 | } |
| 12342 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12343 | static PyObject* |
| 12344 | formatlong(PyObject *val, int flags, int prec, int type) |
| 12345 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12346 | char *buf; |
| 12347 | int len; |
| 12348 | PyObject *str; /* temporary string object. */ |
| 12349 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12350 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12351 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 12352 | if (!str) |
| 12353 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12354 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12355 | Py_DECREF(str); |
| 12356 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12357 | } |
| 12358 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12359 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12360 | formatchar(Py_UCS4 *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12361 | size_t buflen, |
| 12362 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12363 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12364 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12365 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12366 | if (PyUnicode_GET_LENGTH(v) == 1) { |
| 12367 | buf[0] = PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12368 | buf[1] = '\0'; |
| 12369 | return 1; |
| 12370 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12371 | goto onError; |
| 12372 | } |
| 12373 | else { |
| 12374 | /* Integer input truncated to a character */ |
| 12375 | long x; |
| 12376 | x = PyLong_AsLong(v); |
| 12377 | if (x == -1 && PyErr_Occurred()) |
| 12378 | goto onError; |
| 12379 | |
| 12380 | if (x < 0 || x > 0x10ffff) { |
| 12381 | PyErr_SetString(PyExc_OverflowError, |
| 12382 | "%c arg not in range(0x110000)"); |
| 12383 | return -1; |
| 12384 | } |
| 12385 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12386 | buf[0] = (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12387 | buf[1] = '\0'; |
| 12388 | return 1; |
| 12389 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12390 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12391 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12392 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12393 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12394 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12395 | } |
| 12396 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12397 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12398 | FORMATBUFLEN is the length of the buffer in which chars are formatted. |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12399 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12400 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12401 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12402 | PyObject * |
| 12403 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12404 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12405 | void *fmt; |
| 12406 | int fmtkind; |
| 12407 | PyObject *result; |
| 12408 | Py_UCS4 *res, *res0; |
| 12409 | Py_UCS4 max; |
| 12410 | int kind; |
| 12411 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12412 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12413 | PyObject *dict = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12414 | PyUnicodeObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12416 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12417 | PyErr_BadInternalCall(); |
| 12418 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12419 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12420 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 12421 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12422 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12423 | fmt = PyUnicode_DATA(uformat); |
| 12424 | fmtkind = PyUnicode_KIND(uformat); |
| 12425 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 12426 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12427 | |
| 12428 | reslen = rescnt = fmtcnt + 100; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12429 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); |
| 12430 | if (res0 == NULL) { |
| 12431 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12432 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12433 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12434 | |
| 12435 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12436 | arglen = PyTuple_Size(args); |
| 12437 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12438 | } |
| 12439 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12440 | arglen = -1; |
| 12441 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12442 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12443 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12444 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12445 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12446 | |
| 12447 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12448 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12449 | if (--rescnt < 0) { |
| 12450 | rescnt = fmtcnt + 100; |
| 12451 | reslen += rescnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12452 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12453 | if (res0 == NULL){ |
| 12454 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12455 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12456 | } |
| 12457 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12458 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12459 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12460 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12461 | } |
| 12462 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12463 | /* Got a format specifier */ |
| 12464 | int flags = 0; |
| 12465 | Py_ssize_t width = -1; |
| 12466 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12467 | Py_UCS4 c = '\0'; |
| 12468 | Py_UCS4 fill; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12469 | int isnumok; |
| 12470 | PyObject *v = NULL; |
| 12471 | PyObject *temp = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12472 | void *pbuf; |
| 12473 | Py_ssize_t pindex; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12474 | Py_UNICODE sign; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12475 | Py_ssize_t len, len1; |
| 12476 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12477 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12478 | fmtpos++; |
| 12479 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12480 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12481 | Py_ssize_t keylen; |
| 12482 | PyObject *key; |
| 12483 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12484 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12485 | if (dict == NULL) { |
| 12486 | PyErr_SetString(PyExc_TypeError, |
| 12487 | "format requires a mapping"); |
| 12488 | goto onError; |
| 12489 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12490 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12491 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12492 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12493 | /* Skip over balanced parentheses */ |
| 12494 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12495 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12496 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12497 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12498 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12499 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12500 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12501 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12502 | if (fmtcnt < 0 || pcount > 0) { |
| 12503 | PyErr_SetString(PyExc_ValueError, |
| 12504 | "incomplete format key"); |
| 12505 | goto onError; |
| 12506 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12507 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12508 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12509 | if (key == NULL) |
| 12510 | goto onError; |
| 12511 | if (args_owned) { |
| 12512 | Py_DECREF(args); |
| 12513 | args_owned = 0; |
| 12514 | } |
| 12515 | args = PyObject_GetItem(dict, key); |
| 12516 | Py_DECREF(key); |
| 12517 | if (args == NULL) { |
| 12518 | goto onError; |
| 12519 | } |
| 12520 | args_owned = 1; |
| 12521 | arglen = -1; |
| 12522 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12523 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12524 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12525 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12526 | case '-': flags |= F_LJUST; continue; |
| 12527 | case '+': flags |= F_SIGN; continue; |
| 12528 | case ' ': flags |= F_BLANK; continue; |
| 12529 | case '#': flags |= F_ALT; continue; |
| 12530 | case '0': flags |= F_ZERO; continue; |
| 12531 | } |
| 12532 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12533 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12534 | if (c == '*') { |
| 12535 | v = getnextarg(args, arglen, &argidx); |
| 12536 | if (v == NULL) |
| 12537 | goto onError; |
| 12538 | if (!PyLong_Check(v)) { |
| 12539 | PyErr_SetString(PyExc_TypeError, |
| 12540 | "* wants int"); |
| 12541 | goto onError; |
| 12542 | } |
| 12543 | width = PyLong_AsLong(v); |
| 12544 | if (width == -1 && PyErr_Occurred()) |
| 12545 | goto onError; |
| 12546 | if (width < 0) { |
| 12547 | flags |= F_LJUST; |
| 12548 | width = -width; |
| 12549 | } |
| 12550 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12551 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12552 | } |
| 12553 | else if (c >= '0' && c <= '9') { |
| 12554 | width = c - '0'; |
| 12555 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12556 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12557 | if (c < '0' || c > '9') |
| 12558 | break; |
| 12559 | if ((width*10) / 10 != width) { |
| 12560 | PyErr_SetString(PyExc_ValueError, |
| 12561 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12562 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12563 | } |
| 12564 | width = width*10 + (c - '0'); |
| 12565 | } |
| 12566 | } |
| 12567 | if (c == '.') { |
| 12568 | prec = 0; |
| 12569 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12570 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12571 | if (c == '*') { |
| 12572 | v = getnextarg(args, arglen, &argidx); |
| 12573 | if (v == NULL) |
| 12574 | goto onError; |
| 12575 | if (!PyLong_Check(v)) { |
| 12576 | PyErr_SetString(PyExc_TypeError, |
| 12577 | "* wants int"); |
| 12578 | goto onError; |
| 12579 | } |
| 12580 | prec = PyLong_AsLong(v); |
| 12581 | if (prec == -1 && PyErr_Occurred()) |
| 12582 | goto onError; |
| 12583 | if (prec < 0) |
| 12584 | prec = 0; |
| 12585 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12586 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12587 | } |
| 12588 | else if (c >= '0' && c <= '9') { |
| 12589 | prec = c - '0'; |
| 12590 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12591 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12592 | if (c < '0' || c > '9') |
| 12593 | break; |
| 12594 | if ((prec*10) / 10 != prec) { |
| 12595 | PyErr_SetString(PyExc_ValueError, |
| 12596 | "prec too big"); |
| 12597 | goto onError; |
| 12598 | } |
| 12599 | prec = prec*10 + (c - '0'); |
| 12600 | } |
| 12601 | } |
| 12602 | } /* prec */ |
| 12603 | if (fmtcnt >= 0) { |
| 12604 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12605 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12606 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12607 | } |
| 12608 | } |
| 12609 | if (fmtcnt < 0) { |
| 12610 | PyErr_SetString(PyExc_ValueError, |
| 12611 | "incomplete format"); |
| 12612 | goto onError; |
| 12613 | } |
| 12614 | if (c != '%') { |
| 12615 | v = getnextarg(args, arglen, &argidx); |
| 12616 | if (v == NULL) |
| 12617 | goto onError; |
| 12618 | } |
| 12619 | sign = 0; |
| 12620 | fill = ' '; |
| 12621 | switch (c) { |
| 12622 | |
| 12623 | case '%': |
| 12624 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12625 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12626 | /* presume that buffer length is at least 1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12627 | PyUnicode_WRITE(kind, pbuf, 0, '%'); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12628 | len = 1; |
| 12629 | break; |
| 12630 | |
| 12631 | case 's': |
| 12632 | case 'r': |
| 12633 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12634 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12635 | temp = v; |
| 12636 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12637 | } |
| 12638 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12639 | if (c == 's') |
| 12640 | temp = PyObject_Str(v); |
| 12641 | else if (c == 'r') |
| 12642 | temp = PyObject_Repr(v); |
| 12643 | else |
| 12644 | temp = PyObject_ASCII(v); |
| 12645 | if (temp == NULL) |
| 12646 | goto onError; |
| 12647 | if (PyUnicode_Check(temp)) |
| 12648 | /* nothing to do */; |
| 12649 | else { |
| 12650 | Py_DECREF(temp); |
| 12651 | PyErr_SetString(PyExc_TypeError, |
| 12652 | "%s argument has non-string str()"); |
| 12653 | goto onError; |
| 12654 | } |
| 12655 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12656 | if (PyUnicode_READY(temp) == -1) { |
| 12657 | Py_CLEAR(temp); |
| 12658 | goto onError; |
| 12659 | } |
| 12660 | pbuf = PyUnicode_DATA(temp); |
| 12661 | kind = PyUnicode_KIND(temp); |
| 12662 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12663 | if (prec >= 0 && len > prec) |
| 12664 | len = prec; |
| 12665 | break; |
| 12666 | |
| 12667 | case 'i': |
| 12668 | case 'd': |
| 12669 | case 'u': |
| 12670 | case 'o': |
| 12671 | case 'x': |
| 12672 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12673 | isnumok = 0; |
| 12674 | if (PyNumber_Check(v)) { |
| 12675 | PyObject *iobj=NULL; |
| 12676 | |
| 12677 | if (PyLong_Check(v)) { |
| 12678 | iobj = v; |
| 12679 | Py_INCREF(iobj); |
| 12680 | } |
| 12681 | else { |
| 12682 | iobj = PyNumber_Long(v); |
| 12683 | } |
| 12684 | if (iobj!=NULL) { |
| 12685 | if (PyLong_Check(iobj)) { |
| 12686 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 12687 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12688 | Py_DECREF(iobj); |
| 12689 | if (!temp) |
| 12690 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12691 | if (PyUnicode_READY(temp) == -1) { |
| 12692 | Py_CLEAR(temp); |
| 12693 | goto onError; |
| 12694 | } |
| 12695 | pbuf = PyUnicode_DATA(temp); |
| 12696 | kind = PyUnicode_KIND(temp); |
| 12697 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12698 | sign = 1; |
| 12699 | } |
| 12700 | else { |
| 12701 | Py_DECREF(iobj); |
| 12702 | } |
| 12703 | } |
| 12704 | } |
| 12705 | if (!isnumok) { |
| 12706 | PyErr_Format(PyExc_TypeError, |
| 12707 | "%%%c format: a number is required, " |
| 12708 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 12709 | goto onError; |
| 12710 | } |
| 12711 | if (flags & F_ZERO) |
| 12712 | fill = '0'; |
| 12713 | break; |
| 12714 | |
| 12715 | case 'e': |
| 12716 | case 'E': |
| 12717 | case 'f': |
| 12718 | case 'F': |
| 12719 | case 'g': |
| 12720 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12721 | temp = formatfloat(v, flags, prec, c); |
| 12722 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12723 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12724 | if (PyUnicode_READY(temp) == -1) { |
| 12725 | Py_CLEAR(temp); |
| 12726 | goto onError; |
| 12727 | } |
| 12728 | pbuf = PyUnicode_DATA(temp); |
| 12729 | kind = PyUnicode_KIND(temp); |
| 12730 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12731 | sign = 1; |
| 12732 | if (flags & F_ZERO) |
| 12733 | fill = '0'; |
| 12734 | break; |
| 12735 | |
| 12736 | case 'c': |
| 12737 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12738 | kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | b9dcffb | 2011-09-29 00:39:24 +0200 | [diff] [blame] | 12739 | len = formatchar(pbuf, Py_ARRAY_LENGTH(formatbuf), v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12740 | if (len < 0) |
| 12741 | goto onError; |
| 12742 | break; |
| 12743 | |
| 12744 | default: |
| 12745 | PyErr_Format(PyExc_ValueError, |
| 12746 | "unsupported format character '%c' (0x%x) " |
| 12747 | "at index %zd", |
| 12748 | (31<=c && c<=126) ? (char)c : '?', |
| 12749 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12750 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12751 | goto onError; |
| 12752 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12753 | /* pbuf is initialized here. */ |
| 12754 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12755 | if (sign) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12756 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || |
| 12757 | PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 12758 | sign = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12759 | len--; |
| 12760 | } |
| 12761 | else if (flags & F_SIGN) |
| 12762 | sign = '+'; |
| 12763 | else if (flags & F_BLANK) |
| 12764 | sign = ' '; |
| 12765 | else |
| 12766 | sign = 0; |
| 12767 | } |
| 12768 | if (width < len) |
| 12769 | width = len; |
| 12770 | if (rescnt - (sign != 0) < width) { |
| 12771 | reslen -= rescnt; |
| 12772 | rescnt = width + fmtcnt + 100; |
| 12773 | reslen += rescnt; |
| 12774 | if (reslen < 0) { |
| 12775 | Py_XDECREF(temp); |
| 12776 | PyErr_NoMemory(); |
| 12777 | goto onError; |
| 12778 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12779 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12780 | if (res0 == 0) { |
| 12781 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12782 | Py_XDECREF(temp); |
| 12783 | goto onError; |
| 12784 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12785 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12786 | } |
| 12787 | if (sign) { |
| 12788 | if (fill != ' ') |
| 12789 | *res++ = sign; |
| 12790 | rescnt--; |
| 12791 | if (width > len) |
| 12792 | width--; |
| 12793 | } |
| 12794 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12795 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12796 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12797 | if (fill != ' ') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12798 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12799 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12800 | } |
| 12801 | rescnt -= 2; |
| 12802 | width -= 2; |
| 12803 | if (width < 0) |
| 12804 | width = 0; |
| 12805 | len -= 2; |
| 12806 | } |
| 12807 | if (width > len && !(flags & F_LJUST)) { |
| 12808 | do { |
| 12809 | --rescnt; |
| 12810 | *res++ = fill; |
| 12811 | } while (--width > len); |
| 12812 | } |
| 12813 | if (fill == ' ') { |
| 12814 | if (sign) |
| 12815 | *res++ = sign; |
| 12816 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12817 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12818 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
| 12819 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12820 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12821 | } |
| 12822 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12823 | /* Copy all characters, preserving len */ |
| 12824 | len1 = len; |
| 12825 | while (len1--) { |
| 12826 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12827 | rescnt--; |
| 12828 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12829 | while (--width >= len) { |
| 12830 | --rescnt; |
| 12831 | *res++ = ' '; |
| 12832 | } |
| 12833 | if (dict && (argidx < arglen) && c != '%') { |
| 12834 | PyErr_SetString(PyExc_TypeError, |
| 12835 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 12836 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12837 | goto onError; |
| 12838 | } |
| 12839 | Py_XDECREF(temp); |
| 12840 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12841 | } /* until end */ |
| 12842 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12843 | PyErr_SetString(PyExc_TypeError, |
| 12844 | "not all arguments converted during string formatting"); |
| 12845 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12846 | } |
| 12847 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12848 | |
| 12849 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) |
| 12850 | if (*res > max) |
| 12851 | max = *res; |
| 12852 | result = PyUnicode_New(reslen - rescnt, max); |
| 12853 | if (!result) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12854 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12855 | kind = PyUnicode_KIND(result); |
| 12856 | for (res = res0; res < res0+reslen-rescnt; res++) |
| 12857 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); |
| 12858 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12859 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12860 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12861 | } |
| 12862 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12863 | return (PyObject *)result; |
| 12864 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12865 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12866 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12867 | Py_DECREF(uformat); |
| 12868 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12869 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12870 | } |
| 12871 | return NULL; |
| 12872 | } |
| 12873 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 12874 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12875 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 12876 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12877 | static PyObject * |
| 12878 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12879 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12880 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12881 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 12882 | char *encoding = NULL; |
| 12883 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12884 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12885 | if (type != &PyUnicode_Type) |
| 12886 | return unicode_subtype_new(type, args, kwds); |
| 12887 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12888 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12889 | return NULL; |
| 12890 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12891 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12892 | if (encoding == NULL && errors == NULL) |
| 12893 | return PyObject_Str(x); |
| 12894 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12895 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12896 | } |
| 12897 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12898 | static PyObject * |
| 12899 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12900 | { |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12901 | PyUnicodeObject *unicode, *self; |
| 12902 | Py_ssize_t length, char_size; |
| 12903 | int share_wstr, share_utf8; |
| 12904 | unsigned int kind; |
| 12905 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12906 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12907 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12908 | |
| 12909 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 12910 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12911 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 12912 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 12913 | if (_PyUnicode_READY_REPLACE(&unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12914 | return NULL; |
| 12915 | |
| 12916 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); |
| 12917 | if (self == NULL) { |
| 12918 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12919 | return NULL; |
| 12920 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12921 | kind = PyUnicode_KIND(unicode); |
| 12922 | length = PyUnicode_GET_LENGTH(unicode); |
| 12923 | |
| 12924 | _PyUnicode_LENGTH(self) = length; |
| 12925 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 12926 | _PyUnicode_STATE(self).interned = 0; |
| 12927 | _PyUnicode_STATE(self).kind = kind; |
| 12928 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 12929 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12930 | _PyUnicode_STATE(self).ready = 1; |
| 12931 | _PyUnicode_WSTR(self) = NULL; |
| 12932 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 12933 | _PyUnicode_UTF8(self) = NULL; |
| 12934 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12935 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12936 | |
| 12937 | share_utf8 = 0; |
| 12938 | share_wstr = 0; |
| 12939 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12940 | char_size = 1; |
| 12941 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 12942 | share_utf8 = 1; |
| 12943 | } |
| 12944 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12945 | char_size = 2; |
| 12946 | if (sizeof(wchar_t) == 2) |
| 12947 | share_wstr = 1; |
| 12948 | } |
| 12949 | else { |
| 12950 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12951 | char_size = 4; |
| 12952 | if (sizeof(wchar_t) == 4) |
| 12953 | share_wstr = 1; |
| 12954 | } |
| 12955 | |
| 12956 | /* Ensure we won't overflow the length. */ |
| 12957 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 12958 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12959 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12960 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12961 | data = PyObject_MALLOC((length + 1) * char_size); |
| 12962 | if (data == NULL) { |
| 12963 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12964 | goto onError; |
| 12965 | } |
| 12966 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12967 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12968 | if (share_utf8) { |
| 12969 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 12970 | _PyUnicode_UTF8(self) = data; |
| 12971 | } |
| 12972 | if (share_wstr) { |
| 12973 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 12974 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 12975 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12976 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12977 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
| 12978 | PyUnicode_KIND_SIZE(kind, length + 1)); |
| 12979 | Py_DECREF(unicode); |
| 12980 | return (PyObject *)self; |
| 12981 | |
| 12982 | onError: |
| 12983 | Py_DECREF(unicode); |
| 12984 | Py_DECREF(self); |
| 12985 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12986 | } |
| 12987 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12988 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12989 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12990 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 12991 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 12992 | encoding defaults to the current default string encoding.\n\ |
| 12993 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12994 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12995 | static PyObject *unicode_iter(PyObject *seq); |
| 12996 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12997 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 12998 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12999 | "str", /* tp_name */ |
| 13000 | sizeof(PyUnicodeObject), /* tp_size */ |
| 13001 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13002 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13003 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 13004 | 0, /* tp_print */ |
| 13005 | 0, /* tp_getattr */ |
| 13006 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13007 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13008 | unicode_repr, /* tp_repr */ |
| 13009 | &unicode_as_number, /* tp_as_number */ |
| 13010 | &unicode_as_sequence, /* tp_as_sequence */ |
| 13011 | &unicode_as_mapping, /* tp_as_mapping */ |
| 13012 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 13013 | 0, /* tp_call*/ |
| 13014 | (reprfunc) unicode_str, /* tp_str */ |
| 13015 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13016 | 0, /* tp_setattro */ |
| 13017 | 0, /* tp_as_buffer */ |
| 13018 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13019 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13020 | unicode_doc, /* tp_doc */ |
| 13021 | 0, /* tp_traverse */ |
| 13022 | 0, /* tp_clear */ |
| 13023 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13024 | 0, /* tp_weaklistoffset */ |
| 13025 | unicode_iter, /* tp_iter */ |
| 13026 | 0, /* tp_iternext */ |
| 13027 | unicode_methods, /* tp_methods */ |
| 13028 | 0, /* tp_members */ |
| 13029 | 0, /* tp_getset */ |
| 13030 | &PyBaseObject_Type, /* tp_base */ |
| 13031 | 0, /* tp_dict */ |
| 13032 | 0, /* tp_descr_get */ |
| 13033 | 0, /* tp_descr_set */ |
| 13034 | 0, /* tp_dictoffset */ |
| 13035 | 0, /* tp_init */ |
| 13036 | 0, /* tp_alloc */ |
| 13037 | unicode_new, /* tp_new */ |
| 13038 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13039 | }; |
| 13040 | |
| 13041 | /* Initialize the Unicode implementation */ |
| 13042 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13043 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13044 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13045 | int i; |
| 13046 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13047 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13048 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13049 | 0x000A, /* LINE FEED */ |
| 13050 | 0x000D, /* CARRIAGE RETURN */ |
| 13051 | 0x001C, /* FILE SEPARATOR */ |
| 13052 | 0x001D, /* GROUP SEPARATOR */ |
| 13053 | 0x001E, /* RECORD SEPARATOR */ |
| 13054 | 0x0085, /* NEXT LINE */ |
| 13055 | 0x2028, /* LINE SEPARATOR */ |
| 13056 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13057 | }; |
| 13058 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13059 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13060 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13061 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13062 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13063 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13064 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13065 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13066 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13067 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13068 | |
| 13069 | /* initialize the linebreak bloom filter */ |
| 13070 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13071 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13072 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13073 | |
| 13074 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13075 | } |
| 13076 | |
| 13077 | /* Finalize the Unicode implementation */ |
| 13078 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13079 | int |
| 13080 | PyUnicode_ClearFreeList(void) |
| 13081 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13082 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13083 | } |
| 13084 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13085 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13086 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13087 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13088 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13089 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13090 | Py_XDECREF(unicode_empty); |
| 13091 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13092 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13093 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13094 | if (unicode_latin1[i]) { |
| 13095 | Py_DECREF(unicode_latin1[i]); |
| 13096 | unicode_latin1[i] = NULL; |
| 13097 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13098 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13099 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13100 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13101 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13102 | void |
| 13103 | PyUnicode_InternInPlace(PyObject **p) |
| 13104 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13105 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 13106 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13107 | #ifdef Py_DEBUG |
| 13108 | assert(s != NULL); |
| 13109 | assert(_PyUnicode_CHECK(s)); |
| 13110 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13111 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13112 | return; |
| 13113 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13114 | /* If it's a subclass, we don't really know what putting |
| 13115 | it in the interned dict might do. */ |
| 13116 | if (!PyUnicode_CheckExact(s)) |
| 13117 | return; |
| 13118 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13119 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13120 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13121 | assert(0 && "PyUnicode_READY fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13122 | return; |
| 13123 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13124 | s = (PyUnicodeObject *)(*p); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13125 | if (interned == NULL) { |
| 13126 | interned = PyDict_New(); |
| 13127 | if (interned == NULL) { |
| 13128 | PyErr_Clear(); /* Don't leave an exception */ |
| 13129 | return; |
| 13130 | } |
| 13131 | } |
| 13132 | /* It might be that the GetItem call fails even |
| 13133 | though the key is present in the dictionary, |
| 13134 | namely when this happens during a stack overflow. */ |
| 13135 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13136 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13137 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13138 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13139 | if (t) { |
| 13140 | Py_INCREF(t); |
| 13141 | Py_DECREF(*p); |
| 13142 | *p = t; |
| 13143 | return; |
| 13144 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13145 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13146 | PyThreadState_GET()->recursion_critical = 1; |
| 13147 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 13148 | PyErr_Clear(); |
| 13149 | PyThreadState_GET()->recursion_critical = 0; |
| 13150 | return; |
| 13151 | } |
| 13152 | PyThreadState_GET()->recursion_critical = 0; |
| 13153 | /* The two references in interned are not counted by refcnt. |
| 13154 | The deallocator will take care of this */ |
| 13155 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13156 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13157 | } |
| 13158 | |
| 13159 | void |
| 13160 | PyUnicode_InternImmortal(PyObject **p) |
| 13161 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13162 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 13163 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13164 | PyUnicode_InternInPlace(p); |
| 13165 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13166 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13167 | Py_INCREF(*p); |
| 13168 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13169 | } |
| 13170 | |
| 13171 | PyObject * |
| 13172 | PyUnicode_InternFromString(const char *cp) |
| 13173 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13174 | PyObject *s = PyUnicode_FromString(cp); |
| 13175 | if (s == NULL) |
| 13176 | return NULL; |
| 13177 | PyUnicode_InternInPlace(&s); |
| 13178 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13179 | } |
| 13180 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13181 | void |
| 13182 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13183 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13184 | PyObject *keys; |
| 13185 | PyUnicodeObject *s; |
| 13186 | Py_ssize_t i, n; |
| 13187 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13188 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13189 | if (interned == NULL || !PyDict_Check(interned)) |
| 13190 | return; |
| 13191 | keys = PyDict_Keys(interned); |
| 13192 | if (keys == NULL || !PyList_Check(keys)) { |
| 13193 | PyErr_Clear(); |
| 13194 | return; |
| 13195 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13196 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13197 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 13198 | detector, interned unicode strings are not forcibly deallocated; |
| 13199 | rather, we give them their stolen references back, and then clear |
| 13200 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13201 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13202 | n = PyList_GET_SIZE(keys); |
| 13203 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13204 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13205 | for (i = 0; i < n; i++) { |
| 13206 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13207 | if (PyUnicode_READY(s) == -1) |
| 13208 | fprintf(stderr, "could not ready string\n"); |
| 13209 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13210 | case SSTATE_NOT_INTERNED: |
| 13211 | /* XXX Shouldn't happen */ |
| 13212 | break; |
| 13213 | case SSTATE_INTERNED_IMMORTAL: |
| 13214 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13215 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13216 | break; |
| 13217 | case SSTATE_INTERNED_MORTAL: |
| 13218 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13219 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13220 | break; |
| 13221 | default: |
| 13222 | Py_FatalError("Inconsistent interned string state."); |
| 13223 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13224 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13225 | } |
| 13226 | fprintf(stderr, "total size of all interned strings: " |
| 13227 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 13228 | "mortal/immortal\n", mortal_size, immortal_size); |
| 13229 | Py_DECREF(keys); |
| 13230 | PyDict_Clear(interned); |
| 13231 | Py_DECREF(interned); |
| 13232 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13233 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13234 | |
| 13235 | |
| 13236 | /********************* Unicode Iterator **************************/ |
| 13237 | |
| 13238 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13239 | PyObject_HEAD |
| 13240 | Py_ssize_t it_index; |
| 13241 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13242 | } unicodeiterobject; |
| 13243 | |
| 13244 | static void |
| 13245 | unicodeiter_dealloc(unicodeiterobject *it) |
| 13246 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13247 | _PyObject_GC_UNTRACK(it); |
| 13248 | Py_XDECREF(it->it_seq); |
| 13249 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13250 | } |
| 13251 | |
| 13252 | static int |
| 13253 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 13254 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13255 | Py_VISIT(it->it_seq); |
| 13256 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13257 | } |
| 13258 | |
| 13259 | static PyObject * |
| 13260 | unicodeiter_next(unicodeiterobject *it) |
| 13261 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13262 | PyUnicodeObject *seq; |
| 13263 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13264 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13265 | assert(it != NULL); |
| 13266 | seq = it->it_seq; |
| 13267 | if (seq == NULL) |
| 13268 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13269 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13270 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13271 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 13272 | int kind = PyUnicode_KIND(seq); |
| 13273 | void *data = PyUnicode_DATA(seq); |
| 13274 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 13275 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13276 | if (item != NULL) |
| 13277 | ++it->it_index; |
| 13278 | return item; |
| 13279 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13280 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13281 | Py_DECREF(seq); |
| 13282 | it->it_seq = NULL; |
| 13283 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13284 | } |
| 13285 | |
| 13286 | static PyObject * |
| 13287 | unicodeiter_len(unicodeiterobject *it) |
| 13288 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13289 | Py_ssize_t len = 0; |
| 13290 | if (it->it_seq) |
| 13291 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 13292 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13293 | } |
| 13294 | |
| 13295 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 13296 | |
| 13297 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13298 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13299 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13300 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13301 | }; |
| 13302 | |
| 13303 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13304 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 13305 | "str_iterator", /* tp_name */ |
| 13306 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 13307 | 0, /* tp_itemsize */ |
| 13308 | /* methods */ |
| 13309 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 13310 | 0, /* tp_print */ |
| 13311 | 0, /* tp_getattr */ |
| 13312 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13313 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13314 | 0, /* tp_repr */ |
| 13315 | 0, /* tp_as_number */ |
| 13316 | 0, /* tp_as_sequence */ |
| 13317 | 0, /* tp_as_mapping */ |
| 13318 | 0, /* tp_hash */ |
| 13319 | 0, /* tp_call */ |
| 13320 | 0, /* tp_str */ |
| 13321 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13322 | 0, /* tp_setattro */ |
| 13323 | 0, /* tp_as_buffer */ |
| 13324 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 13325 | 0, /* tp_doc */ |
| 13326 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 13327 | 0, /* tp_clear */ |
| 13328 | 0, /* tp_richcompare */ |
| 13329 | 0, /* tp_weaklistoffset */ |
| 13330 | PyObject_SelfIter, /* tp_iter */ |
| 13331 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 13332 | unicodeiter_methods, /* tp_methods */ |
| 13333 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13334 | }; |
| 13335 | |
| 13336 | static PyObject * |
| 13337 | unicode_iter(PyObject *seq) |
| 13338 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13339 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13340 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13341 | if (!PyUnicode_Check(seq)) { |
| 13342 | PyErr_BadInternalCall(); |
| 13343 | return NULL; |
| 13344 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13345 | if (PyUnicode_READY(seq) == -1) |
| 13346 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13347 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 13348 | if (it == NULL) |
| 13349 | return NULL; |
| 13350 | it->it_index = 0; |
| 13351 | Py_INCREF(seq); |
| 13352 | it->it_seq = (PyUnicodeObject *)seq; |
| 13353 | _PyObject_GC_TRACK(it); |
| 13354 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13355 | } |
| 13356 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13357 | #define UNIOP(x) Py_UNICODE_##x |
| 13358 | #define UNIOP_t Py_UNICODE |
| 13359 | #include "uniops.h" |
| 13360 | #undef UNIOP |
| 13361 | #undef UNIOP_t |
| 13362 | #define UNIOP(x) Py_UCS4_##x |
| 13363 | #define UNIOP_t Py_UCS4 |
| 13364 | #include "uniops.h" |
| 13365 | #undef UNIOP |
| 13366 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 13367 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13368 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 13369 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13370 | { |
| 13371 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 13372 | Py_UNICODE *copy; |
| 13373 | Py_ssize_t size; |
| 13374 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13375 | if (!PyUnicode_Check(unicode)) { |
| 13376 | PyErr_BadArgument(); |
| 13377 | return NULL; |
| 13378 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13379 | /* Ensure we won't overflow the size. */ |
| 13380 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 13381 | PyErr_NoMemory(); |
| 13382 | return NULL; |
| 13383 | } |
| 13384 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 13385 | size *= sizeof(Py_UNICODE); |
| 13386 | copy = PyMem_Malloc(size); |
| 13387 | if (copy == NULL) { |
| 13388 | PyErr_NoMemory(); |
| 13389 | return NULL; |
| 13390 | } |
| 13391 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 13392 | return copy; |
| 13393 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13394 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13395 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 13396 | to the string.Formatter class implemented in Python. */ |
| 13397 | |
| 13398 | static PyMethodDef _string_methods[] = { |
| 13399 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 13400 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 13401 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 13402 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 13403 | {NULL, NULL} |
| 13404 | }; |
| 13405 | |
| 13406 | static struct PyModuleDef _string_module = { |
| 13407 | PyModuleDef_HEAD_INIT, |
| 13408 | "_string", |
| 13409 | PyDoc_STR("string helper module"), |
| 13410 | 0, |
| 13411 | _string_methods, |
| 13412 | NULL, |
| 13413 | NULL, |
| 13414 | NULL, |
| 13415 | NULL |
| 13416 | }; |
| 13417 | |
| 13418 | PyMODINIT_FUNC |
| 13419 | PyInit__string(void) |
| 13420 | { |
| 13421 | return PyModule_Create(&_string_module); |
| 13422 | } |
| 13423 | |
| 13424 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13425 | #ifdef __cplusplus |
| 13426 | } |
| 13427 | #endif |