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)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 114 | #define _PyUnicode_WSTR(op) (((PyASCIIObject*)(op))->wstr) |
| 115 | #define _PyUnicode_WSTR_LENGTH(op) (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 116 | #define _PyUnicode_LENGTH(op) (((PyASCIIObject *)(op))->length) |
| 117 | #define _PyUnicode_STATE(op) (((PyASCIIObject *)(op))->state) |
| 118 | #define _PyUnicode_HASH(op) (((PyASCIIObject *)(op))->hash) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 119 | #define _PyUnicode_KIND(op) \ |
| 120 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 121 | ((PyASCIIObject *)(op))->state.kind) |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 122 | #define _PyUnicode_GET_LENGTH(op) \ |
| 123 | (assert(_PyUnicode_CHECK(op)), \ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 124 | ((PyASCIIObject *)(op))->length) |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 125 | #define _PyUnicode_DATA_ANY(op) (((PyUnicodeObject*)(op))->data.any) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 126 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 127 | #undef PyUnicode_READY |
| 128 | #define PyUnicode_READY(op) \ |
| 129 | (assert(_PyUnicode_CHECK(op)), \ |
| 130 | (PyUnicode_IS_READY(op) ? \ |
| 131 | 0 : _PyUnicode_Ready((PyObject *)(op)))) |
| 132 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 133 | #define _PyUnicode_READY_REPLACE(p_obj) \ |
| 134 | (assert(_PyUnicode_CHECK(*p_obj)), \ |
| 135 | (PyUnicode_IS_READY(*p_obj) ? \ |
| 136 | 0 : _PyUnicode_ReadyReplace((PyObject **)(p_obj)))) |
| 137 | |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 138 | #define _PyUnicode_SHARE_UTF8(op) \ |
| 139 | (assert(_PyUnicode_CHECK(op)), \ |
| 140 | assert(!PyUnicode_IS_COMPACT_ASCII(op)), \ |
| 141 | (_PyUnicode_UTF8(op) == PyUnicode_DATA(op))) |
| 142 | #define _PyUnicode_SHARE_WSTR(op) \ |
| 143 | (assert(_PyUnicode_CHECK(op)), \ |
| 144 | (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op))) |
| 145 | |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 146 | /* true if the Unicode object has an allocated UTF-8 memory block |
| 147 | (not shared with other data) */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 148 | #define _PyUnicode_HAS_UTF8_MEMORY(op) \ |
| 149 | (assert(_PyUnicode_CHECK(op)), \ |
| 150 | (!PyUnicode_IS_COMPACT_ASCII(op) \ |
| 151 | && _PyUnicode_UTF8(op) \ |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 152 | && _PyUnicode_UTF8(op) != PyUnicode_DATA(op))) |
| 153 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 154 | /* true if the Unicode object has an allocated wstr memory block |
| 155 | (not shared with other data) */ |
| 156 | #define _PyUnicode_HAS_WSTR_MEMORY(op) \ |
| 157 | (assert(_PyUnicode_CHECK(op)), \ |
| 158 | (_PyUnicode_WSTR(op) && \ |
| 159 | (!PyUnicode_IS_READY(op) || \ |
| 160 | _PyUnicode_WSTR(op) != PyUnicode_DATA(op)))) |
| 161 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 162 | /* Generic helper macro to convert characters of different types. |
| 163 | from_type and to_type have to be valid type names, begin and end |
| 164 | are pointers to the source characters which should be of type |
| 165 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 166 | buffer where the result characters are written to. */ |
| 167 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 168 | do { \ |
| 169 | const from_type *iter_; to_type *to_; \ |
| 170 | for (iter_ = (begin), to_ = (to_type *)(to); \ |
| 171 | iter_ < (end); \ |
| 172 | ++iter_, ++to_) { \ |
| 173 | *to_ = (to_type)*iter_; \ |
| 174 | } \ |
| 175 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 176 | |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 177 | /* The Unicode string has been modified: reset the hash */ |
| 178 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) |
| 179 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 180 | /* This dictionary holds all interned unicode strings. Note that references |
| 181 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 182 | When the interned string reaches a refcnt of 0 the string deallocation |
| 183 | function will delete the reference from this dictionary. |
| 184 | |
| 185 | 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] | 186 | 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] | 187 | */ |
| 188 | static PyObject *interned; |
| 189 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 190 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 191 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 192 | |
| 193 | /* Single character Unicode strings in the Latin-1 range are being |
| 194 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 195 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 196 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 197 | /* Fast detection of the most frequent whitespace characters */ |
| 198 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 199 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 200 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 201 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 202 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 203 | /* case 0x000C: * FORM FEED */ |
| 204 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 205 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 206 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 207 | /* case 0x001C: * FILE SEPARATOR */ |
| 208 | /* case 0x001D: * GROUP SEPARATOR */ |
| 209 | /* case 0x001E: * RECORD SEPARATOR */ |
| 210 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 211 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 212 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 213 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 214 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 215 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 216 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 217 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 218 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 219 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 220 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 221 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 222 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 223 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 224 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 225 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 228 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 229 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 230 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 231 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 232 | static PyObject * |
| 233 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 234 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 235 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 236 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 237 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 238 | static void |
| 239 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 240 | const char *encoding, |
| 241 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 242 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 243 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 244 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 245 | /* Same for linebreaks */ |
| 246 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 247 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 248 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 249 | /* 0x000B, * LINE TABULATION */ |
| 250 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 251 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 252 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 253 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 254 | /* 0x001C, * FILE SEPARATOR */ |
| 255 | /* 0x001D, * GROUP SEPARATOR */ |
| 256 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 257 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 258 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 259 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 260 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 261 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 262 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 263 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 264 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 265 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 266 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 267 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 268 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 269 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 270 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 273 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 274 | 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] | 275 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 276 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 277 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 278 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 279 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 280 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 281 | /* This is actually an illegal character, so it should |
| 282 | not be passed to unichr. */ |
| 283 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 284 | #endif |
| 285 | } |
| 286 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 287 | #ifdef Py_DEBUG |
| 288 | static int |
| 289 | _PyUnicode_CheckConsistency(void *op) |
| 290 | { |
| 291 | PyASCIIObject *ascii; |
| 292 | unsigned int kind; |
| 293 | |
| 294 | assert(PyUnicode_Check(op)); |
| 295 | |
| 296 | ascii = (PyASCIIObject *)op; |
| 297 | kind = ascii->state.kind; |
| 298 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 299 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 300 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 301 | assert(ascii->state.ready == 1); |
| 302 | } |
| 303 | else if (ascii->state.compact == 1) { |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 304 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 305 | void *data; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 306 | assert(kind == PyUnicode_1BYTE_KIND |
| 307 | || kind == PyUnicode_2BYTE_KIND |
| 308 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 309 | assert(ascii->state.ascii == 0); |
| 310 | assert(ascii->state.ready == 1); |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 311 | data = compact + 1; |
| 312 | assert (compact->utf8 != data); |
| 313 | if ( |
| 314 | #if SIZEOF_WCHAR_T == 2 |
| 315 | kind == PyUnicode_2BYTE_KIND |
| 316 | #else |
| 317 | kind == PyUnicode_4BYTE_KIND |
| 318 | #endif |
| 319 | ) |
| 320 | assert(ascii->wstr == data); |
| 321 | else |
| 322 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 323 | } else { |
| 324 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 325 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 326 | |
| 327 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 328 | assert(ascii->state.compact == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 329 | assert(ascii->state.ascii == 0); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 330 | assert(ascii->state.ready == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 331 | assert(ascii->wstr != NULL); |
| 332 | assert(unicode->data.any == NULL); |
| 333 | assert(compact->utf8 == NULL); |
| 334 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
| 335 | } |
| 336 | else { |
| 337 | assert(kind == PyUnicode_1BYTE_KIND |
| 338 | || kind == PyUnicode_2BYTE_KIND |
| 339 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 340 | assert(ascii->state.compact == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 341 | assert(ascii->state.ready == 1); |
| 342 | assert(unicode->data.any != NULL); |
Victor Stinner | 85041a5 | 2011-10-03 14:42:39 +0200 | [diff] [blame] | 343 | if (ascii->state.ascii) |
| 344 | assert (compact->utf8 == unicode->data.any); |
| 345 | else |
| 346 | assert (compact->utf8 != unicode->data.any); |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 347 | if ( |
| 348 | #if SIZEOF_WCHAR_T == 2 |
| 349 | kind == PyUnicode_2BYTE_KIND |
| 350 | #else |
| 351 | kind == PyUnicode_4BYTE_KIND |
| 352 | #endif |
| 353 | ) |
| 354 | assert(ascii->wstr == unicode->data.any); |
| 355 | else |
| 356 | assert(ascii->wstr != unicode->data.any); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | return 1; |
| 360 | } |
| 361 | #endif |
| 362 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 363 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 364 | |
| 365 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 366 | to keep things simple, we use a single bitmask, using the least 5 |
| 367 | bits from each unicode characters as the bit index. */ |
| 368 | |
| 369 | /* the linebreak mask is set up by Unicode_Init below */ |
| 370 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 371 | #if LONG_BIT >= 128 |
| 372 | #define BLOOM_WIDTH 128 |
| 373 | #elif LONG_BIT >= 64 |
| 374 | #define BLOOM_WIDTH 64 |
| 375 | #elif LONG_BIT >= 32 |
| 376 | #define BLOOM_WIDTH 32 |
| 377 | #else |
| 378 | #error "LONG_BIT is smaller than 32" |
| 379 | #endif |
| 380 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 381 | #define BLOOM_MASK unsigned long |
| 382 | |
| 383 | static BLOOM_MASK bloom_linebreak; |
| 384 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 385 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 386 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 387 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 388 | #define BLOOM_LINEBREAK(ch) \ |
| 389 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 390 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 391 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 392 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 393 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 394 | { |
| 395 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 396 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 397 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 398 | Py_ssize_t i; |
| 399 | |
| 400 | mask = 0; |
| 401 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 402 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 403 | |
| 404 | return mask; |
| 405 | } |
| 406 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 407 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 408 | (BLOOM(mask, chr) \ |
| 409 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 410 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 411 | /* --- Unicode Object ----------------------------------------------------- */ |
| 412 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 413 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 414 | fixup(PyUnicodeObject *self, Py_UCS4 (*fixfct)(PyUnicodeObject *s)); |
| 415 | |
| 416 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 417 | Py_ssize_t size, Py_UCS4 ch, |
| 418 | int direction) |
| 419 | { |
| 420 | /* like wcschr, but doesn't stop at NULL characters */ |
| 421 | Py_ssize_t i; |
| 422 | if (direction == 1) { |
| 423 | for(i = 0; i < size; i++) |
| 424 | if (PyUnicode_READ(kind, s, i) == ch) |
| 425 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 426 | } |
| 427 | else { |
| 428 | for(i = size-1; i >= 0; i--) |
| 429 | if (PyUnicode_READ(kind, s, i) == ch) |
| 430 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 431 | } |
| 432 | return NULL; |
| 433 | } |
| 434 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 435 | static PyObject* |
| 436 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 437 | { |
| 438 | Py_ssize_t char_size; |
| 439 | Py_ssize_t struct_size; |
| 440 | Py_ssize_t new_size; |
| 441 | int share_wstr; |
| 442 | |
| 443 | assert(PyUnicode_IS_READY(unicode)); |
| 444 | char_size = PyUnicode_CHARACTER_SIZE(unicode); |
| 445 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 446 | struct_size = sizeof(PyASCIIObject); |
| 447 | else |
| 448 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 449 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 450 | |
| 451 | _Py_DEC_REFTOTAL; |
| 452 | _Py_ForgetReference(unicode); |
| 453 | |
| 454 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 455 | PyErr_NoMemory(); |
| 456 | return NULL; |
| 457 | } |
| 458 | new_size = (struct_size + (length + 1) * char_size); |
| 459 | |
| 460 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 461 | if (unicode == NULL) { |
| 462 | PyObject_Del(unicode); |
| 463 | PyErr_NoMemory(); |
| 464 | return NULL; |
| 465 | } |
| 466 | _Py_NewReference(unicode); |
| 467 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 468 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 469 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 470 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 471 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 472 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 473 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 474 | length, 0); |
| 475 | return unicode; |
| 476 | } |
| 477 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 478 | static int |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame^] | 479 | resize_inplace(PyUnicodeObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 480 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame^] | 481 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 482 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 483 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 484 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame^] | 485 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 486 | |
| 487 | if (PyUnicode_IS_READY(unicode)) { |
| 488 | Py_ssize_t char_size; |
| 489 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 490 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 491 | void *data; |
| 492 | |
| 493 | data = _PyUnicode_DATA_ANY(unicode); |
| 494 | assert(data != NULL); |
| 495 | char_size = PyUnicode_CHARACTER_SIZE(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 496 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 497 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame^] | 498 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 499 | { |
| 500 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 501 | _PyUnicode_UTF8(unicode) = NULL; |
| 502 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 503 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 504 | |
| 505 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 506 | PyErr_NoMemory(); |
| 507 | return -1; |
| 508 | } |
| 509 | new_size = (length + 1) * char_size; |
| 510 | |
| 511 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 512 | if (data == NULL) { |
| 513 | PyErr_NoMemory(); |
| 514 | return -1; |
| 515 | } |
| 516 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 517 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 518 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 519 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 520 | } |
| 521 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 522 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 523 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 524 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 525 | _PyUnicode_LENGTH(unicode) = length; |
| 526 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame^] | 527 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
| 528 | _PyUnicode_CHECK(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 529 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 530 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 531 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame^] | 532 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 533 | |
| 534 | /* check for integer overflow */ |
| 535 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 536 | PyErr_NoMemory(); |
| 537 | return -1; |
| 538 | } |
| 539 | wstr = _PyUnicode_WSTR(unicode); |
| 540 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 541 | if (!wstr) { |
| 542 | PyErr_NoMemory(); |
| 543 | return -1; |
| 544 | } |
| 545 | _PyUnicode_WSTR(unicode) = wstr; |
| 546 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 547 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 548 | _PyUnicode_CHECK(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 552 | static PyObject* |
| 553 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 554 | { |
| 555 | Py_ssize_t copy_length; |
| 556 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 557 | PyObject *copy; |
| 558 | assert(PyUnicode_IS_READY(unicode)); |
| 559 | |
| 560 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 561 | if (copy == NULL) |
| 562 | return NULL; |
| 563 | |
| 564 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
| 565 | if (PyUnicode_CopyCharacters(copy, 0, |
| 566 | unicode, 0, |
| 567 | copy_length) < 0) |
| 568 | { |
| 569 | Py_DECREF(copy); |
| 570 | return NULL; |
| 571 | } |
| 572 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 573 | } |
| 574 | else { |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 575 | PyUnicodeObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 576 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 577 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 578 | w = _PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 579 | if (w == NULL) |
| 580 | return NULL; |
| 581 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 582 | copy_length = Py_MIN(copy_length, length); |
| 583 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 584 | copy_length); |
| 585 | return (PyObject*)w; |
| 586 | } |
| 587 | } |
| 588 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 589 | /* 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] | 590 | Ux0000 terminated; some code (e.g. new_identifier) |
| 591 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 592 | |
| 593 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 594 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 595 | |
| 596 | */ |
| 597 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 598 | #ifdef Py_DEBUG |
| 599 | int unicode_old_new_calls = 0; |
| 600 | #endif |
| 601 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 602 | static PyUnicodeObject * |
| 603 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 604 | { |
| 605 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 606 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 607 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 608 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 609 | if (length == 0 && unicode_empty != NULL) { |
| 610 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 611 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 614 | /* Ensure we won't overflow the size. */ |
| 615 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 616 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 617 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 618 | if (length < 0) { |
| 619 | PyErr_SetString(PyExc_SystemError, |
| 620 | "Negative size passed to _PyUnicode_New"); |
| 621 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 624 | #ifdef Py_DEBUG |
| 625 | ++unicode_old_new_calls; |
| 626 | #endif |
| 627 | |
| 628 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 629 | if (unicode == NULL) |
| 630 | return NULL; |
| 631 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 632 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 633 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 634 | PyErr_NoMemory(); |
| 635 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 636 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 637 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 638 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 639 | * the caller fails before initializing str -- unicode_resize() |
| 640 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 641 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 642 | * We don't want unicode_resize to read uninitialized memory in |
| 643 | * that case. |
| 644 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 645 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 646 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 647 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 648 | _PyUnicode_HASH(unicode) = -1; |
| 649 | _PyUnicode_STATE(unicode).interned = 0; |
| 650 | _PyUnicode_STATE(unicode).kind = 0; |
| 651 | _PyUnicode_STATE(unicode).compact = 0; |
| 652 | _PyUnicode_STATE(unicode).ready = 0; |
| 653 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 654 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 655 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 656 | _PyUnicode_UTF8(unicode) = NULL; |
| 657 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 658 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 659 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 660 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 661 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 662 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 663 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 664 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 665 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 668 | static const char* |
| 669 | unicode_kind_name(PyObject *unicode) |
| 670 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 671 | /* don't check consistency: unicode_kind_name() is called from |
| 672 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 673 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 674 | { |
| 675 | if (!PyUnicode_IS_READY(unicode)) |
| 676 | return "wstr"; |
| 677 | switch(PyUnicode_KIND(unicode)) |
| 678 | { |
| 679 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 680 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 681 | return "legacy ascii"; |
| 682 | else |
| 683 | return "legacy latin1"; |
| 684 | case PyUnicode_2BYTE_KIND: |
| 685 | return "legacy UCS2"; |
| 686 | case PyUnicode_4BYTE_KIND: |
| 687 | return "legacy UCS4"; |
| 688 | default: |
| 689 | return "<legacy invalid kind>"; |
| 690 | } |
| 691 | } |
| 692 | assert(PyUnicode_IS_READY(unicode)); |
| 693 | switch(PyUnicode_KIND(unicode)) |
| 694 | { |
| 695 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 696 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 697 | return "ascii"; |
| 698 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 699 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 700 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 701 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 702 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 703 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 704 | default: |
| 705 | return "<invalid compact kind>"; |
| 706 | } |
| 707 | } |
| 708 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 709 | #ifdef Py_DEBUG |
| 710 | int unicode_new_new_calls = 0; |
| 711 | |
| 712 | /* Functions wrapping macros for use in debugger */ |
| 713 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 714 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | void *_PyUnicode_compact_data(void *unicode) { |
| 718 | return _PyUnicode_COMPACT_DATA(unicode); |
| 719 | } |
| 720 | void *_PyUnicode_data(void *unicode){ |
| 721 | printf("obj %p\n", unicode); |
| 722 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 723 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 724 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 725 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 726 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 727 | return PyUnicode_DATA(unicode); |
| 728 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 729 | |
| 730 | void |
| 731 | _PyUnicode_Dump(PyObject *op) |
| 732 | { |
| 733 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 734 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 735 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 736 | void *data; |
| 737 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 738 | if (ascii->state.compact) |
| 739 | data = (compact + 1); |
| 740 | else |
| 741 | data = unicode->data.any; |
| 742 | if (ascii->wstr == data) |
| 743 | printf("shared "); |
| 744 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 745 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 746 | printf(" (%zu), ", compact->wstr_length); |
| 747 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 748 | printf("shared "); |
| 749 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 750 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 751 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 752 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 753 | #endif |
| 754 | |
| 755 | PyObject * |
| 756 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 757 | { |
| 758 | PyObject *obj; |
| 759 | PyCompactUnicodeObject *unicode; |
| 760 | void *data; |
| 761 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 762 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 763 | Py_ssize_t char_size; |
| 764 | Py_ssize_t struct_size; |
| 765 | |
| 766 | /* Optimization for empty strings */ |
| 767 | if (size == 0 && unicode_empty != NULL) { |
| 768 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 769 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | #ifdef Py_DEBUG |
| 773 | ++unicode_new_new_calls; |
| 774 | #endif |
| 775 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 776 | is_ascii = 0; |
| 777 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 778 | struct_size = sizeof(PyCompactUnicodeObject); |
| 779 | if (maxchar < 128) { |
| 780 | kind_state = PyUnicode_1BYTE_KIND; |
| 781 | char_size = 1; |
| 782 | is_ascii = 1; |
| 783 | struct_size = sizeof(PyASCIIObject); |
| 784 | } |
| 785 | else if (maxchar < 256) { |
| 786 | kind_state = PyUnicode_1BYTE_KIND; |
| 787 | char_size = 1; |
| 788 | } |
| 789 | else if (maxchar < 65536) { |
| 790 | kind_state = PyUnicode_2BYTE_KIND; |
| 791 | char_size = 2; |
| 792 | if (sizeof(wchar_t) == 2) |
| 793 | is_sharing = 1; |
| 794 | } |
| 795 | else { |
| 796 | kind_state = PyUnicode_4BYTE_KIND; |
| 797 | char_size = 4; |
| 798 | if (sizeof(wchar_t) == 4) |
| 799 | is_sharing = 1; |
| 800 | } |
| 801 | |
| 802 | /* Ensure we won't overflow the size. */ |
| 803 | if (size < 0) { |
| 804 | PyErr_SetString(PyExc_SystemError, |
| 805 | "Negative size passed to PyUnicode_New"); |
| 806 | return NULL; |
| 807 | } |
| 808 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 809 | return PyErr_NoMemory(); |
| 810 | |
| 811 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 812 | * PyObject_New() so we are able to allocate space for the object and |
| 813 | * it's data buffer. |
| 814 | */ |
| 815 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 816 | if (obj == NULL) |
| 817 | return PyErr_NoMemory(); |
| 818 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 819 | if (obj == NULL) |
| 820 | return NULL; |
| 821 | |
| 822 | unicode = (PyCompactUnicodeObject *)obj; |
| 823 | if (is_ascii) |
| 824 | data = ((PyASCIIObject*)obj) + 1; |
| 825 | else |
| 826 | data = unicode + 1; |
| 827 | _PyUnicode_LENGTH(unicode) = size; |
| 828 | _PyUnicode_HASH(unicode) = -1; |
| 829 | _PyUnicode_STATE(unicode).interned = 0; |
| 830 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 831 | _PyUnicode_STATE(unicode).compact = 1; |
| 832 | _PyUnicode_STATE(unicode).ready = 1; |
| 833 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 834 | if (is_ascii) { |
| 835 | ((char*)data)[size] = 0; |
| 836 | _PyUnicode_WSTR(unicode) = NULL; |
| 837 | } |
| 838 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 839 | ((char*)data)[size] = 0; |
| 840 | _PyUnicode_WSTR(unicode) = NULL; |
| 841 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 842 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 843 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 844 | } |
| 845 | else { |
| 846 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 847 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 848 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 849 | ((Py_UCS2*)data)[size] = 0; |
| 850 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 851 | ((Py_UCS4*)data)[size] = 0; |
| 852 | if (is_sharing) { |
| 853 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 854 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 855 | } |
| 856 | else { |
| 857 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 858 | _PyUnicode_WSTR(unicode) = NULL; |
| 859 | } |
| 860 | } |
| 861 | return obj; |
| 862 | } |
| 863 | |
| 864 | #if SIZEOF_WCHAR_T == 2 |
| 865 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 866 | will decode surrogate pairs, the other conversions are implemented as macros |
| 867 | for efficency. |
| 868 | |
| 869 | This function assumes that unicode can hold one more code point than wstr |
| 870 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 871 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 872 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 873 | PyUnicodeObject *unicode) |
| 874 | { |
| 875 | const wchar_t *iter; |
| 876 | Py_UCS4 *ucs4_out; |
| 877 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 878 | assert(unicode != NULL); |
| 879 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 880 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 881 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 882 | |
| 883 | for (iter = begin; iter < end; ) { |
| 884 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 885 | _PyUnicode_GET_LENGTH(unicode))); |
| 886 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 887 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 888 | { |
| 889 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 890 | iter += 2; |
| 891 | } |
| 892 | else { |
| 893 | *ucs4_out++ = *iter; |
| 894 | iter++; |
| 895 | } |
| 896 | } |
| 897 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 898 | _PyUnicode_GET_LENGTH(unicode))); |
| 899 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 900 | } |
| 901 | #endif |
| 902 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 903 | static int |
| 904 | _PyUnicode_Dirty(PyObject *unicode) |
| 905 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 906 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 907 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 908 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 909 | "Cannot modify a string having more than 1 reference"); |
| 910 | return -1; |
| 911 | } |
| 912 | _PyUnicode_DIRTY(unicode); |
| 913 | return 0; |
| 914 | } |
| 915 | |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 916 | Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 917 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 918 | PyObject *from, Py_ssize_t from_start, |
| 919 | Py_ssize_t how_many) |
| 920 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 921 | unsigned int from_kind, to_kind; |
| 922 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 923 | |
Victor Stinner | b153615 | 2011-09-30 02:26:10 +0200 | [diff] [blame] | 924 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 925 | PyErr_BadInternalCall(); |
| 926 | return -1; |
| 927 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 928 | |
| 929 | if (PyUnicode_READY(from)) |
| 930 | return -1; |
| 931 | if (PyUnicode_READY(to)) |
| 932 | return -1; |
| 933 | |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 934 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 935 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 936 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 937 | "Cannot write %zi characters at %zi " |
| 938 | "in a string of %zi characters", |
| 939 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 940 | return -1; |
| 941 | } |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 942 | if (how_many == 0) |
| 943 | return 0; |
| 944 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 945 | if (_PyUnicode_Dirty(to)) |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 946 | return -1; |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 947 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 948 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 949 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 950 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 951 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 952 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 953 | if (from_kind == to_kind |
| 954 | /* deny latin1 => ascii */ |
| 955 | && PyUnicode_MAX_CHAR_VALUE(to) >= PyUnicode_MAX_CHAR_VALUE(from)) |
| 956 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 957 | Py_MEMCPY((char*)to_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 958 | + PyUnicode_KIND_SIZE(to_kind, to_start), |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 959 | (char*)from_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 960 | + PyUnicode_KIND_SIZE(from_kind, from_start), |
| 961 | PyUnicode_KIND_SIZE(to_kind, how_many)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 962 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 963 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 964 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 965 | { |
| 966 | _PyUnicode_CONVERT_BYTES( |
| 967 | Py_UCS1, Py_UCS2, |
| 968 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 969 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 970 | PyUnicode_2BYTE_DATA(to) + to_start |
| 971 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 972 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 973 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 974 | && to_kind == PyUnicode_4BYTE_KIND) |
| 975 | { |
| 976 | _PyUnicode_CONVERT_BYTES( |
| 977 | Py_UCS1, Py_UCS4, |
| 978 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 979 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 980 | PyUnicode_4BYTE_DATA(to) + to_start |
| 981 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 982 | } |
| 983 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 984 | && to_kind == PyUnicode_4BYTE_KIND) |
| 985 | { |
| 986 | _PyUnicode_CONVERT_BYTES( |
| 987 | Py_UCS2, Py_UCS4, |
| 988 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 989 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 990 | PyUnicode_4BYTE_DATA(to) + to_start |
| 991 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 992 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 993 | else { |
| 994 | int invalid_kinds; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 995 | |
| 996 | /* check if max_char(from substring) <= max_char(to) */ |
| 997 | if (from_kind > to_kind |
| 998 | /* latin1 => ascii */ |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 999 | || (PyUnicode_IS_ASCII(to) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1000 | && to_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1001 | && !PyUnicode_IS_ASCII(from))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1002 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1003 | /* slow path to check for character overflow */ |
| 1004 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1005 | Py_UCS4 ch, maxchar; |
| 1006 | Py_ssize_t i; |
| 1007 | |
| 1008 | maxchar = 0; |
| 1009 | invalid_kinds = 0; |
| 1010 | for (i=0; i < how_many; i++) { |
| 1011 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1012 | if (ch > maxchar) { |
| 1013 | maxchar = ch; |
| 1014 | if (maxchar > to_maxchar) { |
| 1015 | invalid_kinds = 1; |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1020 | } |
| 1021 | } |
| 1022 | else |
| 1023 | invalid_kinds = 1; |
| 1024 | if (invalid_kinds) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1025 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1026 | "Cannot copy %s characters " |
| 1027 | "into a string of %s characters", |
| 1028 | unicode_kind_name(from), |
| 1029 | unicode_kind_name(to)); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1030 | return -1; |
| 1031 | } |
| 1032 | } |
| 1033 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1036 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1037 | correct string length can be computed before converting a string to UCS4. |
| 1038 | This function counts single surrogates as a character and not as a pair. |
| 1039 | |
| 1040 | Return 0 on success, or -1 on error. */ |
| 1041 | static int |
| 1042 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1043 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1044 | { |
| 1045 | const wchar_t *iter; |
| 1046 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1047 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | if (num_surrogates == NULL || maxchar == NULL) { |
| 1049 | PyErr_SetString(PyExc_SystemError, |
| 1050 | "unexpected NULL arguments to " |
| 1051 | "PyUnicode_FindMaxCharAndNumSurrogatePairs"); |
| 1052 | return -1; |
| 1053 | } |
| 1054 | |
| 1055 | *num_surrogates = 0; |
| 1056 | *maxchar = 0; |
| 1057 | |
| 1058 | for (iter = begin; iter < end; ) { |
| 1059 | if (*iter > *maxchar) |
| 1060 | *maxchar = *iter; |
| 1061 | #if SIZEOF_WCHAR_T == 2 |
| 1062 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1063 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1064 | { |
| 1065 | Py_UCS4 surrogate_val; |
| 1066 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1067 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1068 | ++(*num_surrogates); |
| 1069 | if (surrogate_val > *maxchar) |
| 1070 | *maxchar = surrogate_val; |
| 1071 | iter += 2; |
| 1072 | } |
| 1073 | else |
| 1074 | iter++; |
| 1075 | #else |
| 1076 | iter++; |
| 1077 | #endif |
| 1078 | } |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | #ifdef Py_DEBUG |
| 1083 | int unicode_ready_calls = 0; |
| 1084 | #endif |
| 1085 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1086 | static int |
| 1087 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1088 | { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1089 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1090 | wchar_t *end; |
| 1091 | Py_UCS4 maxchar = 0; |
| 1092 | Py_ssize_t num_surrogates; |
| 1093 | #if SIZEOF_WCHAR_T == 2 |
| 1094 | Py_ssize_t length_wo_surrogates; |
| 1095 | #endif |
| 1096 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1097 | assert(p_obj != NULL); |
| 1098 | unicode = (PyUnicodeObject *)*p_obj; |
| 1099 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1100 | /* _PyUnicode_Ready() is only intented for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1101 | strings were created using _PyObject_New() and where no canonical |
| 1102 | representation (the str field) has been set yet aka strings |
| 1103 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1104 | assert(_PyUnicode_CHECK(unicode)); |
| 1105 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1106 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1107 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1108 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1109 | /* Actually, it should neither be interned nor be anything else: */ |
| 1110 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1111 | |
| 1112 | #ifdef Py_DEBUG |
| 1113 | ++unicode_ready_calls; |
| 1114 | #endif |
| 1115 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1116 | #ifdef Py_DEBUG |
| 1117 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1118 | #else |
| 1119 | if (replace && Py_REFCNT(unicode) != 1) |
| 1120 | replace = 0; |
| 1121 | #endif |
| 1122 | if (replace) { |
| 1123 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1124 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1125 | /* Optimization for empty strings */ |
| 1126 | if (len == 0) { |
| 1127 | Py_INCREF(unicode_empty); |
| 1128 | Py_DECREF(*p_obj); |
| 1129 | *p_obj = unicode_empty; |
| 1130 | return 0; |
| 1131 | } |
| 1132 | if (len == 1 && wstr[0] < 256) { |
| 1133 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1134 | if (latin1_char == NULL) |
| 1135 | return -1; |
| 1136 | Py_DECREF(*p_obj); |
| 1137 | *p_obj = latin1_char; |
| 1138 | return 0; |
| 1139 | } |
| 1140 | } |
| 1141 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1142 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1143 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1144 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1145 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | |
| 1147 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1148 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1149 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1150 | PyErr_NoMemory(); |
| 1151 | return -1; |
| 1152 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1153 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1154 | _PyUnicode_WSTR(unicode), end, |
| 1155 | PyUnicode_1BYTE_DATA(unicode)); |
| 1156 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1157 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1158 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1159 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1160 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1161 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1162 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1163 | } |
| 1164 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1165 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1166 | _PyUnicode_UTF8(unicode) = NULL; |
| 1167 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1168 | } |
| 1169 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1170 | _PyUnicode_WSTR(unicode) = NULL; |
| 1171 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1172 | } |
| 1173 | /* In this case we might have to convert down from 4-byte native |
| 1174 | wchar_t to 2-byte unicode. */ |
| 1175 | else if (maxchar < 65536) { |
| 1176 | assert(num_surrogates == 0 && |
| 1177 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1178 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1179 | #if SIZEOF_WCHAR_T == 2 |
| 1180 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1181 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1182 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1183 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1184 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1185 | _PyUnicode_UTF8(unicode) = NULL; |
| 1186 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1187 | #else |
| 1188 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1189 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1190 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1191 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1192 | PyErr_NoMemory(); |
| 1193 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1194 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1195 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1196 | _PyUnicode_WSTR(unicode), end, |
| 1197 | PyUnicode_2BYTE_DATA(unicode)); |
| 1198 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1199 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1200 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1201 | _PyUnicode_UTF8(unicode) = NULL; |
| 1202 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1203 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1204 | _PyUnicode_WSTR(unicode) = NULL; |
| 1205 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1206 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1207 | } |
| 1208 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1209 | else { |
| 1210 | #if SIZEOF_WCHAR_T == 2 |
| 1211 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1212 | new normalized 4-byte version. */ |
| 1213 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1214 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1215 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1216 | PyErr_NoMemory(); |
| 1217 | return -1; |
| 1218 | } |
| 1219 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1220 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1221 | _PyUnicode_UTF8(unicode) = NULL; |
| 1222 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1223 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1224 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1225 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1226 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1227 | _PyUnicode_WSTR(unicode) = NULL; |
| 1228 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1229 | #else |
| 1230 | assert(num_surrogates == 0); |
| 1231 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1232 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1233 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1234 | _PyUnicode_UTF8(unicode) = NULL; |
| 1235 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1236 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1237 | #endif |
| 1238 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1239 | } |
| 1240 | _PyUnicode_STATE(unicode).ready = 1; |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1244 | int |
| 1245 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1246 | { |
| 1247 | return unicode_ready(op, 1); |
| 1248 | } |
| 1249 | |
| 1250 | int |
| 1251 | _PyUnicode_Ready(PyObject *op) |
| 1252 | { |
| 1253 | return unicode_ready(&op, 0); |
| 1254 | } |
| 1255 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1256 | static void |
| 1257 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1258 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1259 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1260 | case SSTATE_NOT_INTERNED: |
| 1261 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1262 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1263 | case SSTATE_INTERNED_MORTAL: |
| 1264 | /* revive dead object temporarily for DelItem */ |
| 1265 | Py_REFCNT(unicode) = 3; |
| 1266 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 1267 | Py_FatalError( |
| 1268 | "deletion of interned string failed"); |
| 1269 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1270 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1271 | case SSTATE_INTERNED_IMMORTAL: |
| 1272 | Py_FatalError("Immortal interned string died."); |
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 | default: |
| 1275 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1278 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1279 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1280 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1281 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1282 | |
| 1283 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1284 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1285 | } |
| 1286 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1287 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1288 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1289 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1293 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1294 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1295 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1296 | if (Py_REFCNT(unicode) != 1) |
| 1297 | return 0; |
| 1298 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1299 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1300 | assert (unicode != unicode_empty); |
| 1301 | #ifdef Py_DEBUG |
| 1302 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND |
| 1303 | && PyUnicode_GET_LENGTH(unicode) == 1) |
| 1304 | { |
| 1305 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1306 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1307 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1308 | } |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1309 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1310 | return 1; |
| 1311 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1312 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1313 | static int |
| 1314 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1315 | { |
| 1316 | PyObject *unicode; |
| 1317 | Py_ssize_t old_length; |
| 1318 | |
| 1319 | assert(p_unicode != NULL); |
| 1320 | unicode = *p_unicode; |
| 1321 | |
| 1322 | assert(unicode != NULL); |
| 1323 | assert(PyUnicode_Check(unicode)); |
| 1324 | assert(0 <= length); |
| 1325 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1326 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1327 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1328 | else |
| 1329 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1330 | if (old_length == length) |
| 1331 | return 0; |
| 1332 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1333 | if (!unicode_resizable(unicode)) { |
| 1334 | PyObject *copy = resize_copy(unicode, length); |
| 1335 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1336 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1337 | Py_DECREF(*p_unicode); |
| 1338 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1339 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1342 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1343 | *p_unicode = resize_compact(unicode, length); |
| 1344 | if (*p_unicode == NULL) |
| 1345 | return -1; |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame^] | 1346 | _PyUnicode_CHECK(*p_unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1347 | return 0; |
| 1348 | } else |
| 1349 | return resize_inplace((PyUnicodeObject*)unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1352 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1353 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1354 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1355 | PyObject *unicode; |
| 1356 | if (p_unicode == NULL) { |
| 1357 | PyErr_BadInternalCall(); |
| 1358 | return -1; |
| 1359 | } |
| 1360 | unicode = *p_unicode; |
| 1361 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 |
| 1362 | || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) |
| 1363 | { |
| 1364 | PyErr_BadInternalCall(); |
| 1365 | return -1; |
| 1366 | } |
| 1367 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1368 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1369 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1370 | static PyObject* |
| 1371 | get_latin1_char(unsigned char ch) |
| 1372 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1373 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1374 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1375 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1376 | if (!unicode) |
| 1377 | return NULL; |
| 1378 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
| 1379 | unicode_latin1[ch] = unicode; |
| 1380 | } |
| 1381 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1382 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1383 | } |
| 1384 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1385 | PyObject * |
| 1386 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1387 | { |
| 1388 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1389 | Py_UCS4 maxchar = 0; |
| 1390 | Py_ssize_t num_surrogates; |
| 1391 | |
| 1392 | if (u == NULL) |
| 1393 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1394 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1395 | /* If the Unicode data is known at construction time, we can apply |
| 1396 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1397 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1398 | /* Optimization for empty strings */ |
| 1399 | if (size == 0 && unicode_empty != NULL) { |
| 1400 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1401 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1402 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1403 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1404 | /* Single character Unicode objects in the Latin-1 range are |
| 1405 | shared when using this constructor */ |
| 1406 | if (size == 1 && *u < 256) |
| 1407 | return get_latin1_char((unsigned char)*u); |
| 1408 | |
| 1409 | /* If not empty and not single character, copy the Unicode data |
| 1410 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1411 | if (find_maxchar_surrogates(u, u + size, |
| 1412 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1413 | return NULL; |
| 1414 | |
| 1415 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1416 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1417 | if (!unicode) |
| 1418 | return NULL; |
| 1419 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1420 | switch (PyUnicode_KIND(unicode)) { |
| 1421 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1422 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1423 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1424 | break; |
| 1425 | case PyUnicode_2BYTE_KIND: |
| 1426 | #if Py_UNICODE_SIZE == 2 |
| 1427 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1428 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1429 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1430 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1431 | #endif |
| 1432 | break; |
| 1433 | case PyUnicode_4BYTE_KIND: |
| 1434 | #if SIZEOF_WCHAR_T == 2 |
| 1435 | /* This is the only case which has to process surrogates, thus |
| 1436 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1437 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1438 | #else |
| 1439 | assert(num_surrogates == 0); |
| 1440 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1441 | #endif |
| 1442 | break; |
| 1443 | default: |
| 1444 | assert(0 && "Impossible state"); |
| 1445 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1446 | |
| 1447 | return (PyObject *)unicode; |
| 1448 | } |
| 1449 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1450 | PyObject * |
| 1451 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1452 | { |
| 1453 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1454 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1455 | if (size < 0) { |
| 1456 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1457 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1458 | return NULL; |
| 1459 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1460 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1461 | /* 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] | 1462 | some optimizations which share commonly used objects. |
| 1463 | Also, this means the input must be UTF-8, so fall back to the |
| 1464 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1465 | if (u != NULL) { |
| 1466 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1467 | /* Optimization for empty strings */ |
| 1468 | if (size == 0 && unicode_empty != NULL) { |
| 1469 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1470 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1471 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1472 | |
| 1473 | /* Single characters are shared when using this constructor. |
| 1474 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1475 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1476 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1477 | |
| 1478 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1481 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1482 | if (!unicode) |
| 1483 | return NULL; |
| 1484 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1485 | return (PyObject *)unicode; |
| 1486 | } |
| 1487 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1488 | PyObject * |
| 1489 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1490 | { |
| 1491 | size_t size = strlen(u); |
| 1492 | if (size > PY_SSIZE_T_MAX) { |
| 1493 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1494 | return NULL; |
| 1495 | } |
| 1496 | |
| 1497 | return PyUnicode_FromStringAndSize(u, size); |
| 1498 | } |
| 1499 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1500 | static PyObject* |
| 1501 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1502 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1503 | PyObject *res; |
| 1504 | unsigned char max = 127; |
| 1505 | Py_ssize_t i; |
| 1506 | for (i = 0; i < size; i++) { |
| 1507 | if (u[i] & 0x80) { |
| 1508 | max = 255; |
| 1509 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1512 | res = PyUnicode_New(size, max); |
| 1513 | if (!res) |
| 1514 | return NULL; |
| 1515 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
| 1516 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1519 | static PyObject* |
| 1520 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1521 | { |
| 1522 | PyObject *res; |
| 1523 | Py_UCS2 max = 0; |
| 1524 | Py_ssize_t i; |
| 1525 | for (i = 0; i < size; i++) |
| 1526 | if (u[i] > max) |
| 1527 | max = u[i]; |
| 1528 | res = PyUnicode_New(size, max); |
| 1529 | if (!res) |
| 1530 | return NULL; |
| 1531 | if (max >= 256) |
| 1532 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1533 | else |
| 1534 | for (i = 0; i < size; i++) |
| 1535 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
| 1536 | return res; |
| 1537 | } |
| 1538 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1539 | static PyObject* |
| 1540 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1541 | { |
| 1542 | PyObject *res; |
| 1543 | Py_UCS4 max = 0; |
| 1544 | Py_ssize_t i; |
| 1545 | for (i = 0; i < size; i++) |
| 1546 | if (u[i] > max) |
| 1547 | max = u[i]; |
| 1548 | res = PyUnicode_New(size, max); |
| 1549 | if (!res) |
| 1550 | return NULL; |
| 1551 | if (max >= 0x10000) |
| 1552 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1553 | else { |
| 1554 | int kind = PyUnicode_KIND(res); |
| 1555 | void *data = PyUnicode_DATA(res); |
| 1556 | for (i = 0; i < size; i++) |
| 1557 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1558 | } |
| 1559 | return res; |
| 1560 | } |
| 1561 | |
| 1562 | PyObject* |
| 1563 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1564 | { |
| 1565 | switch(kind) { |
| 1566 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1567 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1568 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1569 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1570 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1571 | return _PyUnicode_FromUCS4(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1572 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1573 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1574 | return NULL; |
| 1575 | } |
| 1576 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1577 | PyObject* |
| 1578 | PyUnicode_Copy(PyObject *unicode) |
| 1579 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1580 | Py_ssize_t size; |
| 1581 | PyObject *copy; |
| 1582 | void *data; |
| 1583 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1584 | if (!PyUnicode_Check(unicode)) { |
| 1585 | PyErr_BadInternalCall(); |
| 1586 | return NULL; |
| 1587 | } |
| 1588 | if (PyUnicode_READY(unicode)) |
| 1589 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1590 | |
| 1591 | size = PyUnicode_GET_LENGTH(unicode); |
| 1592 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1593 | if (!copy) |
| 1594 | return NULL; |
| 1595 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1596 | |
| 1597 | data = PyUnicode_DATA(unicode); |
| 1598 | switch (PyUnicode_KIND(unicode)) |
| 1599 | { |
| 1600 | case PyUnicode_1BYTE_KIND: |
| 1601 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1602 | break; |
| 1603 | case PyUnicode_2BYTE_KIND: |
| 1604 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1605 | break; |
| 1606 | case PyUnicode_4BYTE_KIND: |
| 1607 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1608 | break; |
| 1609 | default: |
| 1610 | assert(0); |
| 1611 | break; |
| 1612 | } |
| 1613 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1614 | } |
| 1615 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1616 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1617 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1618 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1619 | |
| 1620 | void* |
| 1621 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1622 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1623 | Py_ssize_t len; |
| 1624 | void *result; |
| 1625 | unsigned int skind; |
| 1626 | |
| 1627 | if (PyUnicode_READY(s)) |
| 1628 | return NULL; |
| 1629 | |
| 1630 | len = PyUnicode_GET_LENGTH(s); |
| 1631 | skind = PyUnicode_KIND(s); |
| 1632 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1633 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1634 | return NULL; |
| 1635 | } |
| 1636 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1637 | case PyUnicode_2BYTE_KIND: |
| 1638 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1639 | if (!result) |
| 1640 | return PyErr_NoMemory(); |
| 1641 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1642 | _PyUnicode_CONVERT_BYTES( |
| 1643 | Py_UCS1, Py_UCS2, |
| 1644 | PyUnicode_1BYTE_DATA(s), |
| 1645 | PyUnicode_1BYTE_DATA(s) + len, |
| 1646 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1647 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1648 | case PyUnicode_4BYTE_KIND: |
| 1649 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1650 | if (!result) |
| 1651 | return PyErr_NoMemory(); |
| 1652 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1653 | _PyUnicode_CONVERT_BYTES( |
| 1654 | Py_UCS2, Py_UCS4, |
| 1655 | PyUnicode_2BYTE_DATA(s), |
| 1656 | PyUnicode_2BYTE_DATA(s) + len, |
| 1657 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1658 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1659 | else { |
| 1660 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1661 | _PyUnicode_CONVERT_BYTES( |
| 1662 | Py_UCS1, Py_UCS4, |
| 1663 | PyUnicode_1BYTE_DATA(s), |
| 1664 | PyUnicode_1BYTE_DATA(s) + len, |
| 1665 | result); |
| 1666 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1667 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1668 | default: |
| 1669 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1670 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1671 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1672 | return NULL; |
| 1673 | } |
| 1674 | |
| 1675 | static Py_UCS4* |
| 1676 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1677 | int copy_null) |
| 1678 | { |
| 1679 | int kind; |
| 1680 | void *data; |
| 1681 | Py_ssize_t len, targetlen; |
| 1682 | if (PyUnicode_READY(string) == -1) |
| 1683 | return NULL; |
| 1684 | kind = PyUnicode_KIND(string); |
| 1685 | data = PyUnicode_DATA(string); |
| 1686 | len = PyUnicode_GET_LENGTH(string); |
| 1687 | targetlen = len; |
| 1688 | if (copy_null) |
| 1689 | targetlen++; |
| 1690 | if (!target) { |
| 1691 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1692 | PyErr_NoMemory(); |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1696 | if (!target) { |
| 1697 | PyErr_NoMemory(); |
| 1698 | return NULL; |
| 1699 | } |
| 1700 | } |
| 1701 | else { |
| 1702 | if (targetsize < targetlen) { |
| 1703 | PyErr_Format(PyExc_SystemError, |
| 1704 | "string is longer than the buffer"); |
| 1705 | if (copy_null && 0 < targetsize) |
| 1706 | target[0] = 0; |
| 1707 | return NULL; |
| 1708 | } |
| 1709 | } |
| 1710 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1711 | Py_ssize_t i; |
| 1712 | for (i = 0; i < len; i++) |
| 1713 | target[i] = PyUnicode_READ(kind, data, i); |
| 1714 | } |
| 1715 | else |
| 1716 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1717 | if (copy_null) |
| 1718 | target[len] = 0; |
| 1719 | return target; |
| 1720 | } |
| 1721 | |
| 1722 | Py_UCS4* |
| 1723 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1724 | int copy_null) |
| 1725 | { |
| 1726 | if (target == NULL || targetsize < 1) { |
| 1727 | PyErr_BadInternalCall(); |
| 1728 | return NULL; |
| 1729 | } |
| 1730 | return as_ucs4(string, target, targetsize, copy_null); |
| 1731 | } |
| 1732 | |
| 1733 | Py_UCS4* |
| 1734 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1735 | { |
| 1736 | return as_ucs4(string, NULL, 0, 1); |
| 1737 | } |
| 1738 | |
| 1739 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1740 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1741 | PyObject * |
| 1742 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1743 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1744 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1745 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1746 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1747 | PyErr_BadInternalCall(); |
| 1748 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1751 | if (size == -1) { |
| 1752 | size = wcslen(w); |
| 1753 | } |
| 1754 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1755 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1758 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1759 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1760 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1761 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1762 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1763 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1764 | *fmt++ = '%'; |
| 1765 | if (width) { |
| 1766 | if (zeropad) |
| 1767 | *fmt++ = '0'; |
| 1768 | fmt += sprintf(fmt, "%d", width); |
| 1769 | } |
| 1770 | if (precision) |
| 1771 | fmt += sprintf(fmt, ".%d", precision); |
| 1772 | if (longflag) |
| 1773 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1774 | else if (longlongflag) { |
| 1775 | /* longlongflag should only ever be nonzero on machines with |
| 1776 | HAVE_LONG_LONG defined */ |
| 1777 | #ifdef HAVE_LONG_LONG |
| 1778 | char *f = PY_FORMAT_LONG_LONG; |
| 1779 | while (*f) |
| 1780 | *fmt++ = *f++; |
| 1781 | #else |
| 1782 | /* we shouldn't ever get here */ |
| 1783 | assert(0); |
| 1784 | *fmt++ = 'l'; |
| 1785 | #endif |
| 1786 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1787 | else if (size_tflag) { |
| 1788 | char *f = PY_FORMAT_SIZE_T; |
| 1789 | while (*f) |
| 1790 | *fmt++ = *f++; |
| 1791 | } |
| 1792 | *fmt++ = c; |
| 1793 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1796 | /* helper for PyUnicode_FromFormatV() */ |
| 1797 | |
| 1798 | static const char* |
| 1799 | parse_format_flags(const char *f, |
| 1800 | int *p_width, int *p_precision, |
| 1801 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 1802 | { |
| 1803 | int width, precision, longflag, longlongflag, size_tflag; |
| 1804 | |
| 1805 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 1806 | f++; |
| 1807 | width = 0; |
| 1808 | while (Py_ISDIGIT((unsigned)*f)) |
| 1809 | width = (width*10) + *f++ - '0'; |
| 1810 | precision = 0; |
| 1811 | if (*f == '.') { |
| 1812 | f++; |
| 1813 | while (Py_ISDIGIT((unsigned)*f)) |
| 1814 | precision = (precision*10) + *f++ - '0'; |
| 1815 | if (*f == '%') { |
| 1816 | /* "%.3%s" => f points to "3" */ |
| 1817 | f--; |
| 1818 | } |
| 1819 | } |
| 1820 | if (*f == '\0') { |
| 1821 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 1822 | f--; |
| 1823 | } |
| 1824 | if (p_width != NULL) |
| 1825 | *p_width = width; |
| 1826 | if (p_precision != NULL) |
| 1827 | *p_precision = precision; |
| 1828 | |
| 1829 | /* Handle %ld, %lu, %lld and %llu. */ |
| 1830 | longflag = 0; |
| 1831 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 1832 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1833 | |
| 1834 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1835 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1836 | longflag = 1; |
| 1837 | ++f; |
| 1838 | } |
| 1839 | #ifdef HAVE_LONG_LONG |
| 1840 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1841 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1842 | longlongflag = 1; |
| 1843 | f += 2; |
| 1844 | } |
| 1845 | #endif |
| 1846 | } |
| 1847 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1848 | 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] | 1849 | size_tflag = 1; |
| 1850 | ++f; |
| 1851 | } |
| 1852 | if (p_longflag != NULL) |
| 1853 | *p_longflag = longflag; |
| 1854 | if (p_longlongflag != NULL) |
| 1855 | *p_longlongflag = longlongflag; |
| 1856 | if (p_size_tflag != NULL) |
| 1857 | *p_size_tflag = size_tflag; |
| 1858 | return f; |
| 1859 | } |
| 1860 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1861 | /* maximum number of characters required for output of %ld. 21 characters |
| 1862 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 1863 | #define MAX_LONG_CHARS 21 |
| 1864 | /* maximum number of characters required for output of %lld. |
| 1865 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 1866 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 1867 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 1868 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1869 | PyObject * |
| 1870 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 1871 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1872 | va_list count; |
| 1873 | Py_ssize_t callcount = 0; |
| 1874 | PyObject **callresults = NULL; |
| 1875 | PyObject **callresult = NULL; |
| 1876 | Py_ssize_t n = 0; |
| 1877 | int width = 0; |
| 1878 | int precision = 0; |
| 1879 | int zeropad; |
| 1880 | const char* f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1881 | PyUnicodeObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1882 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1883 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1884 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 1885 | Py_UCS4 argmaxchar; |
| 1886 | Py_ssize_t numbersize = 0; |
| 1887 | char *numberresults = NULL; |
| 1888 | char *numberresult = NULL; |
| 1889 | Py_ssize_t i; |
| 1890 | int kind; |
| 1891 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1892 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1893 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1894 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 1895 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 1896 | * 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] | 1897 | * result in an array) |
| 1898 | * also esimate a upper bound for all the number formats in the string, |
| 1899 | * numbers will be formated in step 3 and be keept in a '\0'-separated |
| 1900 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1901 | for (f = format; *f; f++) { |
| 1902 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1903 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1904 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 1905 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 1906 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 1907 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1908 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1909 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1910 | #ifdef HAVE_LONG_LONG |
| 1911 | if (longlongflag) { |
| 1912 | if (width < MAX_LONG_LONG_CHARS) |
| 1913 | width = MAX_LONG_LONG_CHARS; |
| 1914 | } |
| 1915 | else |
| 1916 | #endif |
| 1917 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 1918 | including sign. Decimal takes the most space. This |
| 1919 | isn't enough for octal. If a width is specified we |
| 1920 | need more (which we allocate later). */ |
| 1921 | if (width < MAX_LONG_CHARS) |
| 1922 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1923 | |
| 1924 | /* account for the size + '\0' to separate numbers |
| 1925 | inside of the numberresults buffer */ |
| 1926 | numbersize += (width + 1); |
| 1927 | } |
| 1928 | } |
| 1929 | else if ((unsigned char)*f > 127) { |
| 1930 | PyErr_Format(PyExc_ValueError, |
| 1931 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 1932 | "string, got a non-ASCII byte: 0x%02x", |
| 1933 | (unsigned char)*f); |
| 1934 | return NULL; |
| 1935 | } |
| 1936 | } |
| 1937 | /* step 2: allocate memory for the results of |
| 1938 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 1939 | if (callcount) { |
| 1940 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 1941 | if (!callresults) { |
| 1942 | PyErr_NoMemory(); |
| 1943 | return NULL; |
| 1944 | } |
| 1945 | callresult = callresults; |
| 1946 | } |
| 1947 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 1948 | if (numbersize) { |
| 1949 | numberresults = PyObject_Malloc(numbersize); |
| 1950 | if (!numberresults) { |
| 1951 | PyErr_NoMemory(); |
| 1952 | goto fail; |
| 1953 | } |
| 1954 | numberresult = numberresults; |
| 1955 | } |
| 1956 | |
| 1957 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 1958 | for (f = format; *f; f++) { |
| 1959 | if (*f == '%') { |
| 1960 | const char* p; |
| 1961 | int longflag; |
| 1962 | int longlongflag; |
| 1963 | int size_tflag; |
| 1964 | int numprinted; |
| 1965 | |
| 1966 | p = f; |
| 1967 | zeropad = (f[1] == '0'); |
| 1968 | f = parse_format_flags(f, &width, &precision, |
| 1969 | &longflag, &longlongflag, &size_tflag); |
| 1970 | switch (*f) { |
| 1971 | case 'c': |
| 1972 | { |
| 1973 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1974 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1975 | n++; |
| 1976 | break; |
| 1977 | } |
| 1978 | case '%': |
| 1979 | n++; |
| 1980 | break; |
| 1981 | case 'i': |
| 1982 | case 'd': |
| 1983 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1984 | width, precision, *f); |
| 1985 | if (longflag) |
| 1986 | numprinted = sprintf(numberresult, fmt, |
| 1987 | va_arg(count, long)); |
| 1988 | #ifdef HAVE_LONG_LONG |
| 1989 | else if (longlongflag) |
| 1990 | numprinted = sprintf(numberresult, fmt, |
| 1991 | va_arg(count, PY_LONG_LONG)); |
| 1992 | #endif |
| 1993 | else if (size_tflag) |
| 1994 | numprinted = sprintf(numberresult, fmt, |
| 1995 | va_arg(count, Py_ssize_t)); |
| 1996 | else |
| 1997 | numprinted = sprintf(numberresult, fmt, |
| 1998 | va_arg(count, int)); |
| 1999 | n += numprinted; |
| 2000 | /* advance by +1 to skip over the '\0' */ |
| 2001 | numberresult += (numprinted + 1); |
| 2002 | assert(*(numberresult - 1) == '\0'); |
| 2003 | assert(*(numberresult - 2) != '\0'); |
| 2004 | assert(numprinted >= 0); |
| 2005 | assert(numberresult <= numberresults + numbersize); |
| 2006 | break; |
| 2007 | case 'u': |
| 2008 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2009 | width, precision, 'u'); |
| 2010 | if (longflag) |
| 2011 | numprinted = sprintf(numberresult, fmt, |
| 2012 | va_arg(count, unsigned long)); |
| 2013 | #ifdef HAVE_LONG_LONG |
| 2014 | else if (longlongflag) |
| 2015 | numprinted = sprintf(numberresult, fmt, |
| 2016 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2017 | #endif |
| 2018 | else if (size_tflag) |
| 2019 | numprinted = sprintf(numberresult, fmt, |
| 2020 | va_arg(count, size_t)); |
| 2021 | else |
| 2022 | numprinted = sprintf(numberresult, fmt, |
| 2023 | va_arg(count, unsigned int)); |
| 2024 | n += numprinted; |
| 2025 | numberresult += (numprinted + 1); |
| 2026 | assert(*(numberresult - 1) == '\0'); |
| 2027 | assert(*(numberresult - 2) != '\0'); |
| 2028 | assert(numprinted >= 0); |
| 2029 | assert(numberresult <= numberresults + numbersize); |
| 2030 | break; |
| 2031 | case 'x': |
| 2032 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2033 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2034 | n += numprinted; |
| 2035 | numberresult += (numprinted + 1); |
| 2036 | assert(*(numberresult - 1) == '\0'); |
| 2037 | assert(*(numberresult - 2) != '\0'); |
| 2038 | assert(numprinted >= 0); |
| 2039 | assert(numberresult <= numberresults + numbersize); |
| 2040 | break; |
| 2041 | case 'p': |
| 2042 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2043 | /* %p is ill-defined: ensure leading 0x. */ |
| 2044 | if (numberresult[1] == 'X') |
| 2045 | numberresult[1] = 'x'; |
| 2046 | else if (numberresult[1] != 'x') { |
| 2047 | memmove(numberresult + 2, numberresult, |
| 2048 | strlen(numberresult) + 1); |
| 2049 | numberresult[0] = '0'; |
| 2050 | numberresult[1] = 'x'; |
| 2051 | numprinted += 2; |
| 2052 | } |
| 2053 | n += numprinted; |
| 2054 | numberresult += (numprinted + 1); |
| 2055 | assert(*(numberresult - 1) == '\0'); |
| 2056 | assert(*(numberresult - 2) != '\0'); |
| 2057 | assert(numprinted >= 0); |
| 2058 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2059 | break; |
| 2060 | case 's': |
| 2061 | { |
| 2062 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2063 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2064 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2065 | if (!str) |
| 2066 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2067 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2068 | unicode objects, there is no need to call ready on them */ |
| 2069 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2070 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2071 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2072 | /* Remember the str and switch to the next slot */ |
| 2073 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2074 | break; |
| 2075 | } |
| 2076 | case 'U': |
| 2077 | { |
| 2078 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2079 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2080 | if (PyUnicode_READY(obj) == -1) |
| 2081 | goto fail; |
| 2082 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2083 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2084 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2085 | break; |
| 2086 | } |
| 2087 | case 'V': |
| 2088 | { |
| 2089 | PyObject *obj = va_arg(count, PyObject *); |
| 2090 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2091 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2092 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2093 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2094 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2095 | if (PyUnicode_READY(obj) == -1) |
| 2096 | goto fail; |
| 2097 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2098 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2099 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2100 | *callresult++ = NULL; |
| 2101 | } |
| 2102 | else { |
| 2103 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2104 | if (!str_obj) |
| 2105 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2106 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2107 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2108 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2109 | *callresult++ = str_obj; |
| 2110 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2111 | break; |
| 2112 | } |
| 2113 | case 'S': |
| 2114 | { |
| 2115 | PyObject *obj = va_arg(count, PyObject *); |
| 2116 | PyObject *str; |
| 2117 | assert(obj); |
| 2118 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2119 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2120 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2121 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2122 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2123 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2124 | /* Remember the str and switch to the next slot */ |
| 2125 | *callresult++ = str; |
| 2126 | break; |
| 2127 | } |
| 2128 | case 'R': |
| 2129 | { |
| 2130 | PyObject *obj = va_arg(count, PyObject *); |
| 2131 | PyObject *repr; |
| 2132 | assert(obj); |
| 2133 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2134 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2135 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2136 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2137 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2138 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2139 | /* Remember the repr and switch to the next slot */ |
| 2140 | *callresult++ = repr; |
| 2141 | break; |
| 2142 | } |
| 2143 | case 'A': |
| 2144 | { |
| 2145 | PyObject *obj = va_arg(count, PyObject *); |
| 2146 | PyObject *ascii; |
| 2147 | assert(obj); |
| 2148 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2149 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2150 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2151 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2152 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2153 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2154 | /* Remember the repr and switch to the next slot */ |
| 2155 | *callresult++ = ascii; |
| 2156 | break; |
| 2157 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2158 | default: |
| 2159 | /* if we stumble upon an unknown |
| 2160 | formatting code, copy the rest of |
| 2161 | the format string to the output |
| 2162 | string. (we cannot just skip the |
| 2163 | code, since there's no way to know |
| 2164 | what's in the argument list) */ |
| 2165 | n += strlen(p); |
| 2166 | goto expand; |
| 2167 | } |
| 2168 | } else |
| 2169 | n++; |
| 2170 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2171 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2172 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2173 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2174 | we don't have to resize the string. |
| 2175 | There can be no errors beyond this point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2176 | string = (PyUnicodeObject *)PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2177 | if (!string) |
| 2178 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2179 | kind = PyUnicode_KIND(string); |
| 2180 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2181 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2182 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2183 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2184 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2185 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2186 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2187 | |
| 2188 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2189 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2190 | /* checking for == because the last argument could be a empty |
| 2191 | string, which causes i to point to end, the assert at the end of |
| 2192 | the loop */ |
| 2193 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2194 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2195 | switch (*f) { |
| 2196 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2197 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2198 | const int ordinal = va_arg(vargs, int); |
| 2199 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2200 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2201 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2202 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2203 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2204 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2205 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2206 | case 'p': |
| 2207 | /* unused, since we already have the result */ |
| 2208 | if (*f == 'p') |
| 2209 | (void) va_arg(vargs, void *); |
| 2210 | else |
| 2211 | (void) va_arg(vargs, int); |
| 2212 | /* extract the result from numberresults and append. */ |
| 2213 | for (; *numberresult; ++i, ++numberresult) |
| 2214 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2215 | /* skip over the separating '\0' */ |
| 2216 | assert(*numberresult == '\0'); |
| 2217 | numberresult++; |
| 2218 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2219 | break; |
| 2220 | case 's': |
| 2221 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2222 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2223 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2224 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2225 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2226 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2227 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2228 | *callresult, 0, |
| 2229 | size) < 0) |
| 2230 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2231 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2232 | /* We're done with the unicode()/repr() => forget it */ |
| 2233 | Py_DECREF(*callresult); |
| 2234 | /* switch to next unicode()/repr() result */ |
| 2235 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2236 | break; |
| 2237 | } |
| 2238 | case 'U': |
| 2239 | { |
| 2240 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2241 | Py_ssize_t size; |
| 2242 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2243 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2244 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2245 | obj, 0, |
| 2246 | size) < 0) |
| 2247 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2248 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2249 | break; |
| 2250 | } |
| 2251 | case 'V': |
| 2252 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2253 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2254 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2255 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2256 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2257 | size = PyUnicode_GET_LENGTH(obj); |
| 2258 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2259 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2260 | obj, 0, |
| 2261 | size) < 0) |
| 2262 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2263 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2264 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2265 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2266 | assert(PyUnicode_KIND(*callresult) <= |
| 2267 | PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2268 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2269 | *callresult, |
| 2270 | 0, size) < 0) |
| 2271 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2272 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2273 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2274 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2275 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2276 | break; |
| 2277 | } |
| 2278 | case 'S': |
| 2279 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2280 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2281 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2282 | /* unused, since we already have the result */ |
| 2283 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2284 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2285 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2286 | *callresult, 0, |
| 2287 | PyUnicode_GET_LENGTH(*callresult)) < 0) |
| 2288 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2289 | i += PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2290 | /* We're done with the unicode()/repr() => forget it */ |
| 2291 | Py_DECREF(*callresult); |
| 2292 | /* switch to next unicode()/repr() result */ |
| 2293 | ++callresult; |
| 2294 | break; |
| 2295 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2296 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2297 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2298 | break; |
| 2299 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2300 | for (; *p; ++p, ++i) |
| 2301 | PyUnicode_WRITE(kind, data, i, *p); |
| 2302 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2303 | goto end; |
| 2304 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2305 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2306 | else { |
| 2307 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2308 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2309 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2310 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2311 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2312 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2313 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2314 | if (callresults) |
| 2315 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2316 | if (numberresults) |
| 2317 | PyObject_Free(numberresults); |
| 2318 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2319 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2320 | if (callresults) { |
| 2321 | PyObject **callresult2 = callresults; |
| 2322 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2323 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2324 | ++callresult2; |
| 2325 | } |
| 2326 | PyObject_Free(callresults); |
| 2327 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2328 | if (numberresults) |
| 2329 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2330 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2333 | PyObject * |
| 2334 | PyUnicode_FromFormat(const char *format, ...) |
| 2335 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2336 | PyObject* ret; |
| 2337 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2338 | |
| 2339 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2340 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2341 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2342 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2343 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2344 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2345 | va_end(vargs); |
| 2346 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2349 | #ifdef HAVE_WCHAR_H |
| 2350 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2351 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2352 | convert a Unicode object to a wide character string. |
| 2353 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2354 | - 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] | 2355 | character) required to convert the unicode object. Ignore size argument. |
| 2356 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2357 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2358 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2359 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2360 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2361 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 2362 | wchar_t *w, |
| 2363 | Py_ssize_t size) |
| 2364 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2365 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2366 | const wchar_t *wstr; |
| 2367 | |
| 2368 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 2369 | if (wstr == NULL) |
| 2370 | return -1; |
| 2371 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2372 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2373 | if (size > res) |
| 2374 | size = res + 1; |
| 2375 | else |
| 2376 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2377 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2378 | return res; |
| 2379 | } |
| 2380 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2381 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2385 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2386 | wchar_t *w, |
| 2387 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2388 | { |
| 2389 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2390 | PyErr_BadInternalCall(); |
| 2391 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2392 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2393 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2396 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2397 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2398 | Py_ssize_t *size) |
| 2399 | { |
| 2400 | wchar_t* buffer; |
| 2401 | Py_ssize_t buflen; |
| 2402 | |
| 2403 | if (unicode == NULL) { |
| 2404 | PyErr_BadInternalCall(); |
| 2405 | return NULL; |
| 2406 | } |
| 2407 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2408 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2409 | if (buflen == -1) |
| 2410 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2411 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2412 | PyErr_NoMemory(); |
| 2413 | return NULL; |
| 2414 | } |
| 2415 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2416 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2417 | if (buffer == NULL) { |
| 2418 | PyErr_NoMemory(); |
| 2419 | return NULL; |
| 2420 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2421 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2422 | if (buflen == -1) |
| 2423 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2424 | if (size != NULL) |
| 2425 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2426 | return buffer; |
| 2427 | } |
| 2428 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2429 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2430 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2431 | PyObject * |
| 2432 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2433 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2434 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2435 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2436 | PyErr_SetString(PyExc_ValueError, |
| 2437 | "chr() arg not in range(0x110000)"); |
| 2438 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2439 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2440 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2441 | if (ordinal < 256) |
| 2442 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2443 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2444 | v = PyUnicode_New(1, ordinal); |
| 2445 | if (v == NULL) |
| 2446 | return NULL; |
| 2447 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
| 2448 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2451 | PyObject * |
| 2452 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2453 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2454 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2455 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2456 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2457 | if (PyUnicode_READY(obj)) |
| 2458 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2459 | Py_INCREF(obj); |
| 2460 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2461 | } |
| 2462 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2463 | /* For a Unicode subtype that's not a Unicode object, |
| 2464 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2465 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2466 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2467 | PyErr_Format(PyExc_TypeError, |
| 2468 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2469 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2470 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2473 | PyObject * |
| 2474 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2475 | const char *encoding, |
| 2476 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2477 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2478 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2479 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2480 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2481 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2482 | PyErr_BadInternalCall(); |
| 2483 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2484 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2485 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2486 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2487 | if (PyBytes_Check(obj)) { |
| 2488 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2489 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2490 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2491 | } |
| 2492 | else { |
| 2493 | v = PyUnicode_Decode( |
| 2494 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2495 | encoding, errors); |
| 2496 | } |
| 2497 | return v; |
| 2498 | } |
| 2499 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2500 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2501 | PyErr_SetString(PyExc_TypeError, |
| 2502 | "decoding str is not supported"); |
| 2503 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2504 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2505 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2506 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2507 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2508 | PyErr_Format(PyExc_TypeError, |
| 2509 | "coercing to str: need bytes, bytearray " |
| 2510 | "or buffer-like object, %.80s found", |
| 2511 | Py_TYPE(obj)->tp_name); |
| 2512 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2513 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2514 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2515 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2516 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2517 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2518 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2519 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2520 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2521 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2522 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2523 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2526 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2527 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2528 | 1 on success. */ |
| 2529 | static int |
| 2530 | normalize_encoding(const char *encoding, |
| 2531 | char *lower, |
| 2532 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2533 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2534 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2535 | char *l; |
| 2536 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2537 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2538 | e = encoding; |
| 2539 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2540 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2541 | while (*e) { |
| 2542 | if (l == l_end) |
| 2543 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2544 | if (Py_ISUPPER(*e)) { |
| 2545 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2546 | } |
| 2547 | else if (*e == '_') { |
| 2548 | *l++ = '-'; |
| 2549 | e++; |
| 2550 | } |
| 2551 | else { |
| 2552 | *l++ = *e++; |
| 2553 | } |
| 2554 | } |
| 2555 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2556 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2559 | PyObject * |
| 2560 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2561 | Py_ssize_t size, |
| 2562 | const char *encoding, |
| 2563 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2564 | { |
| 2565 | PyObject *buffer = NULL, *unicode; |
| 2566 | Py_buffer info; |
| 2567 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2568 | |
| 2569 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2570 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2571 | |
| 2572 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2573 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2574 | if ((strcmp(lower, "utf-8") == 0) || |
| 2575 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2576 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2577 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2578 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2579 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2580 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2581 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2582 | else if (strcmp(lower, "mbcs") == 0) |
| 2583 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2584 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2585 | else if (strcmp(lower, "ascii") == 0) |
| 2586 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2587 | else if (strcmp(lower, "utf-16") == 0) |
| 2588 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2589 | else if (strcmp(lower, "utf-32") == 0) |
| 2590 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2591 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2592 | |
| 2593 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2594 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2595 | 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] | 2596 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2597 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2598 | if (buffer == NULL) |
| 2599 | goto onError; |
| 2600 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2601 | if (unicode == NULL) |
| 2602 | goto onError; |
| 2603 | if (!PyUnicode_Check(unicode)) { |
| 2604 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2605 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2606 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2607 | Py_DECREF(unicode); |
| 2608 | goto onError; |
| 2609 | } |
| 2610 | Py_DECREF(buffer); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2611 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2612 | Py_DECREF(unicode); |
| 2613 | return NULL; |
| 2614 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2615 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2616 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2617 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2618 | Py_XDECREF(buffer); |
| 2619 | return NULL; |
| 2620 | } |
| 2621 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2622 | PyObject * |
| 2623 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2624 | const char *encoding, |
| 2625 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2626 | { |
| 2627 | PyObject *v; |
| 2628 | |
| 2629 | if (!PyUnicode_Check(unicode)) { |
| 2630 | PyErr_BadArgument(); |
| 2631 | goto onError; |
| 2632 | } |
| 2633 | |
| 2634 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2635 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2636 | |
| 2637 | /* Decode via the codec registry */ |
| 2638 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2639 | if (v == NULL) |
| 2640 | goto onError; |
| 2641 | return v; |
| 2642 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2643 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2644 | return NULL; |
| 2645 | } |
| 2646 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2647 | PyObject * |
| 2648 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2649 | const char *encoding, |
| 2650 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2651 | { |
| 2652 | PyObject *v; |
| 2653 | |
| 2654 | if (!PyUnicode_Check(unicode)) { |
| 2655 | PyErr_BadArgument(); |
| 2656 | goto onError; |
| 2657 | } |
| 2658 | |
| 2659 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2660 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2661 | |
| 2662 | /* Decode via the codec registry */ |
| 2663 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2664 | if (v == NULL) |
| 2665 | goto onError; |
| 2666 | if (!PyUnicode_Check(v)) { |
| 2667 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2668 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2669 | Py_TYPE(v)->tp_name); |
| 2670 | Py_DECREF(v); |
| 2671 | goto onError; |
| 2672 | } |
| 2673 | return v; |
| 2674 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2675 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2676 | return NULL; |
| 2677 | } |
| 2678 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2679 | PyObject * |
| 2680 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2681 | Py_ssize_t size, |
| 2682 | const char *encoding, |
| 2683 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2684 | { |
| 2685 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2686 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2687 | unicode = PyUnicode_FromUnicode(s, size); |
| 2688 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2689 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2690 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2691 | Py_DECREF(unicode); |
| 2692 | return v; |
| 2693 | } |
| 2694 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2695 | PyObject * |
| 2696 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2697 | const char *encoding, |
| 2698 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2699 | { |
| 2700 | PyObject *v; |
| 2701 | |
| 2702 | if (!PyUnicode_Check(unicode)) { |
| 2703 | PyErr_BadArgument(); |
| 2704 | goto onError; |
| 2705 | } |
| 2706 | |
| 2707 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2708 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2709 | |
| 2710 | /* Encode via the codec registry */ |
| 2711 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2712 | if (v == NULL) |
| 2713 | goto onError; |
| 2714 | return v; |
| 2715 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2716 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2717 | return NULL; |
| 2718 | } |
| 2719 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2720 | PyObject * |
| 2721 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2722 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2723 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2724 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2725 | PyUnicode_GET_SIZE(unicode), |
| 2726 | NULL); |
| 2727 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2728 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2729 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2730 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2731 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2732 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2733 | the Python codec requires to encode at least its own filename. Use the C |
| 2734 | version of the locale codec until the codec registry is initialized and |
| 2735 | the Python codec is loaded. |
| 2736 | |
| 2737 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2738 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2739 | subinterpreters. */ |
| 2740 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2741 | return PyUnicode_AsEncodedString(unicode, |
| 2742 | Py_FileSystemDefaultEncoding, |
| 2743 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2744 | } |
| 2745 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2746 | /* locale encoding with surrogateescape */ |
| 2747 | wchar_t *wchar; |
| 2748 | char *bytes; |
| 2749 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2750 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2751 | |
| 2752 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2753 | if (wchar == NULL) |
| 2754 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2755 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2756 | if (bytes == NULL) { |
| 2757 | if (error_pos != (size_t)-1) { |
| 2758 | char *errmsg = strerror(errno); |
| 2759 | PyObject *exc = NULL; |
| 2760 | if (errmsg == NULL) |
| 2761 | errmsg = "Py_wchar2char() failed"; |
| 2762 | raise_encode_exception(&exc, |
| 2763 | "filesystemencoding", |
| 2764 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2765 | error_pos, error_pos+1, |
| 2766 | errmsg); |
| 2767 | Py_XDECREF(exc); |
| 2768 | } |
| 2769 | else |
| 2770 | PyErr_NoMemory(); |
| 2771 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2772 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2773 | } |
| 2774 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2775 | |
| 2776 | bytes_obj = PyBytes_FromString(bytes); |
| 2777 | PyMem_Free(bytes); |
| 2778 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2779 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2780 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2783 | PyObject * |
| 2784 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2785 | const char *encoding, |
| 2786 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2787 | { |
| 2788 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2789 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2790 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2791 | if (!PyUnicode_Check(unicode)) { |
| 2792 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2793 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2794 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2795 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2796 | if (encoding == NULL) { |
| 2797 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2798 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2799 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2800 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2801 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2802 | |
| 2803 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2804 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2805 | if ((strcmp(lower, "utf-8") == 0) || |
| 2806 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2807 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 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 | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2812 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2813 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2814 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2815 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2816 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2817 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2818 | else if (strcmp(lower, "mbcs") == 0) |
| 2819 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2820 | PyUnicode_GET_SIZE(unicode), |
| 2821 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2822 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2823 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2824 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2825 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2826 | |
| 2827 | /* Encode via the codec registry */ |
| 2828 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2829 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2830 | return NULL; |
| 2831 | |
| 2832 | /* The normal path */ |
| 2833 | if (PyBytes_Check(v)) |
| 2834 | return v; |
| 2835 | |
| 2836 | /* 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] | 2837 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2838 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2839 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2840 | |
| 2841 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 2842 | "encoder %s returned bytearray instead of bytes", |
| 2843 | encoding); |
| 2844 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2845 | Py_DECREF(v); |
| 2846 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2847 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2848 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2849 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 2850 | Py_DECREF(v); |
| 2851 | return b; |
| 2852 | } |
| 2853 | |
| 2854 | PyErr_Format(PyExc_TypeError, |
| 2855 | "encoder did not return a bytes object (type=%.400s)", |
| 2856 | Py_TYPE(v)->tp_name); |
| 2857 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2858 | return NULL; |
| 2859 | } |
| 2860 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2861 | PyObject * |
| 2862 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2863 | const char *encoding, |
| 2864 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2865 | { |
| 2866 | PyObject *v; |
| 2867 | |
| 2868 | if (!PyUnicode_Check(unicode)) { |
| 2869 | PyErr_BadArgument(); |
| 2870 | goto onError; |
| 2871 | } |
| 2872 | |
| 2873 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2874 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2875 | |
| 2876 | /* Encode via the codec registry */ |
| 2877 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2878 | if (v == NULL) |
| 2879 | goto onError; |
| 2880 | if (!PyUnicode_Check(v)) { |
| 2881 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2882 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2883 | Py_TYPE(v)->tp_name); |
| 2884 | Py_DECREF(v); |
| 2885 | goto onError; |
| 2886 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2887 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2888 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2889 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2890 | return NULL; |
| 2891 | } |
| 2892 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2893 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2894 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2895 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2896 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 2897 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2898 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2899 | PyObject* |
| 2900 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 2901 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2902 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2903 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 2904 | #elif defined(__APPLE__) |
| 2905 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 2906 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2907 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2908 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2909 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2910 | the Python codec requires to encode at least its own filename. Use the C |
| 2911 | version of the locale codec until the codec registry is initialized and |
| 2912 | the Python codec is loaded. |
| 2913 | |
| 2914 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2915 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2916 | subinterpreters. */ |
| 2917 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2918 | return PyUnicode_Decode(s, size, |
| 2919 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 2920 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2921 | } |
| 2922 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2923 | /* locale encoding with surrogateescape */ |
| 2924 | wchar_t *wchar; |
| 2925 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2926 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2927 | |
| 2928 | if (s[size] != '\0' || size != strlen(s)) { |
| 2929 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2930 | return NULL; |
| 2931 | } |
| 2932 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2933 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2934 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 2935 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2936 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2937 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2938 | PyMem_Free(wchar); |
| 2939 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2940 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2941 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2944 | |
| 2945 | int |
| 2946 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 2947 | { |
| 2948 | PyObject *output = NULL; |
| 2949 | Py_ssize_t size; |
| 2950 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2951 | if (arg == NULL) { |
| 2952 | Py_DECREF(*(PyObject**)addr); |
| 2953 | return 1; |
| 2954 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2955 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2956 | output = arg; |
| 2957 | Py_INCREF(output); |
| 2958 | } |
| 2959 | else { |
| 2960 | arg = PyUnicode_FromObject(arg); |
| 2961 | if (!arg) |
| 2962 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2963 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2964 | Py_DECREF(arg); |
| 2965 | if (!output) |
| 2966 | return 0; |
| 2967 | if (!PyBytes_Check(output)) { |
| 2968 | Py_DECREF(output); |
| 2969 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 2970 | return 0; |
| 2971 | } |
| 2972 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 2973 | size = PyBytes_GET_SIZE(output); |
| 2974 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2975 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 2976 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2977 | Py_DECREF(output); |
| 2978 | return 0; |
| 2979 | } |
| 2980 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2981 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2985 | int |
| 2986 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 2987 | { |
| 2988 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2989 | if (arg == NULL) { |
| 2990 | Py_DECREF(*(PyObject**)addr); |
| 2991 | return 1; |
| 2992 | } |
| 2993 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2994 | if (PyUnicode_READY(arg)) |
| 2995 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2996 | output = arg; |
| 2997 | Py_INCREF(output); |
| 2998 | } |
| 2999 | else { |
| 3000 | arg = PyBytes_FromObject(arg); |
| 3001 | if (!arg) |
| 3002 | return 0; |
| 3003 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3004 | PyBytes_GET_SIZE(arg)); |
| 3005 | Py_DECREF(arg); |
| 3006 | if (!output) |
| 3007 | return 0; |
| 3008 | if (!PyUnicode_Check(output)) { |
| 3009 | Py_DECREF(output); |
| 3010 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3011 | return 0; |
| 3012 | } |
| 3013 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3014 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 3015 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3016 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3017 | Py_DECREF(output); |
| 3018 | return 0; |
| 3019 | } |
| 3020 | *(PyObject**)addr = output; |
| 3021 | return Py_CLEANUP_SUPPORTED; |
| 3022 | } |
| 3023 | |
| 3024 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3025 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3026 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3027 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3028 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3029 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 3030 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3031 | if (!PyUnicode_Check(unicode)) { |
| 3032 | PyErr_BadArgument(); |
| 3033 | return NULL; |
| 3034 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3035 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3036 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3037 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3038 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3039 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3040 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3041 | if (bytes == NULL) |
| 3042 | return NULL; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3043 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3044 | if (_PyUnicode_UTF8(u) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3045 | Py_DECREF(bytes); |
| 3046 | return NULL; |
| 3047 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3048 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); |
| 3049 | 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] | 3050 | Py_DECREF(bytes); |
| 3051 | } |
| 3052 | |
| 3053 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3054 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3055 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3059 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3060 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3061 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3062 | } |
| 3063 | |
| 3064 | #ifdef Py_DEBUG |
| 3065 | int unicode_as_unicode_calls = 0; |
| 3066 | #endif |
| 3067 | |
| 3068 | |
| 3069 | Py_UNICODE * |
| 3070 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3071 | { |
| 3072 | PyUnicodeObject *u; |
| 3073 | const unsigned char *one_byte; |
| 3074 | #if SIZEOF_WCHAR_T == 4 |
| 3075 | const Py_UCS2 *two_bytes; |
| 3076 | #else |
| 3077 | const Py_UCS4 *four_bytes; |
| 3078 | const Py_UCS4 *ucs4_end; |
| 3079 | Py_ssize_t num_surrogates; |
| 3080 | #endif |
| 3081 | wchar_t *w; |
| 3082 | wchar_t *wchar_end; |
| 3083 | |
| 3084 | if (!PyUnicode_Check(unicode)) { |
| 3085 | PyErr_BadArgument(); |
| 3086 | return NULL; |
| 3087 | } |
| 3088 | u = (PyUnicodeObject*)unicode; |
| 3089 | if (_PyUnicode_WSTR(u) == NULL) { |
| 3090 | /* Non-ASCII compact unicode object */ |
| 3091 | assert(_PyUnicode_KIND(u) != 0); |
| 3092 | assert(PyUnicode_IS_READY(u)); |
| 3093 | |
| 3094 | #ifdef Py_DEBUG |
| 3095 | ++unicode_as_unicode_calls; |
| 3096 | #endif |
| 3097 | |
| 3098 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 3099 | #if SIZEOF_WCHAR_T == 2 |
| 3100 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3101 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 3102 | num_surrogates = 0; |
| 3103 | |
| 3104 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3105 | if (*four_bytes > 0xFFFF) |
| 3106 | ++num_surrogates; |
| 3107 | } |
| 3108 | |
| 3109 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 3110 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 3111 | if (!_PyUnicode_WSTR(u)) { |
| 3112 | PyErr_NoMemory(); |
| 3113 | return NULL; |
| 3114 | } |
| 3115 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 3116 | |
| 3117 | w = _PyUnicode_WSTR(u); |
| 3118 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 3119 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3120 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3121 | if (*four_bytes > 0xFFFF) { |
| 3122 | /* encode surrogate pair in this case */ |
| 3123 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3124 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3125 | } |
| 3126 | else |
| 3127 | *w = *four_bytes; |
| 3128 | |
| 3129 | if (w > wchar_end) { |
| 3130 | assert(0 && "Miscalculated string end"); |
| 3131 | } |
| 3132 | } |
| 3133 | *w = 0; |
| 3134 | #else |
| 3135 | /* sizeof(wchar_t) == 4 */ |
| 3136 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3137 | "should share memory already."); |
| 3138 | return NULL; |
| 3139 | #endif |
| 3140 | } |
| 3141 | else { |
| 3142 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3143 | (_PyUnicode_LENGTH(u) + 1)); |
| 3144 | if (!_PyUnicode_WSTR(u)) { |
| 3145 | PyErr_NoMemory(); |
| 3146 | return NULL; |
| 3147 | } |
| 3148 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 3149 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 3150 | w = _PyUnicode_WSTR(u); |
| 3151 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 3152 | |
| 3153 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 3154 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 3155 | for (; w < wchar_end; ++one_byte, ++w) |
| 3156 | *w = *one_byte; |
| 3157 | /* null-terminate the wstr */ |
| 3158 | *w = 0; |
| 3159 | } |
| 3160 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 3161 | #if SIZEOF_WCHAR_T == 4 |
| 3162 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 3163 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3164 | *w = *two_bytes; |
| 3165 | /* null-terminate the wstr */ |
| 3166 | *w = 0; |
| 3167 | #else |
| 3168 | /* sizeof(wchar_t) == 2 */ |
| 3169 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 3170 | _PyUnicode_WSTR(u) = NULL; |
| 3171 | Py_FatalError("Impossible unicode object state, wstr " |
| 3172 | "and str should share memory already."); |
| 3173 | return NULL; |
| 3174 | #endif |
| 3175 | } |
| 3176 | else { |
| 3177 | assert(0 && "This should never happen."); |
| 3178 | } |
| 3179 | } |
| 3180 | } |
| 3181 | if (size != NULL) |
| 3182 | *size = PyUnicode_WSTR_LENGTH(u); |
| 3183 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3186 | Py_UNICODE * |
| 3187 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3188 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3189 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3192 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3193 | Py_ssize_t |
| 3194 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3195 | { |
| 3196 | if (!PyUnicode_Check(unicode)) { |
| 3197 | PyErr_BadArgument(); |
| 3198 | goto onError; |
| 3199 | } |
| 3200 | return PyUnicode_GET_SIZE(unicode); |
| 3201 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3202 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3203 | return -1; |
| 3204 | } |
| 3205 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3206 | Py_ssize_t |
| 3207 | PyUnicode_GetLength(PyObject *unicode) |
| 3208 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3209 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3210 | PyErr_BadArgument(); |
| 3211 | return -1; |
| 3212 | } |
| 3213 | |
| 3214 | return PyUnicode_GET_LENGTH(unicode); |
| 3215 | } |
| 3216 | |
| 3217 | Py_UCS4 |
| 3218 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3219 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3220 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3221 | PyErr_BadArgument(); |
| 3222 | return (Py_UCS4)-1; |
| 3223 | } |
| 3224 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3225 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3226 | return (Py_UCS4)-1; |
| 3227 | } |
| 3228 | return PyUnicode_READ_CHAR(unicode, index); |
| 3229 | } |
| 3230 | |
| 3231 | int |
| 3232 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3233 | { |
| 3234 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3235 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3236 | return -1; |
| 3237 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3238 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3239 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3240 | return -1; |
| 3241 | } |
| 3242 | if (_PyUnicode_Dirty(unicode)) |
| 3243 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3244 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3245 | index, ch); |
| 3246 | return 0; |
| 3247 | } |
| 3248 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3249 | const char * |
| 3250 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3251 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3252 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3255 | /* create or adjust a UnicodeDecodeError */ |
| 3256 | static void |
| 3257 | make_decode_exception(PyObject **exceptionObject, |
| 3258 | const char *encoding, |
| 3259 | const char *input, Py_ssize_t length, |
| 3260 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3261 | const char *reason) |
| 3262 | { |
| 3263 | if (*exceptionObject == NULL) { |
| 3264 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3265 | encoding, input, length, startpos, endpos, reason); |
| 3266 | } |
| 3267 | else { |
| 3268 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3269 | goto onError; |
| 3270 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3271 | goto onError; |
| 3272 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3273 | goto onError; |
| 3274 | } |
| 3275 | return; |
| 3276 | |
| 3277 | onError: |
| 3278 | Py_DECREF(*exceptionObject); |
| 3279 | *exceptionObject = NULL; |
| 3280 | } |
| 3281 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3282 | /* error handling callback helper: |
| 3283 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3284 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3285 | and adjust various state variables. |
| 3286 | return 0 on success, -1 on error |
| 3287 | */ |
| 3288 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3289 | static int |
| 3290 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3291 | const char *encoding, const char *reason, |
| 3292 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3293 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3294 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3295 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3296 | 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] | 3297 | |
| 3298 | PyObject *restuple = NULL; |
| 3299 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3300 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3301 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3302 | Py_ssize_t requiredsize; |
| 3303 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3304 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3305 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3306 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3307 | int res = -1; |
| 3308 | |
| 3309 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3310 | *errorHandler = PyCodec_LookupError(errors); |
| 3311 | if (*errorHandler == NULL) |
| 3312 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3315 | make_decode_exception(exceptionObject, |
| 3316 | encoding, |
| 3317 | *input, *inend - *input, |
| 3318 | *startinpos, *endinpos, |
| 3319 | reason); |
| 3320 | if (*exceptionObject == NULL) |
| 3321 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3322 | |
| 3323 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3324 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3325 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3326 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3327 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3328 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3329 | } |
| 3330 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3331 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3332 | |
| 3333 | /* Copy back the bytes variables, which might have been modified by the |
| 3334 | callback */ |
| 3335 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3336 | if (!inputobj) |
| 3337 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3338 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3339 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3340 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3341 | *input = PyBytes_AS_STRING(inputobj); |
| 3342 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3343 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3344 | /* we can DECREF safely, as the exception has another reference, |
| 3345 | so the object won't go away. */ |
| 3346 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3347 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3348 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3349 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3350 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3351 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3352 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3353 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3354 | |
| 3355 | /* need more space? (at least enough for what we |
| 3356 | have+the replacement+the rest of the string (starting |
| 3357 | at the new input position), so we won't have to check space |
| 3358 | when there are no errors in the rest of the string) */ |
| 3359 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3360 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3361 | requiredsize = *outpos + repsize + insize-newpos; |
| 3362 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3363 | if (requiredsize<2*outsize) |
| 3364 | requiredsize = 2*outsize; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3365 | if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3366 | goto onError; |
| 3367 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3368 | } |
| 3369 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3370 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3371 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3372 | *outptr += repsize; |
| 3373 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3374 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3375 | /* we made it! */ |
| 3376 | res = 0; |
| 3377 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3378 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3379 | Py_XDECREF(restuple); |
| 3380 | return res; |
| 3381 | } |
| 3382 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3383 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3384 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3385 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3386 | |
| 3387 | /* Three simple macros defining base-64. */ |
| 3388 | |
| 3389 | /* Is c a base-64 character? */ |
| 3390 | |
| 3391 | #define IS_BASE64(c) \ |
| 3392 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3393 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3394 | ((c) >= '0' && (c) <= '9') || \ |
| 3395 | (c) == '+' || (c) == '/') |
| 3396 | |
| 3397 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3398 | |
| 3399 | #define FROM_BASE64(c) \ |
| 3400 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3401 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3402 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3403 | (c) == '+' ? 62 : 63) |
| 3404 | |
| 3405 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3406 | |
| 3407 | #define TO_BASE64(n) \ |
| 3408 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3409 | |
| 3410 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3411 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3412 | * byte not decoding to itself is the + which begins a base64 |
| 3413 | * string. */ |
| 3414 | |
| 3415 | #define DECODE_DIRECT(c) \ |
| 3416 | ((c) <= 127 && (c) != '+') |
| 3417 | |
| 3418 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3419 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3420 | * the above). See RFC2152. This array identifies these different |
| 3421 | * sets: |
| 3422 | * 0 : "Set D" |
| 3423 | * alphanumeric and '(),-./:? |
| 3424 | * 1 : "Set O" |
| 3425 | * !"#$%&*;<=>@[]^_`{|} |
| 3426 | * 2 : "whitespace" |
| 3427 | * ht nl cr sp |
| 3428 | * 3 : special (must be base64 encoded) |
| 3429 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3430 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3431 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3432 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3433 | char utf7_category[128] = { |
| 3434 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3435 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3436 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3437 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3438 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3439 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3440 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3441 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3442 | /* @ A B C D E F G H I J K L M N O */ |
| 3443 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3444 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3445 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3446 | /* ` a b c d e f g h i j k l m n o */ |
| 3447 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3448 | /* p q r s t u v w x y z { | } ~ del */ |
| 3449 | 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] | 3450 | }; |
| 3451 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3452 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3453 | * answer depends on whether we are encoding set O as itself, and also |
| 3454 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3455 | * clear that the answers to these questions vary between |
| 3456 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3457 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3458 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3459 | ((c) < 128 && (c) > 0 && \ |
| 3460 | ((utf7_category[(c)] == 0) || \ |
| 3461 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3462 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3463 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3464 | PyObject * |
| 3465 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3466 | Py_ssize_t size, |
| 3467 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3468 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3469 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3470 | } |
| 3471 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3472 | /* The decoder. The only state we preserve is our read position, |
| 3473 | * i.e. how many characters we have consumed. So if we end in the |
| 3474 | * middle of a shift sequence we have to back off the read position |
| 3475 | * and the output to the beginning of the sequence, otherwise we lose |
| 3476 | * all the shift state (seen bits, number of bits seen, high |
| 3477 | * surrogate). */ |
| 3478 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3479 | PyObject * |
| 3480 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3481 | Py_ssize_t size, |
| 3482 | const char *errors, |
| 3483 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3484 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3485 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3486 | Py_ssize_t startinpos; |
| 3487 | Py_ssize_t endinpos; |
| 3488 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3489 | const char *e; |
| 3490 | PyUnicodeObject *unicode; |
| 3491 | Py_UNICODE *p; |
| 3492 | const char *errmsg = ""; |
| 3493 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3494 | Py_UNICODE *shiftOutStart; |
| 3495 | unsigned int base64bits = 0; |
| 3496 | unsigned long base64buffer = 0; |
| 3497 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3498 | PyObject *errorHandler = NULL; |
| 3499 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3500 | |
| 3501 | unicode = _PyUnicode_New(size); |
| 3502 | if (!unicode) |
| 3503 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3504 | if (size == 0) { |
| 3505 | if (consumed) |
| 3506 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3507 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3508 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3509 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3510 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3511 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3512 | e = s + size; |
| 3513 | |
| 3514 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3515 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3516 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3517 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3518 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3519 | if (inShift) { /* in a base-64 section */ |
| 3520 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3521 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3522 | base64bits += 6; |
| 3523 | s++; |
| 3524 | if (base64bits >= 16) { |
| 3525 | /* we have enough bits for a UTF-16 value */ |
| 3526 | Py_UNICODE outCh = (Py_UNICODE) |
| 3527 | (base64buffer >> (base64bits-16)); |
| 3528 | base64bits -= 16; |
| 3529 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3530 | if (surrogate) { |
| 3531 | /* expecting a second surrogate */ |
| 3532 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3533 | #ifdef Py_UNICODE_WIDE |
| 3534 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3535 | | (outCh & 0x3FF)) + 0x10000; |
| 3536 | #else |
| 3537 | *p++ = surrogate; |
| 3538 | *p++ = outCh; |
| 3539 | #endif |
| 3540 | surrogate = 0; |
| 3541 | } |
| 3542 | else { |
| 3543 | surrogate = 0; |
| 3544 | errmsg = "second surrogate missing"; |
| 3545 | goto utf7Error; |
| 3546 | } |
| 3547 | } |
| 3548 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3549 | /* first surrogate */ |
| 3550 | surrogate = outCh; |
| 3551 | } |
| 3552 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3553 | errmsg = "unexpected second surrogate"; |
| 3554 | goto utf7Error; |
| 3555 | } |
| 3556 | else { |
| 3557 | *p++ = outCh; |
| 3558 | } |
| 3559 | } |
| 3560 | } |
| 3561 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3562 | inShift = 0; |
| 3563 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3564 | if (surrogate) { |
| 3565 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3566 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3567 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3568 | if (base64bits > 0) { /* left-over bits */ |
| 3569 | if (base64bits >= 6) { |
| 3570 | /* We've seen at least one base-64 character */ |
| 3571 | errmsg = "partial character in shift sequence"; |
| 3572 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3573 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3574 | else { |
| 3575 | /* Some bits remain; they should be zero */ |
| 3576 | if (base64buffer != 0) { |
| 3577 | errmsg = "non-zero padding bits in shift sequence"; |
| 3578 | goto utf7Error; |
| 3579 | } |
| 3580 | } |
| 3581 | } |
| 3582 | if (ch != '-') { |
| 3583 | /* '-' is absorbed; other terminating |
| 3584 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3585 | *p++ = ch; |
| 3586 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3587 | } |
| 3588 | } |
| 3589 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3590 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3591 | s++; /* consume '+' */ |
| 3592 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3593 | s++; |
| 3594 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3595 | } |
| 3596 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3597 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3598 | shiftOutStart = p; |
| 3599 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3600 | } |
| 3601 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3602 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3603 | *p++ = ch; |
| 3604 | s++; |
| 3605 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3606 | else { |
| 3607 | startinpos = s-starts; |
| 3608 | s++; |
| 3609 | errmsg = "unexpected special character"; |
| 3610 | goto utf7Error; |
| 3611 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3612 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3613 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3614 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3615 | endinpos = s-starts; |
| 3616 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3617 | errors, &errorHandler, |
| 3618 | "utf7", errmsg, |
| 3619 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3620 | &unicode, &outpos, &p)) |
| 3621 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3622 | } |
| 3623 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3624 | /* end of string */ |
| 3625 | |
| 3626 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3627 | /* if we're in an inconsistent state, that's an error */ |
| 3628 | if (surrogate || |
| 3629 | (base64bits >= 6) || |
| 3630 | (base64bits > 0 && base64buffer != 0)) { |
| 3631 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3632 | endinpos = size; |
| 3633 | if (unicode_decode_call_errorhandler( |
| 3634 | errors, &errorHandler, |
| 3635 | "utf7", "unterminated shift sequence", |
| 3636 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3637 | &unicode, &outpos, &p)) |
| 3638 | goto onError; |
| 3639 | if (s < e) |
| 3640 | goto restart; |
| 3641 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3642 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3643 | |
| 3644 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3645 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3646 | if (inShift) { |
| 3647 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3648 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3649 | } |
| 3650 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3651 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3652 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3653 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3654 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3655 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3656 | goto onError; |
| 3657 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3658 | Py_XDECREF(errorHandler); |
| 3659 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3660 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3661 | Py_DECREF(unicode); |
| 3662 | return NULL; |
| 3663 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3664 | return (PyObject *)unicode; |
| 3665 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3666 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3667 | Py_XDECREF(errorHandler); |
| 3668 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3669 | Py_DECREF(unicode); |
| 3670 | return NULL; |
| 3671 | } |
| 3672 | |
| 3673 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3674 | PyObject * |
| 3675 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3676 | Py_ssize_t size, |
| 3677 | int base64SetO, |
| 3678 | int base64WhiteSpace, |
| 3679 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3680 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3681 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3682 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3683 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3684 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3685 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3686 | unsigned int base64bits = 0; |
| 3687 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3688 | char * out; |
| 3689 | char * start; |
| 3690 | |
| 3691 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3692 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3693 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3694 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3695 | return PyErr_NoMemory(); |
| 3696 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3697 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3698 | if (v == NULL) |
| 3699 | return NULL; |
| 3700 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3701 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3702 | for (;i < size; ++i) { |
| 3703 | Py_UNICODE ch = s[i]; |
| 3704 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3705 | if (inShift) { |
| 3706 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3707 | /* shifting out */ |
| 3708 | if (base64bits) { /* output remaining bits */ |
| 3709 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3710 | base64buffer = 0; |
| 3711 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3712 | } |
| 3713 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3714 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3715 | so no '-' is required, except if the character is itself a '-' */ |
| 3716 | if (IS_BASE64(ch) || ch == '-') { |
| 3717 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3718 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3719 | *out++ = (char) ch; |
| 3720 | } |
| 3721 | else { |
| 3722 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3723 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3724 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3725 | else { /* not in a shift sequence */ |
| 3726 | if (ch == '+') { |
| 3727 | *out++ = '+'; |
| 3728 | *out++ = '-'; |
| 3729 | } |
| 3730 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3731 | *out++ = (char) ch; |
| 3732 | } |
| 3733 | else { |
| 3734 | *out++ = '+'; |
| 3735 | inShift = 1; |
| 3736 | goto encode_char; |
| 3737 | } |
| 3738 | } |
| 3739 | continue; |
| 3740 | encode_char: |
| 3741 | #ifdef Py_UNICODE_WIDE |
| 3742 | if (ch >= 0x10000) { |
| 3743 | /* code first surrogate */ |
| 3744 | base64bits += 16; |
| 3745 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3746 | while (base64bits >= 6) { |
| 3747 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3748 | base64bits -= 6; |
| 3749 | } |
| 3750 | /* prepare second surrogate */ |
| 3751 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3752 | } |
| 3753 | #endif |
| 3754 | base64bits += 16; |
| 3755 | base64buffer = (base64buffer << 16) | ch; |
| 3756 | while (base64bits >= 6) { |
| 3757 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3758 | base64bits -= 6; |
| 3759 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3760 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3761 | if (base64bits) |
| 3762 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3763 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3764 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3765 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3766 | return NULL; |
| 3767 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3770 | #undef IS_BASE64 |
| 3771 | #undef FROM_BASE64 |
| 3772 | #undef TO_BASE64 |
| 3773 | #undef DECODE_DIRECT |
| 3774 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3775 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3776 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 3777 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3778 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3779 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3780 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 3781 | illegal prefix. See RFC 3629 for details */ |
| 3782 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 3783 | 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] | 3784 | 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] | 3785 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3786 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3787 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3788 | 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] | 3789 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 3790 | 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] | 3791 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3792 | 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] | 3793 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 3794 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 3795 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 3796 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 3797 | 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] | 3798 | }; |
| 3799 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3800 | PyObject * |
| 3801 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3802 | Py_ssize_t size, |
| 3803 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3804 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3805 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 3806 | } |
| 3807 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3808 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 3809 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 3810 | |
| 3811 | /* Mask to quickly check whether a C 'long' contains a |
| 3812 | non-ASCII, UTF8-encoded char. */ |
| 3813 | #if (SIZEOF_LONG == 8) |
| 3814 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 3815 | #elif (SIZEOF_LONG == 4) |
| 3816 | # define ASCII_CHAR_MASK 0x80808080L |
| 3817 | #else |
| 3818 | # error C 'long' size should be either 4 or 8! |
| 3819 | #endif |
| 3820 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3821 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 3822 | the size of the decoded unicode string and if any major errors were |
| 3823 | encountered. |
| 3824 | |
| 3825 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 3826 | if the string contains surrogates, and if all continuation bytes are |
| 3827 | within the correct ranges, these checks are performed in |
| 3828 | PyUnicode_DecodeUTF8Stateful. |
| 3829 | |
| 3830 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 3831 | will be bogus and you should not rely on useful information in them. |
| 3832 | */ |
| 3833 | static Py_UCS4 |
| 3834 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 3835 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 3836 | int *has_errors) |
| 3837 | { |
| 3838 | Py_ssize_t n; |
| 3839 | Py_ssize_t char_count = 0; |
| 3840 | Py_UCS4 max_char = 127, new_max; |
| 3841 | Py_UCS4 upper_bound; |
| 3842 | const unsigned char *p = (const unsigned char *)s; |
| 3843 | const unsigned char *end = p + string_size; |
| 3844 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 3845 | int err = 0; |
| 3846 | |
| 3847 | for (; p < end && !err; ++p, ++char_count) { |
| 3848 | /* Only check value if it's not a ASCII char... */ |
| 3849 | if (*p < 0x80) { |
| 3850 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 3851 | an explanation. */ |
| 3852 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 3853 | /* Help register allocation */ |
| 3854 | register const unsigned char *_p = p; |
| 3855 | while (_p < aligned_end) { |
| 3856 | unsigned long value = *(unsigned long *) _p; |
| 3857 | if (value & ASCII_CHAR_MASK) |
| 3858 | break; |
| 3859 | _p += SIZEOF_LONG; |
| 3860 | char_count += SIZEOF_LONG; |
| 3861 | } |
| 3862 | p = _p; |
| 3863 | if (p == end) |
| 3864 | break; |
| 3865 | } |
| 3866 | } |
| 3867 | if (*p >= 0x80) { |
| 3868 | n = utf8_code_length[*p]; |
| 3869 | new_max = max_char; |
| 3870 | switch (n) { |
| 3871 | /* invalid start byte */ |
| 3872 | case 0: |
| 3873 | err = 1; |
| 3874 | break; |
| 3875 | case 2: |
| 3876 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 3877 | Approximate the upper bound of the code point, |
| 3878 | if this flips over 255 we can be sure it will be more |
| 3879 | than 255 and the string will need 2 bytes per code coint, |
| 3880 | if it stays under or equal to 255, we can be sure 1 byte |
| 3881 | is enough. |
| 3882 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 3883 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 3884 | if (max_char < upper_bound) |
| 3885 | new_max = upper_bound; |
| 3886 | /* Ensure we track at least that we left ASCII space. */ |
| 3887 | if (new_max < 128) |
| 3888 | new_max = 128; |
| 3889 | break; |
| 3890 | case 3: |
| 3891 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 3892 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 3893 | if (max_char < 65535) |
| 3894 | new_max = 65535; |
| 3895 | break; |
| 3896 | case 4: |
| 3897 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 3898 | new_max = 65537; |
| 3899 | break; |
| 3900 | /* Internal error, this should be caught by the first if */ |
| 3901 | case 1: |
| 3902 | default: |
| 3903 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 3904 | err = 1; |
| 3905 | } |
| 3906 | /* Instead of number of overall bytes for this code point, |
| 3907 | n containts the number of following bytes: */ |
| 3908 | --n; |
| 3909 | /* Check if the follow up chars are all valid continuation bytes */ |
| 3910 | if (n >= 1) { |
| 3911 | const unsigned char *cont; |
| 3912 | if ((p + n) >= end) { |
| 3913 | if (consumed == 0) |
| 3914 | /* incomplete data, non-incremental decoding */ |
| 3915 | err = 1; |
| 3916 | break; |
| 3917 | } |
| 3918 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 3919 | if ((*cont & 0xc0) != 0x80) { |
| 3920 | err = 1; |
| 3921 | break; |
| 3922 | } |
| 3923 | } |
| 3924 | p += n; |
| 3925 | } |
| 3926 | else |
| 3927 | err = 1; |
| 3928 | max_char = new_max; |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | if (unicode_size) |
| 3933 | *unicode_size = char_count; |
| 3934 | if (has_errors) |
| 3935 | *has_errors = err; |
| 3936 | return max_char; |
| 3937 | } |
| 3938 | |
| 3939 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 3940 | of the legacy unicode representation */ |
| 3941 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 3942 | do { \ |
| 3943 | const int k_ = (kind); \ |
| 3944 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 3945 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 3946 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 3947 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 3948 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 3949 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 3950 | else \ |
| 3951 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 3952 | } while (0) |
| 3953 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3954 | PyObject * |
| 3955 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3956 | Py_ssize_t size, |
| 3957 | const char *errors, |
| 3958 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3959 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3960 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3961 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3962 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3963 | Py_ssize_t startinpos; |
| 3964 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3965 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3966 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3967 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3968 | PyObject *errorHandler = NULL; |
| 3969 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3970 | Py_UCS4 maxchar = 0; |
| 3971 | Py_ssize_t unicode_size; |
| 3972 | Py_ssize_t i; |
| 3973 | int kind; |
| 3974 | void *data; |
| 3975 | int has_errors; |
| 3976 | Py_UNICODE *error_outptr; |
| 3977 | #if SIZEOF_WCHAR_T == 2 |
| 3978 | Py_ssize_t wchar_offset = 0; |
| 3979 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3980 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3981 | if (size == 0) { |
| 3982 | if (consumed) |
| 3983 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3984 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3985 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3986 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 3987 | consumed, &has_errors); |
| 3988 | if (has_errors) { |
| 3989 | unicode = _PyUnicode_New(size); |
| 3990 | if (!unicode) |
| 3991 | return NULL; |
| 3992 | kind = PyUnicode_WCHAR_KIND; |
| 3993 | data = PyUnicode_AS_UNICODE(unicode); |
| 3994 | assert(data != NULL); |
| 3995 | } |
| 3996 | else { |
| 3997 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 3998 | if (!unicode) |
| 3999 | return NULL; |
| 4000 | /* When the string is ASCII only, just use memcpy and return. |
| 4001 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4002 | sequence at the end of the ASCII block. */ |
| 4003 | if (maxchar < 128 && size == unicode_size) { |
| 4004 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4005 | return (PyObject *)unicode; |
| 4006 | } |
| 4007 | kind = PyUnicode_KIND(unicode); |
| 4008 | data = PyUnicode_DATA(unicode); |
| 4009 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4010 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4011 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4012 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4013 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4014 | |
| 4015 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4016 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4017 | |
| 4018 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4019 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4020 | input will consist of an overwhelming majority of ASCII |
| 4021 | characters, we try to optimize for this case by checking |
| 4022 | as many characters as a C 'long' can contain. |
| 4023 | First, check if we can do an aligned read, as most CPUs have |
| 4024 | a penalty for unaligned reads. |
| 4025 | */ |
| 4026 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4027 | /* Help register allocation */ |
| 4028 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4029 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4030 | while (_s < aligned_end) { |
| 4031 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4032 | and do a fast unrolled copy if it only contains ASCII |
| 4033 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4034 | unsigned long value = *(unsigned long *) _s; |
| 4035 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4036 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4037 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4038 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4039 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4040 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4041 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4042 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4043 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4044 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4045 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4046 | #endif |
| 4047 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4048 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4049 | } |
| 4050 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4051 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4052 | if (s == e) |
| 4053 | break; |
| 4054 | ch = (unsigned char)*s; |
| 4055 | } |
| 4056 | } |
| 4057 | |
| 4058 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4059 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4060 | s++; |
| 4061 | continue; |
| 4062 | } |
| 4063 | |
| 4064 | n = utf8_code_length[ch]; |
| 4065 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4066 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4067 | if (consumed) |
| 4068 | break; |
| 4069 | else { |
| 4070 | errmsg = "unexpected end of data"; |
| 4071 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4072 | endinpos = startinpos+1; |
| 4073 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4074 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4075 | goto utf8Error; |
| 4076 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4077 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4078 | |
| 4079 | switch (n) { |
| 4080 | |
| 4081 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4082 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4083 | startinpos = s-starts; |
| 4084 | endinpos = startinpos+1; |
| 4085 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4086 | |
| 4087 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4088 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4089 | startinpos = s-starts; |
| 4090 | endinpos = startinpos+1; |
| 4091 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4092 | |
| 4093 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4094 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4095 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4096 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4097 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4098 | goto utf8Error; |
| 4099 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4100 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4101 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4102 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4103 | break; |
| 4104 | |
| 4105 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4106 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4107 | will result in surrogates in range d800-dfff. Surrogates are |
| 4108 | not valid UTF-8 so they are rejected. |
| 4109 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4110 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4111 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4112 | (s[2] & 0xc0) != 0x80 || |
| 4113 | ((unsigned char)s[0] == 0xE0 && |
| 4114 | (unsigned char)s[1] < 0xA0) || |
| 4115 | ((unsigned char)s[0] == 0xED && |
| 4116 | (unsigned char)s[1] > 0x9F)) { |
| 4117 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4118 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4119 | endinpos = startinpos + 1; |
| 4120 | |
| 4121 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4122 | continuation byte is s[2], so increment endinpos by 1, |
| 4123 | if not, s[1] is invalid and endinpos doesn't need to |
| 4124 | be incremented. */ |
| 4125 | if ((s[1] & 0xC0) == 0x80) |
| 4126 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4127 | goto utf8Error; |
| 4128 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4129 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4130 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4131 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4132 | break; |
| 4133 | |
| 4134 | case 4: |
| 4135 | if ((s[1] & 0xc0) != 0x80 || |
| 4136 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4137 | (s[3] & 0xc0) != 0x80 || |
| 4138 | ((unsigned char)s[0] == 0xF0 && |
| 4139 | (unsigned char)s[1] < 0x90) || |
| 4140 | ((unsigned char)s[0] == 0xF4 && |
| 4141 | (unsigned char)s[1] > 0x8F)) { |
| 4142 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4143 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4144 | endinpos = startinpos + 1; |
| 4145 | if ((s[1] & 0xC0) == 0x80) { |
| 4146 | endinpos++; |
| 4147 | if ((s[2] & 0xC0) == 0x80) |
| 4148 | endinpos++; |
| 4149 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4150 | goto utf8Error; |
| 4151 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4152 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4153 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4154 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4155 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4156 | /* If the string is flexible or we have native UCS-4, write |
| 4157 | directly.. */ |
| 4158 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4159 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4160 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4161 | else { |
| 4162 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4163 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4164 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4165 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4166 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4167 | /* high surrogate = top 10 bits added to D800 */ |
| 4168 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4169 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4170 | |
| 4171 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4172 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4173 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4174 | } |
| 4175 | #if SIZEOF_WCHAR_T == 2 |
| 4176 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4177 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4178 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4179 | } |
| 4180 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4181 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4182 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4183 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4184 | /* If this is not yet a resizable string, make it one.. */ |
| 4185 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4186 | const Py_UNICODE *u; |
| 4187 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 4188 | if (!new_unicode) |
| 4189 | goto onError; |
| 4190 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 4191 | if (!u) |
| 4192 | goto onError; |
| 4193 | #if SIZEOF_WCHAR_T == 2 |
| 4194 | i += wchar_offset; |
| 4195 | #endif |
| 4196 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4197 | Py_DECREF(unicode); |
| 4198 | unicode = new_unicode; |
| 4199 | kind = 0; |
| 4200 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4201 | assert(data != NULL); |
| 4202 | } |
| 4203 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4204 | if (unicode_decode_call_errorhandler( |
| 4205 | errors, &errorHandler, |
| 4206 | "utf8", errmsg, |
| 4207 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4208 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4209 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4210 | /* Update data because unicode_decode_call_errorhandler might have |
| 4211 | re-created or resized the unicode object. */ |
| 4212 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4213 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4214 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4215 | /* Ensure the unicode_size calculation above was correct: */ |
| 4216 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4217 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4218 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4219 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4220 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4221 | /* Adjust length and ready string when it contained errors and |
| 4222 | is of the old resizable kind. */ |
| 4223 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4224 | if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4225 | goto onError; |
| 4226 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4227 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4228 | Py_XDECREF(errorHandler); |
| 4229 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4230 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4231 | Py_DECREF(unicode); |
| 4232 | return NULL; |
| 4233 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4234 | return (PyObject *)unicode; |
| 4235 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4236 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4237 | Py_XDECREF(errorHandler); |
| 4238 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4239 | Py_DECREF(unicode); |
| 4240 | return NULL; |
| 4241 | } |
| 4242 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4243 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4244 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4245 | #ifdef __APPLE__ |
| 4246 | |
| 4247 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4248 | used to decode the command line arguments on Mac OS X. */ |
| 4249 | |
| 4250 | wchar_t* |
| 4251 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4252 | { |
| 4253 | int n; |
| 4254 | const char *e; |
| 4255 | wchar_t *unicode, *p; |
| 4256 | |
| 4257 | /* Note: size will always be longer than the resulting Unicode |
| 4258 | character count */ |
| 4259 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4260 | PyErr_NoMemory(); |
| 4261 | return NULL; |
| 4262 | } |
| 4263 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4264 | if (!unicode) |
| 4265 | return NULL; |
| 4266 | |
| 4267 | /* Unpack UTF-8 encoded data */ |
| 4268 | p = unicode; |
| 4269 | e = s + size; |
| 4270 | while (s < e) { |
| 4271 | Py_UCS4 ch = (unsigned char)*s; |
| 4272 | |
| 4273 | if (ch < 0x80) { |
| 4274 | *p++ = (wchar_t)ch; |
| 4275 | s++; |
| 4276 | continue; |
| 4277 | } |
| 4278 | |
| 4279 | n = utf8_code_length[ch]; |
| 4280 | if (s + n > e) { |
| 4281 | goto surrogateescape; |
| 4282 | } |
| 4283 | |
| 4284 | switch (n) { |
| 4285 | case 0: |
| 4286 | case 1: |
| 4287 | goto surrogateescape; |
| 4288 | |
| 4289 | case 2: |
| 4290 | if ((s[1] & 0xc0) != 0x80) |
| 4291 | goto surrogateescape; |
| 4292 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4293 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4294 | *p++ = (wchar_t)ch; |
| 4295 | break; |
| 4296 | |
| 4297 | case 3: |
| 4298 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4299 | will result in surrogates in range d800-dfff. Surrogates are |
| 4300 | not valid UTF-8 so they are rejected. |
| 4301 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4302 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4303 | if ((s[1] & 0xc0) != 0x80 || |
| 4304 | (s[2] & 0xc0) != 0x80 || |
| 4305 | ((unsigned char)s[0] == 0xE0 && |
| 4306 | (unsigned char)s[1] < 0xA0) || |
| 4307 | ((unsigned char)s[0] == 0xED && |
| 4308 | (unsigned char)s[1] > 0x9F)) { |
| 4309 | |
| 4310 | goto surrogateescape; |
| 4311 | } |
| 4312 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4313 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4314 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4315 | break; |
| 4316 | |
| 4317 | case 4: |
| 4318 | if ((s[1] & 0xc0) != 0x80 || |
| 4319 | (s[2] & 0xc0) != 0x80 || |
| 4320 | (s[3] & 0xc0) != 0x80 || |
| 4321 | ((unsigned char)s[0] == 0xF0 && |
| 4322 | (unsigned char)s[1] < 0x90) || |
| 4323 | ((unsigned char)s[0] == 0xF4 && |
| 4324 | (unsigned char)s[1] > 0x8F)) { |
| 4325 | goto surrogateescape; |
| 4326 | } |
| 4327 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4328 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4329 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4330 | |
| 4331 | #if SIZEOF_WCHAR_T == 4 |
| 4332 | *p++ = (wchar_t)ch; |
| 4333 | #else |
| 4334 | /* compute and append the two surrogates: */ |
| 4335 | |
| 4336 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4337 | ch -= 0x10000; |
| 4338 | |
| 4339 | /* high surrogate = top 10 bits added to D800 */ |
| 4340 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4341 | |
| 4342 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4343 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4344 | #endif |
| 4345 | break; |
| 4346 | } |
| 4347 | s += n; |
| 4348 | continue; |
| 4349 | |
| 4350 | surrogateescape: |
| 4351 | *p++ = 0xDC00 + ch; |
| 4352 | s++; |
| 4353 | } |
| 4354 | *p = L'\0'; |
| 4355 | return unicode; |
| 4356 | } |
| 4357 | |
| 4358 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4359 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4360 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4361 | |
| 4362 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4363 | and allocate exactly as much space needed at the end. Else allocate the |
| 4364 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4365 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4366 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4367 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4368 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4369 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4370 | #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] | 4371 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4372 | Py_ssize_t i; /* index into s of next input byte */ |
| 4373 | PyObject *result; /* result string object */ |
| 4374 | char *p; /* next free byte in output buffer */ |
| 4375 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4376 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4377 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4378 | PyObject *errorHandler = NULL; |
| 4379 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4380 | int kind; |
| 4381 | void *data; |
| 4382 | Py_ssize_t size; |
| 4383 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4384 | #if SIZEOF_WCHAR_T == 2 |
| 4385 | Py_ssize_t wchar_offset = 0; |
| 4386 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4387 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4388 | if (!PyUnicode_Check(unicode)) { |
| 4389 | PyErr_BadArgument(); |
| 4390 | return NULL; |
| 4391 | } |
| 4392 | |
| 4393 | if (PyUnicode_READY(unicode) == -1) |
| 4394 | return NULL; |
| 4395 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4396 | if (PyUnicode_UTF8(unicode)) |
| 4397 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4398 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4399 | |
| 4400 | kind = PyUnicode_KIND(unicode); |
| 4401 | data = PyUnicode_DATA(unicode); |
| 4402 | size = PyUnicode_GET_LENGTH(unicode); |
| 4403 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4404 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4405 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4406 | if (size <= MAX_SHORT_UNICHARS) { |
| 4407 | /* Write into the stack buffer; nallocated can't overflow. |
| 4408 | * At the end, we'll allocate exactly as much heap space as it |
| 4409 | * turns out we need. |
| 4410 | */ |
| 4411 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4412 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4413 | p = stackbuf; |
| 4414 | } |
| 4415 | else { |
| 4416 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4417 | nallocated = size * 4; |
| 4418 | if (nallocated / 4 != size) /* overflow! */ |
| 4419 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4420 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4421 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4422 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4423 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4424 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4425 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4426 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4427 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4428 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4429 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4430 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4431 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4432 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4433 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4434 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4435 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4436 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4437 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4438 | Py_ssize_t newpos; |
| 4439 | PyObject *rep; |
| 4440 | Py_ssize_t repsize, k, startpos; |
| 4441 | startpos = i-1; |
| 4442 | #if SIZEOF_WCHAR_T == 2 |
| 4443 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4444 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4445 | rep = unicode_encode_call_errorhandler( |
| 4446 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4447 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4448 | &exc, startpos, startpos+1, &newpos); |
| 4449 | if (!rep) |
| 4450 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4451 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4452 | if (PyBytes_Check(rep)) |
| 4453 | repsize = PyBytes_GET_SIZE(rep); |
| 4454 | else |
| 4455 | repsize = PyUnicode_GET_SIZE(rep); |
| 4456 | |
| 4457 | if (repsize > 4) { |
| 4458 | Py_ssize_t offset; |
| 4459 | |
| 4460 | if (result == NULL) |
| 4461 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4462 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4463 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4464 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4465 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4466 | /* integer overflow */ |
| 4467 | PyErr_NoMemory(); |
| 4468 | goto error; |
| 4469 | } |
| 4470 | nallocated += repsize - 4; |
| 4471 | if (result != NULL) { |
| 4472 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4473 | goto error; |
| 4474 | } else { |
| 4475 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4476 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4477 | goto error; |
| 4478 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4479 | } |
| 4480 | p = PyBytes_AS_STRING(result) + offset; |
| 4481 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4482 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4483 | if (PyBytes_Check(rep)) { |
| 4484 | char *prep = PyBytes_AS_STRING(rep); |
| 4485 | for(k = repsize; k > 0; k--) |
| 4486 | *p++ = *prep++; |
| 4487 | } else /* rep is unicode */ { |
| 4488 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4489 | Py_UNICODE c; |
| 4490 | |
| 4491 | for(k=0; k<repsize; k++) { |
| 4492 | c = prep[k]; |
| 4493 | if (0x80 <= c) { |
| 4494 | raise_encode_exception(&exc, "utf-8", |
| 4495 | PyUnicode_AS_UNICODE(unicode), |
| 4496 | size, i-1, i, |
| 4497 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4498 | goto error; |
| 4499 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4500 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4501 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4502 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4503 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4504 | } else if (ch < 0x10000) { |
| 4505 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4506 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4507 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4508 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4509 | /* Encode UCS4 Unicode ordinals */ |
| 4510 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4511 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4512 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4513 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4514 | #if SIZEOF_WCHAR_T == 2 |
| 4515 | wchar_offset++; |
| 4516 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4517 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4518 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4519 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4520 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4521 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4522 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4523 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4524 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4525 | } |
| 4526 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4527 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4528 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4529 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4530 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4531 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4532 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4533 | Py_XDECREF(errorHandler); |
| 4534 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4535 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4536 | error: |
| 4537 | Py_XDECREF(errorHandler); |
| 4538 | Py_XDECREF(exc); |
| 4539 | Py_XDECREF(result); |
| 4540 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4541 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4542 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4545 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4546 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4547 | Py_ssize_t size, |
| 4548 | const char *errors) |
| 4549 | { |
| 4550 | PyObject *v, *unicode; |
| 4551 | |
| 4552 | unicode = PyUnicode_FromUnicode(s, size); |
| 4553 | if (unicode == NULL) |
| 4554 | return NULL; |
| 4555 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4556 | Py_DECREF(unicode); |
| 4557 | return v; |
| 4558 | } |
| 4559 | |
| 4560 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4561 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4562 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4563 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4566 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4567 | |
| 4568 | PyObject * |
| 4569 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4570 | Py_ssize_t size, |
| 4571 | const char *errors, |
| 4572 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4573 | { |
| 4574 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4575 | } |
| 4576 | |
| 4577 | PyObject * |
| 4578 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4579 | Py_ssize_t size, |
| 4580 | const char *errors, |
| 4581 | int *byteorder, |
| 4582 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4583 | { |
| 4584 | const char *starts = s; |
| 4585 | Py_ssize_t startinpos; |
| 4586 | Py_ssize_t endinpos; |
| 4587 | Py_ssize_t outpos; |
| 4588 | PyUnicodeObject *unicode; |
| 4589 | Py_UNICODE *p; |
| 4590 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4591 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4592 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4593 | #else |
| 4594 | const int pairs = 0; |
| 4595 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4596 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4597 | int bo = 0; /* assume native ordering by default */ |
| 4598 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4599 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4600 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4601 | int iorder[] = {0, 1, 2, 3}; |
| 4602 | #else |
| 4603 | int iorder[] = {3, 2, 1, 0}; |
| 4604 | #endif |
| 4605 | PyObject *errorHandler = NULL; |
| 4606 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4607 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4608 | q = (unsigned char *)s; |
| 4609 | e = q + size; |
| 4610 | |
| 4611 | if (byteorder) |
| 4612 | bo = *byteorder; |
| 4613 | |
| 4614 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4615 | byte order setting accordingly. In native mode, the leading BOM |
| 4616 | mark is skipped, in all other modes, it is copied to the output |
| 4617 | stream as-is (giving a ZWNBSP character). */ |
| 4618 | if (bo == 0) { |
| 4619 | if (size >= 4) { |
| 4620 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4621 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4622 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4623 | if (bom == 0x0000FEFF) { |
| 4624 | q += 4; |
| 4625 | bo = -1; |
| 4626 | } |
| 4627 | else if (bom == 0xFFFE0000) { |
| 4628 | q += 4; |
| 4629 | bo = 1; |
| 4630 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4631 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4632 | if (bom == 0x0000FEFF) { |
| 4633 | q += 4; |
| 4634 | bo = 1; |
| 4635 | } |
| 4636 | else if (bom == 0xFFFE0000) { |
| 4637 | q += 4; |
| 4638 | bo = -1; |
| 4639 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4640 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4641 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | if (bo == -1) { |
| 4645 | /* force LE */ |
| 4646 | iorder[0] = 0; |
| 4647 | iorder[1] = 1; |
| 4648 | iorder[2] = 2; |
| 4649 | iorder[3] = 3; |
| 4650 | } |
| 4651 | else if (bo == 1) { |
| 4652 | /* force BE */ |
| 4653 | iorder[0] = 3; |
| 4654 | iorder[1] = 2; |
| 4655 | iorder[2] = 1; |
| 4656 | iorder[3] = 0; |
| 4657 | } |
| 4658 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4659 | /* On narrow builds we split characters outside the BMP into two |
| 4660 | codepoints => count how much extra space we need. */ |
| 4661 | #ifndef Py_UNICODE_WIDE |
| 4662 | for (qq = q; qq < e; qq += 4) |
| 4663 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4664 | pairs++; |
| 4665 | #endif |
| 4666 | |
| 4667 | /* This might be one to much, because of a BOM */ |
| 4668 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4669 | if (!unicode) |
| 4670 | return NULL; |
| 4671 | if (size == 0) |
| 4672 | return (PyObject *)unicode; |
| 4673 | |
| 4674 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4675 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4676 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4677 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4678 | Py_UCS4 ch; |
| 4679 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4680 | if (e-q<4) { |
| 4681 | if (consumed) |
| 4682 | break; |
| 4683 | errmsg = "truncated data"; |
| 4684 | startinpos = ((const char *)q)-starts; |
| 4685 | endinpos = ((const char *)e)-starts; |
| 4686 | goto utf32Error; |
| 4687 | /* The remaining input chars are ignored if the callback |
| 4688 | chooses to skip the input */ |
| 4689 | } |
| 4690 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4691 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4692 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4693 | if (ch >= 0x110000) |
| 4694 | { |
| 4695 | errmsg = "codepoint not in range(0x110000)"; |
| 4696 | startinpos = ((const char *)q)-starts; |
| 4697 | endinpos = startinpos+4; |
| 4698 | goto utf32Error; |
| 4699 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4700 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4701 | if (ch >= 0x10000) |
| 4702 | { |
| 4703 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4704 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4705 | } |
| 4706 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4707 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4708 | *p++ = ch; |
| 4709 | q += 4; |
| 4710 | continue; |
| 4711 | utf32Error: |
| 4712 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4713 | if (unicode_decode_call_errorhandler( |
| 4714 | errors, &errorHandler, |
| 4715 | "utf32", errmsg, |
| 4716 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4717 | &unicode, &outpos, &p)) |
| 4718 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4719 | } |
| 4720 | |
| 4721 | if (byteorder) |
| 4722 | *byteorder = bo; |
| 4723 | |
| 4724 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4725 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4726 | |
| 4727 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 4728 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4729 | goto onError; |
| 4730 | |
| 4731 | Py_XDECREF(errorHandler); |
| 4732 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4733 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4734 | Py_DECREF(unicode); |
| 4735 | return NULL; |
| 4736 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4737 | return (PyObject *)unicode; |
| 4738 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4739 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4740 | Py_DECREF(unicode); |
| 4741 | Py_XDECREF(errorHandler); |
| 4742 | Py_XDECREF(exc); |
| 4743 | return NULL; |
| 4744 | } |
| 4745 | |
| 4746 | PyObject * |
| 4747 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4748 | Py_ssize_t size, |
| 4749 | const char *errors, |
| 4750 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4751 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4752 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4753 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4754 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4755 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4756 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4757 | #else |
| 4758 | const int pairs = 0; |
| 4759 | #endif |
| 4760 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4761 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4762 | int iorder[] = {0, 1, 2, 3}; |
| 4763 | #else |
| 4764 | int iorder[] = {3, 2, 1, 0}; |
| 4765 | #endif |
| 4766 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4767 | #define STORECHAR(CH) \ |
| 4768 | do { \ |
| 4769 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4770 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4771 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4772 | p[iorder[0]] = (CH) & 0xff; \ |
| 4773 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4774 | } while(0) |
| 4775 | |
| 4776 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 4777 | so we need less space. */ |
| 4778 | #ifndef Py_UNICODE_WIDE |
| 4779 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4780 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 4781 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 4782 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4783 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4784 | nsize = (size - pairs + (byteorder == 0)); |
| 4785 | bytesize = nsize * 4; |
| 4786 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4787 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4788 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4789 | if (v == NULL) |
| 4790 | return NULL; |
| 4791 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4792 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4793 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4794 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4795 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4796 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4797 | |
| 4798 | if (byteorder == -1) { |
| 4799 | /* force LE */ |
| 4800 | iorder[0] = 0; |
| 4801 | iorder[1] = 1; |
| 4802 | iorder[2] = 2; |
| 4803 | iorder[3] = 3; |
| 4804 | } |
| 4805 | else if (byteorder == 1) { |
| 4806 | /* force BE */ |
| 4807 | iorder[0] = 3; |
| 4808 | iorder[1] = 2; |
| 4809 | iorder[2] = 1; |
| 4810 | iorder[3] = 0; |
| 4811 | } |
| 4812 | |
| 4813 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4814 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4815 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4816 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 4817 | Py_UCS4 ch2 = *s; |
| 4818 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 4819 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 4820 | s++; |
| 4821 | size--; |
| 4822 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4823 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4824 | #endif |
| 4825 | STORECHAR(ch); |
| 4826 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4827 | |
| 4828 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4829 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4830 | #undef STORECHAR |
| 4831 | } |
| 4832 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4833 | PyObject * |
| 4834 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4835 | { |
| 4836 | if (!PyUnicode_Check(unicode)) { |
| 4837 | PyErr_BadArgument(); |
| 4838 | return NULL; |
| 4839 | } |
| 4840 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4841 | PyUnicode_GET_SIZE(unicode), |
| 4842 | NULL, |
| 4843 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4844 | } |
| 4845 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4846 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 4847 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4848 | PyObject * |
| 4849 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4850 | Py_ssize_t size, |
| 4851 | const char *errors, |
| 4852 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4853 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4854 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 4855 | } |
| 4856 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4857 | /* Two masks for fast checking of whether a C 'long' may contain |
| 4858 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 4859 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 4860 | rare in most input. |
| 4861 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 4862 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4863 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4864 | #if (SIZEOF_LONG == 8) |
| 4865 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 4866 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 4867 | #elif (SIZEOF_LONG == 4) |
| 4868 | # define FAST_CHAR_MASK 0x80008000L |
| 4869 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 4870 | #else |
| 4871 | # error C 'long' size should be either 4 or 8! |
| 4872 | #endif |
| 4873 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4874 | PyObject * |
| 4875 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4876 | Py_ssize_t size, |
| 4877 | const char *errors, |
| 4878 | int *byteorder, |
| 4879 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4880 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4881 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4882 | Py_ssize_t startinpos; |
| 4883 | Py_ssize_t endinpos; |
| 4884 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4885 | PyUnicodeObject *unicode; |
| 4886 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4887 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4888 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4889 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4890 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4891 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 4892 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4893 | int ihi = 1, ilo = 0; |
| 4894 | #else |
| 4895 | int ihi = 0, ilo = 1; |
| 4896 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4897 | PyObject *errorHandler = NULL; |
| 4898 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4899 | |
| 4900 | /* Note: size will always be longer than the resulting Unicode |
| 4901 | character count */ |
| 4902 | unicode = _PyUnicode_New(size); |
| 4903 | if (!unicode) |
| 4904 | return NULL; |
| 4905 | if (size == 0) |
| 4906 | return (PyObject *)unicode; |
| 4907 | |
| 4908 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4909 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4910 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4911 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4912 | |
| 4913 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4914 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4915 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4916 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4917 | byte order setting accordingly. In native mode, the leading BOM |
| 4918 | mark is skipped, in all other modes, it is copied to the output |
| 4919 | stream as-is (giving a ZWNBSP character). */ |
| 4920 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4921 | if (size >= 2) { |
| 4922 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4923 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4924 | if (bom == 0xFEFF) { |
| 4925 | q += 2; |
| 4926 | bo = -1; |
| 4927 | } |
| 4928 | else if (bom == 0xFFFE) { |
| 4929 | q += 2; |
| 4930 | bo = 1; |
| 4931 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4932 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4933 | if (bom == 0xFEFF) { |
| 4934 | q += 2; |
| 4935 | bo = 1; |
| 4936 | } |
| 4937 | else if (bom == 0xFFFE) { |
| 4938 | q += 2; |
| 4939 | bo = -1; |
| 4940 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4941 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4942 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4943 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4944 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4945 | if (bo == -1) { |
| 4946 | /* force LE */ |
| 4947 | ihi = 1; |
| 4948 | ilo = 0; |
| 4949 | } |
| 4950 | else if (bo == 1) { |
| 4951 | /* force BE */ |
| 4952 | ihi = 0; |
| 4953 | ilo = 1; |
| 4954 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4955 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4956 | native_ordering = ilo < ihi; |
| 4957 | #else |
| 4958 | native_ordering = ilo > ihi; |
| 4959 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4960 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4961 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4962 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4963 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4964 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 4965 | reads are more expensive, better to defer to another iteration. */ |
| 4966 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 4967 | /* Fast path for runs of non-surrogate chars. */ |
| 4968 | register const unsigned char *_q = q; |
| 4969 | Py_UNICODE *_p = p; |
| 4970 | if (native_ordering) { |
| 4971 | /* Native ordering is simple: as long as the input cannot |
| 4972 | possibly contain a surrogate char, do an unrolled copy |
| 4973 | of several 16-bit code points to the target object. |
| 4974 | The non-surrogate check is done on several input bytes |
| 4975 | at a time (as many as a C 'long' can contain). */ |
| 4976 | while (_q < aligned_end) { |
| 4977 | unsigned long data = * (unsigned long *) _q; |
| 4978 | if (data & FAST_CHAR_MASK) |
| 4979 | break; |
| 4980 | _p[0] = ((unsigned short *) _q)[0]; |
| 4981 | _p[1] = ((unsigned short *) _q)[1]; |
| 4982 | #if (SIZEOF_LONG == 8) |
| 4983 | _p[2] = ((unsigned short *) _q)[2]; |
| 4984 | _p[3] = ((unsigned short *) _q)[3]; |
| 4985 | #endif |
| 4986 | _q += SIZEOF_LONG; |
| 4987 | _p += SIZEOF_LONG / 2; |
| 4988 | } |
| 4989 | } |
| 4990 | else { |
| 4991 | /* Byteswapped ordering is similar, but we must decompose |
| 4992 | the copy bytewise, and take care of zero'ing out the |
| 4993 | upper bytes if the target object is in 32-bit units |
| 4994 | (that is, in UCS-4 builds). */ |
| 4995 | while (_q < aligned_end) { |
| 4996 | unsigned long data = * (unsigned long *) _q; |
| 4997 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 4998 | break; |
| 4999 | /* Zero upper bytes in UCS-4 builds */ |
| 5000 | #if (Py_UNICODE_SIZE > 2) |
| 5001 | _p[0] = 0; |
| 5002 | _p[1] = 0; |
| 5003 | #if (SIZEOF_LONG == 8) |
| 5004 | _p[2] = 0; |
| 5005 | _p[3] = 0; |
| 5006 | #endif |
| 5007 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5008 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 5009 | fill the two last bytes of each 4-byte unit. */ |
| 5010 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 5011 | # define OFF 2 |
| 5012 | #else |
| 5013 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5014 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5015 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 5016 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 5017 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 5018 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 5019 | #if (SIZEOF_LONG == 8) |
| 5020 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 5021 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 5022 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 5023 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 5024 | #endif |
| 5025 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5026 | _q += SIZEOF_LONG; |
| 5027 | _p += SIZEOF_LONG / 2; |
| 5028 | } |
| 5029 | } |
| 5030 | p = _p; |
| 5031 | q = _q; |
| 5032 | if (q >= e) |
| 5033 | break; |
| 5034 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5035 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5036 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5037 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5038 | |
| 5039 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5040 | *p++ = ch; |
| 5041 | continue; |
| 5042 | } |
| 5043 | |
| 5044 | /* UTF-16 code pair: */ |
| 5045 | if (q > e) { |
| 5046 | errmsg = "unexpected end of data"; |
| 5047 | startinpos = (((const char *)q) - 2) - starts; |
| 5048 | endinpos = ((const char *)e) + 1 - starts; |
| 5049 | goto utf16Error; |
| 5050 | } |
| 5051 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5052 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5053 | q += 2; |
| 5054 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5055 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5056 | *p++ = ch; |
| 5057 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5058 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5059 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5060 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5061 | continue; |
| 5062 | } |
| 5063 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5064 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | startinpos = (((const char *)q)-4)-starts; |
| 5066 | endinpos = startinpos+2; |
| 5067 | goto utf16Error; |
| 5068 | } |
| 5069 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5070 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5071 | errmsg = "illegal encoding"; |
| 5072 | startinpos = (((const char *)q)-2)-starts; |
| 5073 | endinpos = startinpos+2; |
| 5074 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5075 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5076 | utf16Error: |
| 5077 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5078 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5079 | errors, |
| 5080 | &errorHandler, |
| 5081 | "utf16", errmsg, |
| 5082 | &starts, |
| 5083 | (const char **)&e, |
| 5084 | &startinpos, |
| 5085 | &endinpos, |
| 5086 | &exc, |
| 5087 | (const char **)&q, |
| 5088 | &unicode, |
| 5089 | &outpos, |
| 5090 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5091 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5092 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5093 | /* remaining byte at the end? (size should be even) */ |
| 5094 | if (e == q) { |
| 5095 | if (!consumed) { |
| 5096 | errmsg = "truncated data"; |
| 5097 | startinpos = ((const char *)q) - starts; |
| 5098 | endinpos = ((const char *)e) + 1 - starts; |
| 5099 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5100 | if (unicode_decode_call_errorhandler( |
| 5101 | errors, |
| 5102 | &errorHandler, |
| 5103 | "utf16", errmsg, |
| 5104 | &starts, |
| 5105 | (const char **)&e, |
| 5106 | &startinpos, |
| 5107 | &endinpos, |
| 5108 | &exc, |
| 5109 | (const char **)&q, |
| 5110 | &unicode, |
| 5111 | &outpos, |
| 5112 | &p)) |
| 5113 | goto onError; |
| 5114 | /* The remaining input chars are ignored if the callback |
| 5115 | chooses to skip the input */ |
| 5116 | } |
| 5117 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5118 | |
| 5119 | if (byteorder) |
| 5120 | *byteorder = bo; |
| 5121 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5122 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5123 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5125 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5126 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5127 | goto onError; |
| 5128 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5129 | Py_XDECREF(errorHandler); |
| 5130 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5131 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5132 | Py_DECREF(unicode); |
| 5133 | return NULL; |
| 5134 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5135 | return (PyObject *)unicode; |
| 5136 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5137 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5138 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5139 | Py_XDECREF(errorHandler); |
| 5140 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5141 | return NULL; |
| 5142 | } |
| 5143 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5144 | #undef FAST_CHAR_MASK |
| 5145 | #undef SWAPPED_FAST_CHAR_MASK |
| 5146 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5147 | PyObject * |
| 5148 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5149 | Py_ssize_t size, |
| 5150 | const char *errors, |
| 5151 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5152 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5153 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5154 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5155 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5156 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5157 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5158 | #else |
| 5159 | const int pairs = 0; |
| 5160 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5161 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5162 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5163 | int ihi = 1, ilo = 0; |
| 5164 | #else |
| 5165 | int ihi = 0, ilo = 1; |
| 5166 | #endif |
| 5167 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5168 | #define STORECHAR(CH) \ |
| 5169 | do { \ |
| 5170 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5171 | p[ilo] = (CH) & 0xff; \ |
| 5172 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5173 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5174 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5175 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5176 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5177 | if (s[i] >= 0x10000) |
| 5178 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5179 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5180 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5181 | if (size > PY_SSIZE_T_MAX || |
| 5182 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5183 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5184 | nsize = size + pairs + (byteorder == 0); |
| 5185 | bytesize = nsize * 2; |
| 5186 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5187 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5188 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5189 | if (v == NULL) |
| 5190 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5191 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5192 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5193 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5194 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5195 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5196 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5197 | |
| 5198 | if (byteorder == -1) { |
| 5199 | /* force LE */ |
| 5200 | ihi = 1; |
| 5201 | ilo = 0; |
| 5202 | } |
| 5203 | else if (byteorder == 1) { |
| 5204 | /* force BE */ |
| 5205 | ihi = 0; |
| 5206 | ilo = 1; |
| 5207 | } |
| 5208 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5209 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5210 | Py_UNICODE ch = *s++; |
| 5211 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5212 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5213 | if (ch >= 0x10000) { |
| 5214 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5215 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5216 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5217 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5218 | STORECHAR(ch); |
| 5219 | if (ch2) |
| 5220 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5221 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5222 | |
| 5223 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5224 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5225 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5226 | } |
| 5227 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5228 | PyObject * |
| 5229 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5230 | { |
| 5231 | if (!PyUnicode_Check(unicode)) { |
| 5232 | PyErr_BadArgument(); |
| 5233 | return NULL; |
| 5234 | } |
| 5235 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5236 | PyUnicode_GET_SIZE(unicode), |
| 5237 | NULL, |
| 5238 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
| 5241 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5242 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5243 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5244 | if all the escapes in the string make it still a valid ASCII string. |
| 5245 | Returns -1 if any escapes were found which cause the string to |
| 5246 | pop out of ASCII range. Otherwise returns the length of the |
| 5247 | required buffer to hold the string. |
| 5248 | */ |
| 5249 | Py_ssize_t |
| 5250 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5251 | { |
| 5252 | const unsigned char *p = (const unsigned char *)s; |
| 5253 | const unsigned char *end = p + size; |
| 5254 | Py_ssize_t length = 0; |
| 5255 | |
| 5256 | if (size < 0) |
| 5257 | return -1; |
| 5258 | |
| 5259 | for (; p < end; ++p) { |
| 5260 | if (*p > 127) { |
| 5261 | /* Non-ASCII */ |
| 5262 | return -1; |
| 5263 | } |
| 5264 | else if (*p != '\\') { |
| 5265 | /* Normal character */ |
| 5266 | ++length; |
| 5267 | } |
| 5268 | else { |
| 5269 | /* Backslash-escape, check next char */ |
| 5270 | ++p; |
| 5271 | /* Escape sequence reaches till end of string or |
| 5272 | non-ASCII follow-up. */ |
| 5273 | if (p >= end || *p > 127) |
| 5274 | return -1; |
| 5275 | switch (*p) { |
| 5276 | case '\n': |
| 5277 | /* backslash + \n result in zero characters */ |
| 5278 | break; |
| 5279 | case '\\': case '\'': case '\"': |
| 5280 | case 'b': case 'f': case 't': |
| 5281 | case 'n': case 'r': case 'v': case 'a': |
| 5282 | ++length; |
| 5283 | break; |
| 5284 | case '0': case '1': case '2': case '3': |
| 5285 | case '4': case '5': case '6': case '7': |
| 5286 | case 'x': case 'u': case 'U': case 'N': |
| 5287 | /* these do not guarantee ASCII characters */ |
| 5288 | return -1; |
| 5289 | default: |
| 5290 | /* count the backslash + the other character */ |
| 5291 | length += 2; |
| 5292 | } |
| 5293 | } |
| 5294 | } |
| 5295 | return length; |
| 5296 | } |
| 5297 | |
| 5298 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5299 | or treat string as ASCII. */ |
| 5300 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5301 | do { \ |
| 5302 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5303 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5304 | else \ |
| 5305 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5306 | } while (0) |
| 5307 | |
| 5308 | #define WRITE_WSTR(buf, index, value) \ |
| 5309 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5310 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5311 | |
| 5312 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5313 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5314 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5315 | PyObject * |
| 5316 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5317 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5318 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5319 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5320 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5321 | Py_ssize_t startinpos; |
| 5322 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5323 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5324 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5325 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5326 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5327 | char* message; |
| 5328 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5329 | PyObject *errorHandler = NULL; |
| 5330 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5331 | Py_ssize_t ascii_length; |
| 5332 | Py_ssize_t i; |
| 5333 | int kind; |
| 5334 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5335 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5336 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5337 | |
| 5338 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5339 | either the string is pure ASCII with named escapes like \n, etc. |
| 5340 | and we determined it's exact size (common case) |
| 5341 | or it contains \x, \u, ... escape sequences. then we create a |
| 5342 | legacy wchar string and resize it at the end of this function. */ |
| 5343 | if (ascii_length >= 0) { |
| 5344 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 5345 | if (!v) |
| 5346 | goto onError; |
| 5347 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5348 | kind = PyUnicode_1BYTE_KIND; |
| 5349 | data = PyUnicode_DATA(v); |
| 5350 | } |
| 5351 | else { |
| 5352 | /* Escaped strings will always be longer than the resulting |
| 5353 | Unicode string, so we start with size here and then reduce the |
| 5354 | length after conversion to the true value. |
| 5355 | (but if the error callback returns a long replacement string |
| 5356 | we'll have to allocate more space) */ |
| 5357 | v = _PyUnicode_New(size); |
| 5358 | if (!v) |
| 5359 | goto onError; |
| 5360 | kind = PyUnicode_WCHAR_KIND; |
| 5361 | data = PyUnicode_AS_UNICODE(v); |
| 5362 | } |
| 5363 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5364 | if (size == 0) |
| 5365 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5366 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5367 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5368 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5369 | while (s < end) { |
| 5370 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5371 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5372 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5373 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5374 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5375 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5376 | } |
| 5377 | else { |
| 5378 | /* The only case in which i == ascii_length is a backslash |
| 5379 | followed by a newline. */ |
| 5380 | assert(i <= ascii_length); |
| 5381 | } |
| 5382 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5383 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5384 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5385 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | continue; |
| 5387 | } |
| 5388 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5389 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5390 | /* \ - Escapes */ |
| 5391 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5392 | c = *s++; |
| 5393 | if (s > end) |
| 5394 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5395 | |
| 5396 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5397 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5398 | } |
| 5399 | else { |
| 5400 | /* The only case in which i == ascii_length is a backslash |
| 5401 | followed by a newline. */ |
| 5402 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5403 | } |
| 5404 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5405 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5406 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5407 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5408 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5409 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5410 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5411 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5412 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5413 | /* FF */ |
| 5414 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5415 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5416 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5417 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5418 | /* VT */ |
| 5419 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5420 | /* BEL, not classic C */ |
| 5421 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5423 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5424 | case '0': case '1': case '2': case '3': |
| 5425 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5426 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5427 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5428 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5429 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5430 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5431 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5432 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5433 | break; |
| 5434 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5435 | /* hex escapes */ |
| 5436 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5437 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5438 | digits = 2; |
| 5439 | message = "truncated \\xXX escape"; |
| 5440 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5442 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5443 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5444 | digits = 4; |
| 5445 | message = "truncated \\uXXXX escape"; |
| 5446 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5448 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5449 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5450 | digits = 8; |
| 5451 | message = "truncated \\UXXXXXXXX escape"; |
| 5452 | hexescape: |
| 5453 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5454 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5455 | if (s+digits>end) { |
| 5456 | endinpos = size; |
| 5457 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5458 | errors, &errorHandler, |
| 5459 | "unicodeescape", "end of string in escape sequence", |
| 5460 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5461 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5462 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5463 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5464 | goto nextByte; |
| 5465 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5466 | for (j = 0; j < digits; ++j) { |
| 5467 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5468 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5469 | endinpos = (s+j+1)-starts; |
| 5470 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5471 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5472 | errors, &errorHandler, |
| 5473 | "unicodeescape", message, |
| 5474 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5475 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5476 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5477 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5478 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5479 | } |
| 5480 | chr = (chr<<4) & ~0xF; |
| 5481 | if (c >= '0' && c <= '9') |
| 5482 | chr += c - '0'; |
| 5483 | else if (c >= 'a' && c <= 'f') |
| 5484 | chr += 10 + c - 'a'; |
| 5485 | else |
| 5486 | chr += 10 + c - 'A'; |
| 5487 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5488 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5489 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5490 | /* _decoding_error will have already written into the |
| 5491 | target buffer. */ |
| 5492 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5493 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5494 | /* when we get here, chr is a 32-bit unicode character */ |
| 5495 | if (chr <= 0xffff) |
| 5496 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5497 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5498 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5499 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5500 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5501 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5502 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5503 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5504 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5505 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5506 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5507 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5508 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5509 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5510 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5511 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5512 | errors, &errorHandler, |
| 5513 | "unicodeescape", "illegal Unicode character", |
| 5514 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5515 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5516 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5517 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5518 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5519 | break; |
| 5520 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5521 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5522 | case 'N': |
| 5523 | message = "malformed \\N character escape"; |
| 5524 | if (ucnhash_CAPI == NULL) { |
| 5525 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5526 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5527 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5528 | if (ucnhash_CAPI == NULL) |
| 5529 | goto ucnhashError; |
| 5530 | } |
| 5531 | if (*s == '{') { |
| 5532 | const char *start = s+1; |
| 5533 | /* look for the closing brace */ |
| 5534 | while (*s != '}' && s < end) |
| 5535 | s++; |
| 5536 | if (s > start && s < end && *s == '}') { |
| 5537 | /* found a name. look it up in the unicode database */ |
| 5538 | message = "unknown Unicode character name"; |
| 5539 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5540 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5541 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5542 | goto store; |
| 5543 | } |
| 5544 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5545 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5546 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5547 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5548 | errors, &errorHandler, |
| 5549 | "unicodeescape", message, |
| 5550 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5551 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5552 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5553 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5554 | break; |
| 5555 | |
| 5556 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5557 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5558 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5559 | message = "\\ at end of string"; |
| 5560 | s--; |
| 5561 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5562 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5563 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5564 | errors, &errorHandler, |
| 5565 | "unicodeescape", message, |
| 5566 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5567 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5568 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5569 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5570 | } |
| 5571 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5572 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5573 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5574 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5575 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5576 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5577 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5578 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5579 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5580 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5581 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5582 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5583 | if (kind == PyUnicode_WCHAR_KIND) |
| 5584 | { |
| 5585 | if (PyUnicode_Resize((PyObject**)&v, i) < 0) |
| 5586 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5587 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5588 | Py_XDECREF(errorHandler); |
| 5589 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5590 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5591 | Py_DECREF(v); |
| 5592 | return NULL; |
| 5593 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5594 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5595 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5596 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5597 | PyErr_SetString( |
| 5598 | PyExc_UnicodeError, |
| 5599 | "\\N escapes not supported (can't load unicodedata module)" |
| 5600 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5601 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5602 | Py_XDECREF(errorHandler); |
| 5603 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5604 | return NULL; |
| 5605 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5606 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5607 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5608 | Py_XDECREF(errorHandler); |
| 5609 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5610 | return NULL; |
| 5611 | } |
| 5612 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5613 | #undef WRITE_ASCII_OR_WSTR |
| 5614 | #undef WRITE_WSTR |
| 5615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5616 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5617 | |
| 5618 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5619 | appropriate. |
| 5620 | |
| 5621 | */ |
| 5622 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5623 | static const char *hexdigits = "0123456789abcdef"; |
| 5624 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5625 | PyObject * |
| 5626 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5627 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5628 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5629 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5630 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5632 | #ifdef Py_UNICODE_WIDE |
| 5633 | const Py_ssize_t expandsize = 10; |
| 5634 | #else |
| 5635 | const Py_ssize_t expandsize = 6; |
| 5636 | #endif |
| 5637 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5638 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5639 | better to choose a different scheme. Perhaps scan the |
| 5640 | first N-chars of the string and allocate based on that size. |
| 5641 | */ |
| 5642 | /* Initial allocation is based on the longest-possible unichr |
| 5643 | escape. |
| 5644 | |
| 5645 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5646 | unichr, so in this case it's the longest unichr escape. In |
| 5647 | narrow (UTF-16) builds this is five chars per source unichr |
| 5648 | since there are two unichrs in the surrogate pair, so in narrow |
| 5649 | (UTF-16) builds it's not the longest unichr escape. |
| 5650 | |
| 5651 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5652 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5653 | escape. |
| 5654 | */ |
| 5655 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5656 | if (size == 0) |
| 5657 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5658 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5659 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5660 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5661 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5662 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5663 | 2 |
| 5664 | + expandsize*size |
| 5665 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | if (repr == NULL) |
| 5667 | return NULL; |
| 5668 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5669 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5671 | while (size-- > 0) { |
| 5672 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5673 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5674 | /* Escape backslashes */ |
| 5675 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 | *p++ = '\\'; |
| 5677 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5678 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5679 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5680 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5681 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5682 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5683 | else if (ch >= 0x10000) { |
| 5684 | *p++ = '\\'; |
| 5685 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5686 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5687 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5688 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5689 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5690 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5691 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5692 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5693 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5694 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5695 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5696 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5697 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5698 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5699 | Py_UNICODE ch2; |
| 5700 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5701 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5702 | ch2 = *s++; |
| 5703 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5704 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5705 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5706 | *p++ = '\\'; |
| 5707 | *p++ = 'U'; |
| 5708 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5709 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5710 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5711 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5712 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5713 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5714 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5715 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5716 | continue; |
| 5717 | } |
| 5718 | /* Fall through: isolated surrogates are copied as-is */ |
| 5719 | s--; |
| 5720 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5721 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5722 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5723 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5724 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5725 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5726 | *p++ = '\\'; |
| 5727 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5728 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5729 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5730 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5731 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5732 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5733 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5734 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5735 | else if (ch == '\t') { |
| 5736 | *p++ = '\\'; |
| 5737 | *p++ = 't'; |
| 5738 | } |
| 5739 | else if (ch == '\n') { |
| 5740 | *p++ = '\\'; |
| 5741 | *p++ = 'n'; |
| 5742 | } |
| 5743 | else if (ch == '\r') { |
| 5744 | *p++ = '\\'; |
| 5745 | *p++ = 'r'; |
| 5746 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5747 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5748 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5749 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5750 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5751 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5752 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5753 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5754 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5756 | /* Copy everything else as-is */ |
| 5757 | else |
| 5758 | *p++ = (char) ch; |
| 5759 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5760 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5761 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5762 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5763 | return NULL; |
| 5764 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5767 | PyObject * |
| 5768 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5769 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5770 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5771 | if (!PyUnicode_Check(unicode)) { |
| 5772 | PyErr_BadArgument(); |
| 5773 | return NULL; |
| 5774 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5775 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5776 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5777 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5778 | } |
| 5779 | |
| 5780 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5781 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5782 | PyObject * |
| 5783 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5784 | Py_ssize_t size, |
| 5785 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5786 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5787 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5788 | Py_ssize_t startinpos; |
| 5789 | Py_ssize_t endinpos; |
| 5790 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5791 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5792 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5793 | const char *end; |
| 5794 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5795 | PyObject *errorHandler = NULL; |
| 5796 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5797 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5798 | /* Escaped strings will always be longer than the resulting |
| 5799 | 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] | 5800 | length after conversion to the true value. (But decoding error |
| 5801 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5802 | v = _PyUnicode_New(size); |
| 5803 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5804 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5805 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5806 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5807 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5808 | end = s + size; |
| 5809 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5810 | unsigned char c; |
| 5811 | Py_UCS4 x; |
| 5812 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5813 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5814 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5815 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5816 | if (*s != '\\') { |
| 5817 | *p++ = (unsigned char)*s++; |
| 5818 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5819 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5820 | startinpos = s-starts; |
| 5821 | |
| 5822 | /* \u-escapes are only interpreted iff the number of leading |
| 5823 | backslashes if odd */ |
| 5824 | bs = s; |
| 5825 | for (;s < end;) { |
| 5826 | if (*s != '\\') |
| 5827 | break; |
| 5828 | *p++ = (unsigned char)*s++; |
| 5829 | } |
| 5830 | if (((s - bs) & 1) == 0 || |
| 5831 | s >= end || |
| 5832 | (*s != 'u' && *s != 'U')) { |
| 5833 | continue; |
| 5834 | } |
| 5835 | p--; |
| 5836 | count = *s=='u' ? 4 : 8; |
| 5837 | s++; |
| 5838 | |
| 5839 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 5840 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5841 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5842 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5843 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5844 | endinpos = s-starts; |
| 5845 | if (unicode_decode_call_errorhandler( |
| 5846 | errors, &errorHandler, |
| 5847 | "rawunicodeescape", "truncated \\uXXXX", |
| 5848 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5849 | &v, &outpos, &p)) |
| 5850 | goto onError; |
| 5851 | goto nextByte; |
| 5852 | } |
| 5853 | x = (x<<4) & ~0xF; |
| 5854 | if (c >= '0' && c <= '9') |
| 5855 | x += c - '0'; |
| 5856 | else if (c >= 'a' && c <= 'f') |
| 5857 | x += 10 + c - 'a'; |
| 5858 | else |
| 5859 | x += 10 + c - 'A'; |
| 5860 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5861 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5862 | /* UCS-2 character */ |
| 5863 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5864 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5865 | /* UCS-4 character. Either store directly, or as |
| 5866 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5867 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5868 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5869 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5870 | x -= 0x10000L; |
| 5871 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 5872 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5873 | #endif |
| 5874 | } else { |
| 5875 | endinpos = s-starts; |
| 5876 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5877 | if (unicode_decode_call_errorhandler( |
| 5878 | errors, &errorHandler, |
| 5879 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5880 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5881 | &v, &outpos, &p)) |
| 5882 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5883 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5884 | nextByte: |
| 5885 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5886 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5887 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5888 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5889 | Py_XDECREF(errorHandler); |
| 5890 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5891 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5892 | Py_DECREF(v); |
| 5893 | return NULL; |
| 5894 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5895 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5896 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5897 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5898 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5899 | Py_XDECREF(errorHandler); |
| 5900 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5901 | return NULL; |
| 5902 | } |
| 5903 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5904 | PyObject * |
| 5905 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5906 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5908 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5909 | char *p; |
| 5910 | char *q; |
| 5911 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5912 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5913 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5914 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5915 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5916 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5917 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5918 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5919 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5920 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5921 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5922 | if (repr == NULL) |
| 5923 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5924 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5925 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5927 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5928 | while (size-- > 0) { |
| 5929 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5930 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5931 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5932 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5933 | *p++ = '\\'; |
| 5934 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5935 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 5936 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 5937 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 5938 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 5939 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5940 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5941 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5942 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5943 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5944 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5945 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5946 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5947 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 5948 | Py_UNICODE ch2; |
| 5949 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5950 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5951 | ch2 = *s++; |
| 5952 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5953 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5954 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5955 | *p++ = '\\'; |
| 5956 | *p++ = 'U'; |
| 5957 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 5958 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 5959 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 5960 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 5961 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 5962 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 5963 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 5964 | *p++ = hexdigits[ucs & 0xf]; |
| 5965 | continue; |
| 5966 | } |
| 5967 | /* Fall through: isolated surrogates are copied as-is */ |
| 5968 | s--; |
| 5969 | size++; |
| 5970 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5971 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5972 | /* Map 16-bit characters to '\uxxxx' */ |
| 5973 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5974 | *p++ = '\\'; |
| 5975 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5976 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5977 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5978 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5979 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5981 | /* Copy everything else as-is */ |
| 5982 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5983 | *p++ = (char) ch; |
| 5984 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5985 | size = p - q; |
| 5986 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5987 | assert(size > 0); |
| 5988 | if (_PyBytes_Resize(&repr, size) < 0) |
| 5989 | return NULL; |
| 5990 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5991 | } |
| 5992 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5993 | PyObject * |
| 5994 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5995 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5996 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5997 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5998 | PyErr_BadArgument(); |
| 5999 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6000 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6001 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6002 | PyUnicode_GET_SIZE(unicode)); |
| 6003 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6004 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | } |
| 6006 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6007 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6008 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6009 | PyObject * |
| 6010 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6011 | Py_ssize_t size, |
| 6012 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6013 | { |
| 6014 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6015 | Py_ssize_t startinpos; |
| 6016 | Py_ssize_t endinpos; |
| 6017 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6018 | PyUnicodeObject *v; |
| 6019 | Py_UNICODE *p; |
| 6020 | const char *end; |
| 6021 | const char *reason; |
| 6022 | PyObject *errorHandler = NULL; |
| 6023 | PyObject *exc = NULL; |
| 6024 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6025 | #ifdef Py_UNICODE_WIDE |
| 6026 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 6027 | #endif |
| 6028 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6029 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6030 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 6031 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6032 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6033 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 6034 | as string was created with the old API. */ |
| 6035 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6036 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6037 | p = PyUnicode_AS_UNICODE(v); |
| 6038 | end = s + size; |
| 6039 | |
| 6040 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6041 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6042 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6043 | some malformed UCS-4 data. */ |
| 6044 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6045 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6046 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6047 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6048 | end-s < Py_UNICODE_SIZE |
| 6049 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6050 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6051 | startinpos = s - starts; |
| 6052 | if (end-s < Py_UNICODE_SIZE) { |
| 6053 | endinpos = end-starts; |
| 6054 | reason = "truncated input"; |
| 6055 | } |
| 6056 | else { |
| 6057 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6058 | reason = "illegal code point (> 0x10FFFF)"; |
| 6059 | } |
| 6060 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6061 | if (unicode_decode_call_errorhandler( |
| 6062 | errors, &errorHandler, |
| 6063 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6064 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6065 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6066 | goto onError; |
| 6067 | } |
| 6068 | } |
| 6069 | else { |
| 6070 | p++; |
| 6071 | s += Py_UNICODE_SIZE; |
| 6072 | } |
| 6073 | } |
| 6074 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6075 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6076 | goto onError; |
| 6077 | Py_XDECREF(errorHandler); |
| 6078 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6079 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6080 | Py_DECREF(v); |
| 6081 | return NULL; |
| 6082 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6083 | return (PyObject *)v; |
| 6084 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6085 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6086 | Py_XDECREF(v); |
| 6087 | Py_XDECREF(errorHandler); |
| 6088 | Py_XDECREF(exc); |
| 6089 | return NULL; |
| 6090 | } |
| 6091 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6092 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6093 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6094 | PyObject * |
| 6095 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6096 | Py_ssize_t size, |
| 6097 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6098 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6099 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6100 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6101 | } |
| 6102 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6103 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6104 | static void |
| 6105 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6106 | const char *encoding, |
| 6107 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6108 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6109 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6110 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6111 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6112 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6113 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 | } |
| 6115 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6116 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6117 | goto onError; |
| 6118 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6119 | goto onError; |
| 6120 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6121 | goto onError; |
| 6122 | return; |
| 6123 | onError: |
| 6124 | Py_DECREF(*exceptionObject); |
| 6125 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | } |
| 6127 | } |
| 6128 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6129 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6130 | static void |
| 6131 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6132 | const char *encoding, |
| 6133 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6134 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6135 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6136 | { |
| 6137 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6138 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6139 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6140 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
| 6143 | /* error handling callback helper: |
| 6144 | build arguments, call the callback and check the arguments, |
| 6145 | put the result into newpos and return the replacement string, which |
| 6146 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6147 | static PyObject * |
| 6148 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6149 | PyObject **errorHandler, |
| 6150 | const char *encoding, const char *reason, |
| 6151 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 6152 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6153 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6154 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6155 | 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] | 6156 | |
| 6157 | PyObject *restuple; |
| 6158 | PyObject *resunicode; |
| 6159 | |
| 6160 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6161 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6162 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6163 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6164 | } |
| 6165 | |
| 6166 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6168 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6169 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6170 | |
| 6171 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6172 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6173 | if (restuple == 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 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6176 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6177 | Py_DECREF(restuple); |
| 6178 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6179 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6180 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6181 | &resunicode, newpos)) { |
| 6182 | Py_DECREF(restuple); |
| 6183 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6184 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6185 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6186 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6187 | Py_DECREF(restuple); |
| 6188 | return NULL; |
| 6189 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6190 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6191 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6192 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6193 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6194 | Py_DECREF(restuple); |
| 6195 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6196 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6197 | Py_INCREF(resunicode); |
| 6198 | Py_DECREF(restuple); |
| 6199 | return resunicode; |
| 6200 | } |
| 6201 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6202 | static PyObject * |
| 6203 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6204 | Py_ssize_t size, |
| 6205 | const char *errors, |
| 6206 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6207 | { |
| 6208 | /* output object */ |
| 6209 | PyObject *res; |
| 6210 | /* pointers to the beginning and end+1 of input */ |
| 6211 | const Py_UNICODE *startp = p; |
| 6212 | const Py_UNICODE *endp = p + size; |
| 6213 | /* pointer to the beginning of the unencodable characters */ |
| 6214 | /* const Py_UNICODE *badp = NULL; */ |
| 6215 | /* pointer into the output */ |
| 6216 | char *str; |
| 6217 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6218 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6219 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6220 | 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] | 6221 | PyObject *errorHandler = NULL; |
| 6222 | PyObject *exc = NULL; |
| 6223 | /* the following variable is used for caching string comparisons |
| 6224 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6225 | int known_errorHandler = -1; |
| 6226 | |
| 6227 | /* allocate enough for a simple encoding without |
| 6228 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6229 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6230 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6231 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6232 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6233 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6234 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6235 | ressize = size; |
| 6236 | |
| 6237 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6238 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6239 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6240 | /* can we encode this? */ |
| 6241 | if (c<limit) { |
| 6242 | /* no overflow check, because we know that the space is enough */ |
| 6243 | *str++ = (char)c; |
| 6244 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6245 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6246 | else { |
| 6247 | Py_ssize_t unicodepos = p-startp; |
| 6248 | Py_ssize_t requiredsize; |
| 6249 | PyObject *repunicode; |
| 6250 | Py_ssize_t repsize; |
| 6251 | Py_ssize_t newpos; |
| 6252 | Py_ssize_t respos; |
| 6253 | Py_UNICODE *uni2; |
| 6254 | /* startpos for collecting unencodable chars */ |
| 6255 | const Py_UNICODE *collstart = p; |
| 6256 | const Py_UNICODE *collend = p; |
| 6257 | /* find all unecodable characters */ |
| 6258 | while ((collend < endp) && ((*collend)>=limit)) |
| 6259 | ++collend; |
| 6260 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6261 | if (known_errorHandler==-1) { |
| 6262 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6263 | known_errorHandler = 1; |
| 6264 | else if (!strcmp(errors, "replace")) |
| 6265 | known_errorHandler = 2; |
| 6266 | else if (!strcmp(errors, "ignore")) |
| 6267 | known_errorHandler = 3; |
| 6268 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6269 | known_errorHandler = 4; |
| 6270 | else |
| 6271 | known_errorHandler = 0; |
| 6272 | } |
| 6273 | switch (known_errorHandler) { |
| 6274 | case 1: /* strict */ |
| 6275 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 6276 | goto onError; |
| 6277 | case 2: /* replace */ |
| 6278 | while (collstart++<collend) |
| 6279 | *str++ = '?'; /* fall through */ |
| 6280 | case 3: /* ignore */ |
| 6281 | p = collend; |
| 6282 | break; |
| 6283 | case 4: /* xmlcharrefreplace */ |
| 6284 | respos = str - PyBytes_AS_STRING(res); |
| 6285 | /* determine replacement size (temporarily (mis)uses p) */ |
| 6286 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 6287 | if (*p<10) |
| 6288 | repsize += 2+1+1; |
| 6289 | else if (*p<100) |
| 6290 | repsize += 2+2+1; |
| 6291 | else if (*p<1000) |
| 6292 | repsize += 2+3+1; |
| 6293 | else if (*p<10000) |
| 6294 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6295 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6296 | else |
| 6297 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6298 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6299 | else if (*p<100000) |
| 6300 | repsize += 2+5+1; |
| 6301 | else if (*p<1000000) |
| 6302 | repsize += 2+6+1; |
| 6303 | else |
| 6304 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6305 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6306 | } |
| 6307 | requiredsize = respos+repsize+(endp-collend); |
| 6308 | if (requiredsize > ressize) { |
| 6309 | if (requiredsize<2*ressize) |
| 6310 | requiredsize = 2*ressize; |
| 6311 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6312 | goto onError; |
| 6313 | str = PyBytes_AS_STRING(res) + respos; |
| 6314 | ressize = requiredsize; |
| 6315 | } |
| 6316 | /* generate replacement (temporarily (mis)uses p) */ |
| 6317 | for (p = collstart; p < collend; ++p) { |
| 6318 | str += sprintf(str, "&#%d;", (int)*p); |
| 6319 | } |
| 6320 | p = collend; |
| 6321 | break; |
| 6322 | default: |
| 6323 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6324 | encoding, reason, startp, size, &exc, |
| 6325 | collstart-startp, collend-startp, &newpos); |
| 6326 | if (repunicode == NULL) |
| 6327 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6328 | if (PyBytes_Check(repunicode)) { |
| 6329 | /* Directly copy bytes result to output. */ |
| 6330 | repsize = PyBytes_Size(repunicode); |
| 6331 | if (repsize > 1) { |
| 6332 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6333 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6334 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6335 | Py_DECREF(repunicode); |
| 6336 | goto onError; |
| 6337 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6338 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6339 | ressize += repsize-1; |
| 6340 | } |
| 6341 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6342 | str += repsize; |
| 6343 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6344 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6345 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6346 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6347 | /* need more space? (at least enough for what we |
| 6348 | have+the replacement+the rest of the string, so |
| 6349 | we won't have to check space for encodable characters) */ |
| 6350 | respos = str - PyBytes_AS_STRING(res); |
| 6351 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6352 | requiredsize = respos+repsize+(endp-collend); |
| 6353 | if (requiredsize > ressize) { |
| 6354 | if (requiredsize<2*ressize) |
| 6355 | requiredsize = 2*ressize; |
| 6356 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6357 | Py_DECREF(repunicode); |
| 6358 | goto onError; |
| 6359 | } |
| 6360 | str = PyBytes_AS_STRING(res) + respos; |
| 6361 | ressize = requiredsize; |
| 6362 | } |
| 6363 | /* check if there is anything unencodable in the replacement |
| 6364 | and copy it to the output */ |
| 6365 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6366 | c = *uni2; |
| 6367 | if (c >= limit) { |
| 6368 | raise_encode_exception(&exc, encoding, startp, size, |
| 6369 | unicodepos, unicodepos+1, reason); |
| 6370 | Py_DECREF(repunicode); |
| 6371 | goto onError; |
| 6372 | } |
| 6373 | *str = (char)c; |
| 6374 | } |
| 6375 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6376 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6377 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6378 | } |
| 6379 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6380 | /* Resize if we allocated to much */ |
| 6381 | size = str - PyBytes_AS_STRING(res); |
| 6382 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6383 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6384 | if (_PyBytes_Resize(&res, size) < 0) |
| 6385 | goto onError; |
| 6386 | } |
| 6387 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6388 | Py_XDECREF(errorHandler); |
| 6389 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6390 | return res; |
| 6391 | |
| 6392 | onError: |
| 6393 | Py_XDECREF(res); |
| 6394 | Py_XDECREF(errorHandler); |
| 6395 | Py_XDECREF(exc); |
| 6396 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6397 | } |
| 6398 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6399 | PyObject * |
| 6400 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6401 | Py_ssize_t size, |
| 6402 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6403 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6404 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6405 | } |
| 6406 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6407 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6408 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6409 | { |
| 6410 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6411 | PyErr_BadArgument(); |
| 6412 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6413 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6414 | if (PyUnicode_READY(unicode) == -1) |
| 6415 | return NULL; |
| 6416 | /* Fast path: if it is a one-byte string, construct |
| 6417 | bytes object directly. */ |
| 6418 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6419 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6420 | PyUnicode_GET_LENGTH(unicode)); |
| 6421 | /* Non-Latin-1 characters present. Defer to above function to |
| 6422 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6423 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6424 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6425 | errors); |
| 6426 | } |
| 6427 | |
| 6428 | PyObject* |
| 6429 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6430 | { |
| 6431 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
| 6434 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6435 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6436 | PyObject * |
| 6437 | PyUnicode_DecodeASCII(const char *s, |
| 6438 | Py_ssize_t size, |
| 6439 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6440 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6441 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6442 | PyUnicodeObject *v; |
| 6443 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6444 | Py_ssize_t startinpos; |
| 6445 | Py_ssize_t endinpos; |
| 6446 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6447 | const char *e; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6448 | unsigned char* d; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6449 | PyObject *errorHandler = NULL; |
| 6450 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6451 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6453 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6454 | if (size == 1 && *(unsigned char*)s < 128) |
| 6455 | return PyUnicode_FromOrdinal(*(unsigned char*)s); |
| 6456 | |
| 6457 | /* Fast path. Assume the input actually *is* ASCII, and allocate |
| 6458 | a single-block Unicode object with that assumption. If there is |
| 6459 | an error, drop the object and start over. */ |
| 6460 | v = (PyUnicodeObject*)PyUnicode_New(size, 127); |
| 6461 | if (v == NULL) |
| 6462 | goto onError; |
| 6463 | d = PyUnicode_1BYTE_DATA(v); |
| 6464 | for (i = 0; i < size; i++) { |
| 6465 | unsigned char ch = ((unsigned char*)s)[i]; |
| 6466 | if (ch < 128) |
| 6467 | d[i] = ch; |
| 6468 | else |
| 6469 | break; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6470 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6471 | if (i == size) |
| 6472 | return (PyObject*)v; |
| 6473 | Py_DECREF(v); /* start over */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6474 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6475 | v = _PyUnicode_New(size); |
| 6476 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6477 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6478 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6479 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6480 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6481 | e = s + size; |
| 6482 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6483 | register unsigned char c = (unsigned char)*s; |
| 6484 | if (c < 128) { |
| 6485 | *p++ = c; |
| 6486 | ++s; |
| 6487 | } |
| 6488 | else { |
| 6489 | startinpos = s-starts; |
| 6490 | endinpos = startinpos + 1; |
| 6491 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 6492 | if (unicode_decode_call_errorhandler( |
| 6493 | errors, &errorHandler, |
| 6494 | "ascii", "ordinal not in range(128)", |
| 6495 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6496 | &v, &outpos, &p)) |
| 6497 | goto onError; |
| 6498 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6499 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6500 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6501 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6502 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6503 | Py_XDECREF(errorHandler); |
| 6504 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6505 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6506 | Py_DECREF(v); |
| 6507 | return NULL; |
| 6508 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6509 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6510 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6511 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6512 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6513 | Py_XDECREF(errorHandler); |
| 6514 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6515 | return NULL; |
| 6516 | } |
| 6517 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6518 | PyObject * |
| 6519 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6520 | Py_ssize_t size, |
| 6521 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6522 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6523 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6524 | } |
| 6525 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6526 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6527 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6528 | { |
| 6529 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6530 | PyErr_BadArgument(); |
| 6531 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6532 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6533 | if (PyUnicode_READY(unicode) == -1) |
| 6534 | return NULL; |
| 6535 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6536 | directly. Else defer to above function to raise the exception. */ |
| 6537 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6538 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6539 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6540 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6541 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6542 | errors); |
| 6543 | } |
| 6544 | |
| 6545 | PyObject * |
| 6546 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6547 | { |
| 6548 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6549 | } |
| 6550 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6551 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6552 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6553 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6554 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6555 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6556 | #define NEED_RETRY |
| 6557 | #endif |
| 6558 | |
| 6559 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6560 | a) it assumes an incomplete character consists of a single byte, and |
| 6561 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6562 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6563 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6564 | static int |
| 6565 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6566 | { |
| 6567 | const char *curr = s + offset; |
| 6568 | |
| 6569 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6570 | const char *prev = CharPrev(s, curr); |
| 6571 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6572 | } |
| 6573 | return 0; |
| 6574 | } |
| 6575 | |
| 6576 | /* |
| 6577 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6578 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6579 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6580 | static int |
| 6581 | decode_mbcs(PyUnicodeObject **v, |
| 6582 | const char *s, /* MBCS string */ |
| 6583 | int size, /* sizeof MBCS string */ |
| 6584 | int final, |
| 6585 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6586 | { |
| 6587 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6588 | Py_ssize_t n; |
| 6589 | DWORD usize; |
| 6590 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6591 | |
| 6592 | assert(size >= 0); |
| 6593 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6594 | /* check and handle 'errors' arg */ |
| 6595 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6596 | flags = MB_ERR_INVALID_CHARS; |
| 6597 | else if (strcmp(errors, "ignore")==0) |
| 6598 | flags = 0; |
| 6599 | else { |
| 6600 | PyErr_Format(PyExc_ValueError, |
| 6601 | "mbcs encoding does not support errors='%s'", |
| 6602 | errors); |
| 6603 | return -1; |
| 6604 | } |
| 6605 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6606 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6607 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6608 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6609 | |
| 6610 | /* First get the size of the result */ |
| 6611 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6612 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6613 | if (usize==0) |
| 6614 | goto mbcs_decode_error; |
| 6615 | } else |
| 6616 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6617 | |
| 6618 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6619 | /* Create unicode object */ |
| 6620 | *v = _PyUnicode_New(usize); |
| 6621 | if (*v == NULL) |
| 6622 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6623 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6624 | } |
| 6625 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6626 | /* Extend unicode object */ |
| 6627 | n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 6628 | if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6629 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6630 | } |
| 6631 | |
| 6632 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6633 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6634 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6635 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6636 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6637 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6638 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6639 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6640 | |
| 6641 | mbcs_decode_error: |
| 6642 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6643 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6644 | windows error |
| 6645 | */ |
| 6646 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6647 | /* Ideally, we should get reason from FormatMessage - this |
| 6648 | is the Windows 2000 English version of the message |
| 6649 | */ |
| 6650 | PyObject *exc = NULL; |
| 6651 | const char *reason = "No mapping for the Unicode character exists " |
| 6652 | "in the target multi-byte code page."; |
| 6653 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6654 | if (exc != NULL) { |
| 6655 | PyCodec_StrictErrors(exc); |
| 6656 | Py_DECREF(exc); |
| 6657 | } |
| 6658 | } else { |
| 6659 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6660 | } |
| 6661 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6664 | PyObject * |
| 6665 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6666 | Py_ssize_t size, |
| 6667 | const char *errors, |
| 6668 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6669 | { |
| 6670 | PyUnicodeObject *v = NULL; |
| 6671 | int done; |
| 6672 | |
| 6673 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6674 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6675 | |
| 6676 | #ifdef NEED_RETRY |
| 6677 | retry: |
| 6678 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6679 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6680 | else |
| 6681 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6682 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6683 | |
| 6684 | if (done < 0) { |
| 6685 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6686 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6687 | } |
| 6688 | |
| 6689 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6690 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6691 | |
| 6692 | #ifdef NEED_RETRY |
| 6693 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6694 | s += done; |
| 6695 | size -= done; |
| 6696 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6697 | } |
| 6698 | #endif |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6699 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6700 | Py_DECREF(v); |
| 6701 | return NULL; |
| 6702 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6703 | return (PyObject *)v; |
| 6704 | } |
| 6705 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6706 | PyObject * |
| 6707 | PyUnicode_DecodeMBCS(const char *s, |
| 6708 | Py_ssize_t size, |
| 6709 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6710 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6711 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6712 | } |
| 6713 | |
| 6714 | /* |
| 6715 | * Convert unicode into string object (MBCS). |
| 6716 | * Returns 0 if succeed, -1 otherwise. |
| 6717 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6718 | static int |
| 6719 | encode_mbcs(PyObject **repr, |
| 6720 | const Py_UNICODE *p, /* unicode */ |
| 6721 | int size, /* size of unicode */ |
| 6722 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6723 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6724 | BOOL usedDefaultChar = FALSE; |
| 6725 | BOOL *pusedDefaultChar; |
| 6726 | int mbcssize; |
| 6727 | Py_ssize_t n; |
| 6728 | PyObject *exc = NULL; |
| 6729 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6730 | |
| 6731 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6732 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6733 | /* check and handle 'errors' arg */ |
| 6734 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 6735 | flags = WC_NO_BEST_FIT_CHARS; |
| 6736 | pusedDefaultChar = &usedDefaultChar; |
| 6737 | } else if (strcmp(errors, "replace")==0) { |
| 6738 | flags = 0; |
| 6739 | pusedDefaultChar = NULL; |
| 6740 | } else { |
| 6741 | PyErr_Format(PyExc_ValueError, |
| 6742 | "mbcs encoding does not support errors='%s'", |
| 6743 | errors); |
| 6744 | return -1; |
| 6745 | } |
| 6746 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6747 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6748 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6749 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 6750 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6751 | if (mbcssize == 0) { |
| 6752 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6753 | return -1; |
| 6754 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6755 | /* If we used a default char, then we failed! */ |
| 6756 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6757 | goto mbcs_encode_error; |
| 6758 | } else { |
| 6759 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6760 | } |
| 6761 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6762 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6763 | /* Create string object */ |
| 6764 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 6765 | if (*repr == NULL) |
| 6766 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6767 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6768 | } |
| 6769 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6770 | /* Extend string object */ |
| 6771 | n = PyBytes_Size(*repr); |
| 6772 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 6773 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6774 | } |
| 6775 | |
| 6776 | /* Do the conversion */ |
| 6777 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6778 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6779 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 6780 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6781 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6782 | return -1; |
| 6783 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6784 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6785 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6786 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6787 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6788 | |
| 6789 | mbcs_encode_error: |
| 6790 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 6791 | Py_XDECREF(exc); |
| 6792 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6793 | } |
| 6794 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6795 | PyObject * |
| 6796 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 6797 | Py_ssize_t size, |
| 6798 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6799 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6800 | PyObject *repr = NULL; |
| 6801 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 6802 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6803 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6804 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6805 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6806 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6807 | else |
| 6808 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6809 | ret = encode_mbcs(&repr, p, (int)size, 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 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6812 | Py_XDECREF(repr); |
| 6813 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6814 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6815 | |
| 6816 | #ifdef NEED_RETRY |
| 6817 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6818 | p += INT_MAX; |
| 6819 | size -= INT_MAX; |
| 6820 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6821 | } |
| 6822 | #endif |
| 6823 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6824 | return repr; |
| 6825 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6826 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6827 | PyObject * |
| 6828 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6829 | { |
| 6830 | if (!PyUnicode_Check(unicode)) { |
| 6831 | PyErr_BadArgument(); |
| 6832 | return NULL; |
| 6833 | } |
| 6834 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6835 | PyUnicode_GET_SIZE(unicode), |
| 6836 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6837 | } |
| 6838 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6839 | #undef NEED_RETRY |
| 6840 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6841 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6842 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6843 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 6844 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6845 | PyObject * |
| 6846 | PyUnicode_DecodeCharmap(const char *s, |
| 6847 | Py_ssize_t size, |
| 6848 | PyObject *mapping, |
| 6849 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6850 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6851 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6852 | Py_ssize_t startinpos; |
| 6853 | Py_ssize_t endinpos; |
| 6854 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6855 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6856 | PyUnicodeObject *v; |
| 6857 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6858 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6859 | PyObject *errorHandler = NULL; |
| 6860 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6861 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6862 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6863 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6864 | /* Default to Latin-1 */ |
| 6865 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6866 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6867 | |
| 6868 | v = _PyUnicode_New(size); |
| 6869 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6870 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6871 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6872 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6873 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6874 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6875 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6876 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 6877 | maplen = PyUnicode_GET_SIZE(mapping); |
| 6878 | while (s < e) { |
| 6879 | unsigned char ch = *s; |
| 6880 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6881 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6882 | if (ch < maplen) |
| 6883 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6884 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6885 | if (x == 0xfffe) { |
| 6886 | /* undefined mapping */ |
| 6887 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6888 | startinpos = s-starts; |
| 6889 | endinpos = startinpos+1; |
| 6890 | if (unicode_decode_call_errorhandler( |
| 6891 | errors, &errorHandler, |
| 6892 | "charmap", "character maps to <undefined>", |
| 6893 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6894 | &v, &outpos, &p)) { |
| 6895 | goto onError; |
| 6896 | } |
| 6897 | continue; |
| 6898 | } |
| 6899 | *p++ = x; |
| 6900 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6901 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6902 | } |
| 6903 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6904 | while (s < e) { |
| 6905 | unsigned char ch = *s; |
| 6906 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6907 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6908 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 6909 | w = PyLong_FromLong((long)ch); |
| 6910 | if (w == NULL) |
| 6911 | goto onError; |
| 6912 | x = PyObject_GetItem(mapping, w); |
| 6913 | Py_DECREF(w); |
| 6914 | if (x == NULL) { |
| 6915 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6916 | /* No mapping found means: mapping is undefined. */ |
| 6917 | PyErr_Clear(); |
| 6918 | x = Py_None; |
| 6919 | Py_INCREF(x); |
| 6920 | } else |
| 6921 | goto onError; |
| 6922 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6923 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6924 | /* Apply mapping */ |
| 6925 | if (PyLong_Check(x)) { |
| 6926 | long value = PyLong_AS_LONG(x); |
| 6927 | if (value < 0 || value > 65535) { |
| 6928 | PyErr_SetString(PyExc_TypeError, |
| 6929 | "character mapping must be in range(65536)"); |
| 6930 | Py_DECREF(x); |
| 6931 | goto onError; |
| 6932 | } |
| 6933 | *p++ = (Py_UNICODE)value; |
| 6934 | } |
| 6935 | else if (x == Py_None) { |
| 6936 | /* undefined mapping */ |
| 6937 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6938 | startinpos = s-starts; |
| 6939 | endinpos = startinpos+1; |
| 6940 | if (unicode_decode_call_errorhandler( |
| 6941 | errors, &errorHandler, |
| 6942 | "charmap", "character maps to <undefined>", |
| 6943 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6944 | &v, &outpos, &p)) { |
| 6945 | Py_DECREF(x); |
| 6946 | goto onError; |
| 6947 | } |
| 6948 | Py_DECREF(x); |
| 6949 | continue; |
| 6950 | } |
| 6951 | else if (PyUnicode_Check(x)) { |
| 6952 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6953 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6954 | if (targetsize == 1) |
| 6955 | /* 1-1 mapping */ |
| 6956 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6957 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6958 | else if (targetsize > 1) { |
| 6959 | /* 1-n mapping */ |
| 6960 | if (targetsize > extrachars) { |
| 6961 | /* resize first */ |
| 6962 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 6963 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 6964 | (targetsize << 2); |
| 6965 | extrachars += needed; |
| 6966 | /* XXX overflow detection missing */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6967 | if (PyUnicode_Resize((PyObject**)&v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6968 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 6969 | Py_DECREF(x); |
| 6970 | goto onError; |
| 6971 | } |
| 6972 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 6973 | } |
| 6974 | Py_UNICODE_COPY(p, |
| 6975 | PyUnicode_AS_UNICODE(x), |
| 6976 | targetsize); |
| 6977 | p += targetsize; |
| 6978 | extrachars -= targetsize; |
| 6979 | } |
| 6980 | /* 1-0 mapping: skip the character */ |
| 6981 | } |
| 6982 | else { |
| 6983 | /* wrong return value */ |
| 6984 | PyErr_SetString(PyExc_TypeError, |
| 6985 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6986 | Py_DECREF(x); |
| 6987 | goto onError; |
| 6988 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6989 | Py_DECREF(x); |
| 6990 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6991 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6992 | } |
| 6993 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6994 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6995 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6996 | Py_XDECREF(errorHandler); |
| 6997 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6998 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6999 | Py_DECREF(v); |
| 7000 | return NULL; |
| 7001 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7002 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7003 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7004 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7005 | Py_XDECREF(errorHandler); |
| 7006 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7007 | Py_XDECREF(v); |
| 7008 | return NULL; |
| 7009 | } |
| 7010 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7011 | /* Charmap encoding: the lookup table */ |
| 7012 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7013 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7014 | PyObject_HEAD |
| 7015 | unsigned char level1[32]; |
| 7016 | int count2, count3; |
| 7017 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7018 | }; |
| 7019 | |
| 7020 | static PyObject* |
| 7021 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7022 | { |
| 7023 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7024 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7025 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7026 | } |
| 7027 | |
| 7028 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7029 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7030 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7031 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7032 | }; |
| 7033 | |
| 7034 | static void |
| 7035 | encoding_map_dealloc(PyObject* o) |
| 7036 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7037 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7038 | } |
| 7039 | |
| 7040 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7041 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7042 | "EncodingMap", /*tp_name*/ |
| 7043 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7044 | 0, /*tp_itemsize*/ |
| 7045 | /* methods */ |
| 7046 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7047 | 0, /*tp_print*/ |
| 7048 | 0, /*tp_getattr*/ |
| 7049 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7050 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7051 | 0, /*tp_repr*/ |
| 7052 | 0, /*tp_as_number*/ |
| 7053 | 0, /*tp_as_sequence*/ |
| 7054 | 0, /*tp_as_mapping*/ |
| 7055 | 0, /*tp_hash*/ |
| 7056 | 0, /*tp_call*/ |
| 7057 | 0, /*tp_str*/ |
| 7058 | 0, /*tp_getattro*/ |
| 7059 | 0, /*tp_setattro*/ |
| 7060 | 0, /*tp_as_buffer*/ |
| 7061 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7062 | 0, /*tp_doc*/ |
| 7063 | 0, /*tp_traverse*/ |
| 7064 | 0, /*tp_clear*/ |
| 7065 | 0, /*tp_richcompare*/ |
| 7066 | 0, /*tp_weaklistoffset*/ |
| 7067 | 0, /*tp_iter*/ |
| 7068 | 0, /*tp_iternext*/ |
| 7069 | encoding_map_methods, /*tp_methods*/ |
| 7070 | 0, /*tp_members*/ |
| 7071 | 0, /*tp_getset*/ |
| 7072 | 0, /*tp_base*/ |
| 7073 | 0, /*tp_dict*/ |
| 7074 | 0, /*tp_descr_get*/ |
| 7075 | 0, /*tp_descr_set*/ |
| 7076 | 0, /*tp_dictoffset*/ |
| 7077 | 0, /*tp_init*/ |
| 7078 | 0, /*tp_alloc*/ |
| 7079 | 0, /*tp_new*/ |
| 7080 | 0, /*tp_free*/ |
| 7081 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7082 | }; |
| 7083 | |
| 7084 | PyObject* |
| 7085 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7086 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7087 | PyObject *result; |
| 7088 | struct encoding_map *mresult; |
| 7089 | int i; |
| 7090 | int need_dict = 0; |
| 7091 | unsigned char level1[32]; |
| 7092 | unsigned char level2[512]; |
| 7093 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7094 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7095 | int kind; |
| 7096 | void *data; |
| 7097 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7098 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7099 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7100 | PyErr_BadArgument(); |
| 7101 | return NULL; |
| 7102 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7103 | kind = PyUnicode_KIND(string); |
| 7104 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7105 | memset(level1, 0xFF, sizeof level1); |
| 7106 | memset(level2, 0xFF, sizeof level2); |
| 7107 | |
| 7108 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7109 | or if there are non-BMP characters, we need to use |
| 7110 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7111 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7112 | need_dict = 1; |
| 7113 | for (i = 1; i < 256; i++) { |
| 7114 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7115 | ch = PyUnicode_READ(kind, data, i); |
| 7116 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7117 | need_dict = 1; |
| 7118 | break; |
| 7119 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7120 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7121 | /* unmapped character */ |
| 7122 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7123 | l1 = ch >> 11; |
| 7124 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7125 | if (level1[l1] == 0xFF) |
| 7126 | level1[l1] = count2++; |
| 7127 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7128 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7129 | } |
| 7130 | |
| 7131 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7132 | need_dict = 1; |
| 7133 | |
| 7134 | if (need_dict) { |
| 7135 | PyObject *result = PyDict_New(); |
| 7136 | PyObject *key, *value; |
| 7137 | if (!result) |
| 7138 | return NULL; |
| 7139 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7140 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7141 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7142 | if (!key || !value) |
| 7143 | goto failed1; |
| 7144 | if (PyDict_SetItem(result, key, value) == -1) |
| 7145 | goto failed1; |
| 7146 | Py_DECREF(key); |
| 7147 | Py_DECREF(value); |
| 7148 | } |
| 7149 | return result; |
| 7150 | failed1: |
| 7151 | Py_XDECREF(key); |
| 7152 | Py_XDECREF(value); |
| 7153 | Py_DECREF(result); |
| 7154 | return NULL; |
| 7155 | } |
| 7156 | |
| 7157 | /* Create a three-level trie */ |
| 7158 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7159 | 16*count2 + 128*count3 - 1); |
| 7160 | if (!result) |
| 7161 | return PyErr_NoMemory(); |
| 7162 | PyObject_Init(result, &EncodingMapType); |
| 7163 | mresult = (struct encoding_map*)result; |
| 7164 | mresult->count2 = count2; |
| 7165 | mresult->count3 = count3; |
| 7166 | mlevel1 = mresult->level1; |
| 7167 | mlevel2 = mresult->level23; |
| 7168 | mlevel3 = mresult->level23 + 16*count2; |
| 7169 | memcpy(mlevel1, level1, 32); |
| 7170 | memset(mlevel2, 0xFF, 16*count2); |
| 7171 | memset(mlevel3, 0, 128*count3); |
| 7172 | count3 = 0; |
| 7173 | for (i = 1; i < 256; i++) { |
| 7174 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7175 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7176 | /* unmapped character */ |
| 7177 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7178 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7179 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7180 | i2 = 16*mlevel1[o1] + o2; |
| 7181 | if (mlevel2[i2] == 0xFF) |
| 7182 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7183 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7184 | i3 = 128*mlevel2[i2] + o3; |
| 7185 | mlevel3[i3] = i; |
| 7186 | } |
| 7187 | return result; |
| 7188 | } |
| 7189 | |
| 7190 | static int |
| 7191 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7192 | { |
| 7193 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7194 | int l1 = c>>11; |
| 7195 | int l2 = (c>>7) & 0xF; |
| 7196 | int l3 = c & 0x7F; |
| 7197 | int i; |
| 7198 | |
| 7199 | #ifdef Py_UNICODE_WIDE |
| 7200 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7201 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7202 | } |
| 7203 | #endif |
| 7204 | if (c == 0) |
| 7205 | return 0; |
| 7206 | /* level 1*/ |
| 7207 | i = map->level1[l1]; |
| 7208 | if (i == 0xFF) { |
| 7209 | return -1; |
| 7210 | } |
| 7211 | /* level 2*/ |
| 7212 | i = map->level23[16*i+l2]; |
| 7213 | if (i == 0xFF) { |
| 7214 | return -1; |
| 7215 | } |
| 7216 | /* level 3 */ |
| 7217 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7218 | if (i == 0) { |
| 7219 | return -1; |
| 7220 | } |
| 7221 | return i; |
| 7222 | } |
| 7223 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7224 | /* Lookup the character ch in the mapping. If the character |
| 7225 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7226 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7227 | static PyObject * |
| 7228 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7229 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7230 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7231 | PyObject *x; |
| 7232 | |
| 7233 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7234 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7235 | x = PyObject_GetItem(mapping, w); |
| 7236 | Py_DECREF(w); |
| 7237 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7238 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7239 | /* No mapping found means: mapping is undefined. */ |
| 7240 | PyErr_Clear(); |
| 7241 | x = Py_None; |
| 7242 | Py_INCREF(x); |
| 7243 | return x; |
| 7244 | } else |
| 7245 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7246 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7247 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7248 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7249 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7250 | long value = PyLong_AS_LONG(x); |
| 7251 | if (value < 0 || value > 255) { |
| 7252 | PyErr_SetString(PyExc_TypeError, |
| 7253 | "character mapping must be in range(256)"); |
| 7254 | Py_DECREF(x); |
| 7255 | return NULL; |
| 7256 | } |
| 7257 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7258 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7259 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7260 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7261 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7262 | /* wrong return value */ |
| 7263 | PyErr_Format(PyExc_TypeError, |
| 7264 | "character mapping must return integer, bytes or None, not %.400s", |
| 7265 | x->ob_type->tp_name); |
| 7266 | Py_DECREF(x); |
| 7267 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7268 | } |
| 7269 | } |
| 7270 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7271 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7272 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7273 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7274 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7275 | /* exponentially overallocate to minimize reallocations */ |
| 7276 | if (requiredsize < 2*outsize) |
| 7277 | requiredsize = 2*outsize; |
| 7278 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7279 | return -1; |
| 7280 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7281 | } |
| 7282 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7283 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7284 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7285 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7286 | /* 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] | 7287 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7288 | space is available. Return a new reference to the object that |
| 7289 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7290 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7291 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7292 | static charmapencode_result |
| 7293 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 7294 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7295 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7296 | PyObject *rep; |
| 7297 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7298 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7299 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7300 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7301 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7302 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7303 | if (res == -1) |
| 7304 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7305 | if (outsize<requiredsize) |
| 7306 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7307 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7308 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7309 | outstart[(*outpos)++] = (char)res; |
| 7310 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7314 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7315 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7316 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7317 | Py_DECREF(rep); |
| 7318 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7319 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7320 | if (PyLong_Check(rep)) { |
| 7321 | Py_ssize_t requiredsize = *outpos+1; |
| 7322 | if (outsize<requiredsize) |
| 7323 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7324 | Py_DECREF(rep); |
| 7325 | return enc_EXCEPTION; |
| 7326 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7327 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7328 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7329 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7330 | else { |
| 7331 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7332 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7333 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7334 | if (outsize<requiredsize) |
| 7335 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7336 | Py_DECREF(rep); |
| 7337 | return enc_EXCEPTION; |
| 7338 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7339 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7340 | memcpy(outstart + *outpos, repchars, repsize); |
| 7341 | *outpos += repsize; |
| 7342 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7343 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7344 | Py_DECREF(rep); |
| 7345 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7346 | } |
| 7347 | |
| 7348 | /* handle an error in PyUnicode_EncodeCharmap |
| 7349 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7350 | static int |
| 7351 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7352 | 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] | 7353 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7354 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7355 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7356 | { |
| 7357 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7358 | Py_ssize_t repsize; |
| 7359 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7360 | Py_UNICODE *uni2; |
| 7361 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7362 | Py_ssize_t collstartpos = *inpos; |
| 7363 | Py_ssize_t collendpos = *inpos+1; |
| 7364 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7365 | char *encoding = "charmap"; |
| 7366 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7367 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7368 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7369 | /* find all unencodable characters */ |
| 7370 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7371 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7372 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7373 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 7374 | if (res != -1) |
| 7375 | break; |
| 7376 | ++collendpos; |
| 7377 | continue; |
| 7378 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7379 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7380 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 7381 | if (rep==NULL) |
| 7382 | return -1; |
| 7383 | else if (rep!=Py_None) { |
| 7384 | Py_DECREF(rep); |
| 7385 | break; |
| 7386 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7387 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7388 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7389 | } |
| 7390 | /* cache callback name lookup |
| 7391 | * (if not done yet, i.e. it's the first error) */ |
| 7392 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7393 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7394 | *known_errorHandler = 1; |
| 7395 | else if (!strcmp(errors, "replace")) |
| 7396 | *known_errorHandler = 2; |
| 7397 | else if (!strcmp(errors, "ignore")) |
| 7398 | *known_errorHandler = 3; |
| 7399 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7400 | *known_errorHandler = 4; |
| 7401 | else |
| 7402 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7403 | } |
| 7404 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7405 | case 1: /* strict */ |
| 7406 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7407 | return -1; |
| 7408 | case 2: /* replace */ |
| 7409 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7410 | x = charmapencode_output('?', mapping, res, respos); |
| 7411 | if (x==enc_EXCEPTION) { |
| 7412 | return -1; |
| 7413 | } |
| 7414 | else if (x==enc_FAILED) { |
| 7415 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7416 | return -1; |
| 7417 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7418 | } |
| 7419 | /* fall through */ |
| 7420 | case 3: /* ignore */ |
| 7421 | *inpos = collendpos; |
| 7422 | break; |
| 7423 | case 4: /* xmlcharrefreplace */ |
| 7424 | /* generate replacement (temporarily (mis)uses p) */ |
| 7425 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7426 | char buffer[2+29+1+1]; |
| 7427 | char *cp; |
| 7428 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7429 | for (cp = buffer; *cp; ++cp) { |
| 7430 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7431 | if (x==enc_EXCEPTION) |
| 7432 | return -1; |
| 7433 | else if (x==enc_FAILED) { |
| 7434 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7435 | return -1; |
| 7436 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7437 | } |
| 7438 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7439 | *inpos = collendpos; |
| 7440 | break; |
| 7441 | default: |
| 7442 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7443 | encoding, reason, p, size, exceptionObject, |
| 7444 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7445 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7446 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7447 | if (PyBytes_Check(repunicode)) { |
| 7448 | /* Directly copy bytes result to output. */ |
| 7449 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7450 | Py_ssize_t requiredsize; |
| 7451 | repsize = PyBytes_Size(repunicode); |
| 7452 | requiredsize = *respos + repsize; |
| 7453 | if (requiredsize > outsize) |
| 7454 | /* Make room for all additional bytes. */ |
| 7455 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7456 | Py_DECREF(repunicode); |
| 7457 | return -1; |
| 7458 | } |
| 7459 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7460 | PyBytes_AsString(repunicode), repsize); |
| 7461 | *respos += repsize; |
| 7462 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7463 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7464 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7465 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7466 | /* generate replacement */ |
| 7467 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7468 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7469 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7470 | if (x==enc_EXCEPTION) { |
| 7471 | return -1; |
| 7472 | } |
| 7473 | else if (x==enc_FAILED) { |
| 7474 | Py_DECREF(repunicode); |
| 7475 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7476 | return -1; |
| 7477 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7478 | } |
| 7479 | *inpos = newpos; |
| 7480 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7481 | } |
| 7482 | return 0; |
| 7483 | } |
| 7484 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7485 | PyObject * |
| 7486 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7487 | Py_ssize_t size, |
| 7488 | PyObject *mapping, |
| 7489 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7490 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7491 | /* output object */ |
| 7492 | PyObject *res = NULL; |
| 7493 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7494 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7495 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7496 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7497 | PyObject *errorHandler = NULL; |
| 7498 | PyObject *exc = NULL; |
| 7499 | /* the following variable is used for caching string comparisons |
| 7500 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7501 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7502 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7503 | |
| 7504 | /* Default to Latin-1 */ |
| 7505 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7506 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7507 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7508 | /* allocate enough for a simple encoding without |
| 7509 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7510 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7511 | if (res == NULL) |
| 7512 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7513 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7514 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7515 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7516 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7517 | /* try to encode it */ |
| 7518 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7519 | if (x==enc_EXCEPTION) /* error */ |
| 7520 | goto onError; |
| 7521 | if (x==enc_FAILED) { /* unencodable character */ |
| 7522 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7523 | &exc, |
| 7524 | &known_errorHandler, &errorHandler, errors, |
| 7525 | &res, &respos)) { |
| 7526 | goto onError; |
| 7527 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7528 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7529 | else |
| 7530 | /* done with this character => adjust input position */ |
| 7531 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7532 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7533 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7534 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7535 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7536 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7537 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7538 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7539 | Py_XDECREF(exc); |
| 7540 | Py_XDECREF(errorHandler); |
| 7541 | return res; |
| 7542 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7543 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7544 | Py_XDECREF(res); |
| 7545 | Py_XDECREF(exc); |
| 7546 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7547 | return NULL; |
| 7548 | } |
| 7549 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7550 | PyObject * |
| 7551 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7552 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7553 | { |
| 7554 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7555 | PyErr_BadArgument(); |
| 7556 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7557 | } |
| 7558 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7559 | PyUnicode_GET_SIZE(unicode), |
| 7560 | mapping, |
| 7561 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7562 | } |
| 7563 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7564 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7565 | static void |
| 7566 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7567 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7568 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7569 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7570 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7571 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7572 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7573 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7574 | } |
| 7575 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7576 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7577 | goto onError; |
| 7578 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7579 | goto onError; |
| 7580 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7581 | goto onError; |
| 7582 | return; |
| 7583 | onError: |
| 7584 | Py_DECREF(*exceptionObject); |
| 7585 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7586 | } |
| 7587 | } |
| 7588 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7589 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7590 | static void |
| 7591 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7592 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7593 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7594 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7595 | { |
| 7596 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7597 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7598 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7599 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7600 | } |
| 7601 | |
| 7602 | /* error handling callback helper: |
| 7603 | build arguments, call the callback and check the arguments, |
| 7604 | put the result into newpos and return the replacement string, which |
| 7605 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7606 | static PyObject * |
| 7607 | unicode_translate_call_errorhandler(const char *errors, |
| 7608 | PyObject **errorHandler, |
| 7609 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7610 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7611 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7612 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7613 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7614 | 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] | 7615 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7616 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7617 | PyObject *restuple; |
| 7618 | PyObject *resunicode; |
| 7619 | |
| 7620 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7621 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7622 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7623 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7624 | } |
| 7625 | |
| 7626 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7627 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7628 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7629 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7630 | |
| 7631 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7632 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7633 | if (restuple == 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 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7636 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7637 | Py_DECREF(restuple); |
| 7638 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7639 | } |
| 7640 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7641 | &resunicode, &i_newpos)) { |
| 7642 | Py_DECREF(restuple); |
| 7643 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7644 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7645 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7646 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7647 | else |
| 7648 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7649 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7650 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7651 | Py_DECREF(restuple); |
| 7652 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7653 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7654 | Py_INCREF(resunicode); |
| 7655 | Py_DECREF(restuple); |
| 7656 | return resunicode; |
| 7657 | } |
| 7658 | |
| 7659 | /* Lookup the character ch in the mapping and put the result in result, |
| 7660 | which must be decrefed by the caller. |
| 7661 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7662 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7663 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7664 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7665 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7666 | PyObject *x; |
| 7667 | |
| 7668 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7669 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7670 | x = PyObject_GetItem(mapping, w); |
| 7671 | Py_DECREF(w); |
| 7672 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7673 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7674 | /* No mapping found means: use 1:1 mapping. */ |
| 7675 | PyErr_Clear(); |
| 7676 | *result = NULL; |
| 7677 | return 0; |
| 7678 | } else |
| 7679 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7680 | } |
| 7681 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7682 | *result = x; |
| 7683 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7684 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7685 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7686 | long value = PyLong_AS_LONG(x); |
| 7687 | long max = PyUnicode_GetMax(); |
| 7688 | if (value < 0 || value > max) { |
| 7689 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7690 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7691 | Py_DECREF(x); |
| 7692 | return -1; |
| 7693 | } |
| 7694 | *result = x; |
| 7695 | return 0; |
| 7696 | } |
| 7697 | else if (PyUnicode_Check(x)) { |
| 7698 | *result = x; |
| 7699 | return 0; |
| 7700 | } |
| 7701 | else { |
| 7702 | /* wrong return value */ |
| 7703 | PyErr_SetString(PyExc_TypeError, |
| 7704 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7705 | Py_DECREF(x); |
| 7706 | return -1; |
| 7707 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7708 | } |
| 7709 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7710 | if not reallocate and adjust various state variables. |
| 7711 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7712 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7713 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7714 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7715 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7716 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7717 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7718 | /* exponentially overallocate to minimize reallocations */ |
| 7719 | if (requiredsize < 2 * oldsize) |
| 7720 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7721 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7722 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7723 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7724 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7725 | } |
| 7726 | return 0; |
| 7727 | } |
| 7728 | /* lookup the character, put the result in the output string and adjust |
| 7729 | various state variables. Return a new reference to the object that |
| 7730 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 7731 | undefined (in which case no character was written). |
| 7732 | The called must decref result. |
| 7733 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7734 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7735 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 7736 | PyObject *mapping, Py_UCS4 **output, |
| 7737 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7738 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7739 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7740 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 7741 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7742 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7743 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7744 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7745 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7746 | } |
| 7747 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7748 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7749 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7750 | /* 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] | 7751 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7752 | } |
| 7753 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7754 | Py_ssize_t repsize; |
| 7755 | if (PyUnicode_READY(*res) == -1) |
| 7756 | return -1; |
| 7757 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7758 | if (repsize==1) { |
| 7759 | /* 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] | 7760 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7761 | } |
| 7762 | else if (repsize!=0) { |
| 7763 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7764 | Py_ssize_t requiredsize = *opos + |
| 7765 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7766 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7767 | Py_ssize_t i; |
| 7768 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7769 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7770 | for(i = 0; i < repsize; i++) |
| 7771 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7772 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7773 | } |
| 7774 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7775 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7776 | return 0; |
| 7777 | } |
| 7778 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7779 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7780 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 7781 | PyObject *mapping, |
| 7782 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7783 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7784 | /* input object */ |
| 7785 | char *idata; |
| 7786 | Py_ssize_t size, i; |
| 7787 | int kind; |
| 7788 | /* output buffer */ |
| 7789 | Py_UCS4 *output = NULL; |
| 7790 | Py_ssize_t osize; |
| 7791 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7792 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7793 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7794 | char *reason = "character maps to <undefined>"; |
| 7795 | PyObject *errorHandler = NULL; |
| 7796 | PyObject *exc = NULL; |
| 7797 | /* the following variable is used for caching string comparisons |
| 7798 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7799 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7800 | int known_errorHandler = -1; |
| 7801 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7802 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7803 | PyErr_BadArgument(); |
| 7804 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7805 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7806 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7807 | if (PyUnicode_READY(input) == -1) |
| 7808 | return NULL; |
| 7809 | idata = (char*)PyUnicode_DATA(input); |
| 7810 | kind = PyUnicode_KIND(input); |
| 7811 | size = PyUnicode_GET_LENGTH(input); |
| 7812 | i = 0; |
| 7813 | |
| 7814 | if (size == 0) { |
| 7815 | Py_INCREF(input); |
| 7816 | return input; |
| 7817 | } |
| 7818 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7819 | /* allocate enough for a simple 1:1 translation without |
| 7820 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7821 | osize = size; |
| 7822 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 7823 | opos = 0; |
| 7824 | if (output == NULL) { |
| 7825 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7826 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7827 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7828 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7829 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7830 | /* try to encode it */ |
| 7831 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7832 | if (charmaptranslate_output(input, i, mapping, |
| 7833 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7834 | Py_XDECREF(x); |
| 7835 | goto onError; |
| 7836 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7837 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7838 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7839 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7840 | else { /* untranslatable character */ |
| 7841 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 7842 | Py_ssize_t repsize; |
| 7843 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7844 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7845 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7846 | Py_ssize_t collstart = i; |
| 7847 | Py_ssize_t collend = i+1; |
| 7848 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7850 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7851 | while (collend < size) { |
| 7852 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7853 | goto onError; |
| 7854 | Py_XDECREF(x); |
| 7855 | if (x!=Py_None) |
| 7856 | break; |
| 7857 | ++collend; |
| 7858 | } |
| 7859 | /* cache callback name lookup |
| 7860 | * (if not done yet, i.e. it's the first error) */ |
| 7861 | if (known_errorHandler==-1) { |
| 7862 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7863 | known_errorHandler = 1; |
| 7864 | else if (!strcmp(errors, "replace")) |
| 7865 | known_errorHandler = 2; |
| 7866 | else if (!strcmp(errors, "ignore")) |
| 7867 | known_errorHandler = 3; |
| 7868 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7869 | known_errorHandler = 4; |
| 7870 | else |
| 7871 | known_errorHandler = 0; |
| 7872 | } |
| 7873 | switch (known_errorHandler) { |
| 7874 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7875 | raise_translate_exception(&exc, input, collstart, |
| 7876 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7877 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7878 | case 2: /* replace */ |
| 7879 | /* 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] | 7880 | for (coll = collstart; coll<collend; coll++) |
| 7881 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7882 | /* fall through */ |
| 7883 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7884 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7885 | break; |
| 7886 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7887 | /* generate replacement (temporarily (mis)uses i) */ |
| 7888 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7889 | char buffer[2+29+1+1]; |
| 7890 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7891 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 7892 | if (charmaptranslate_makespace(&output, &osize, |
| 7893 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7894 | goto onError; |
| 7895 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7896 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7897 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7898 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7899 | break; |
| 7900 | default: |
| 7901 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7902 | reason, input, &exc, |
| 7903 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7904 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7905 | goto onError; |
| 7906 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7907 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 7908 | if (charmaptranslate_makespace(&output, &osize, |
| 7909 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7910 | Py_DECREF(repunicode); |
| 7911 | goto onError; |
| 7912 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7913 | for (uni2 = 0; repsize-->0; ++uni2) |
| 7914 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 7915 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7916 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7917 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7918 | } |
| 7919 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7920 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 7921 | if (!res) |
| 7922 | goto onError; |
| 7923 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7924 | Py_XDECREF(exc); |
| 7925 | Py_XDECREF(errorHandler); |
| 7926 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7927 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7928 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7929 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7930 | Py_XDECREF(exc); |
| 7931 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7932 | return NULL; |
| 7933 | } |
| 7934 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7935 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 7936 | PyObject * |
| 7937 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 7938 | Py_ssize_t size, |
| 7939 | PyObject *mapping, |
| 7940 | const char *errors) |
| 7941 | { |
| 7942 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7943 | if (!unicode) |
| 7944 | return NULL; |
| 7945 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 7946 | } |
| 7947 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7948 | PyObject * |
| 7949 | PyUnicode_Translate(PyObject *str, |
| 7950 | PyObject *mapping, |
| 7951 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7952 | { |
| 7953 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7954 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7955 | str = PyUnicode_FromObject(str); |
| 7956 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7957 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7958 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7959 | Py_DECREF(str); |
| 7960 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7961 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7962 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7963 | Py_XDECREF(str); |
| 7964 | return NULL; |
| 7965 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7966 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7967 | static Py_UCS4 |
| 7968 | fix_decimal_and_space_to_ascii(PyUnicodeObject *self) |
| 7969 | { |
| 7970 | /* No need to call PyUnicode_READY(self) because this function is only |
| 7971 | called as a callback from fixup() which does it already. */ |
| 7972 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 7973 | const int kind = PyUnicode_KIND(self); |
| 7974 | void *data = PyUnicode_DATA(self); |
| 7975 | Py_UCS4 maxchar = 0, ch, fixed; |
| 7976 | Py_ssize_t i; |
| 7977 | |
| 7978 | for (i = 0; i < len; ++i) { |
| 7979 | ch = PyUnicode_READ(kind, data, i); |
| 7980 | fixed = 0; |
| 7981 | if (ch > 127) { |
| 7982 | if (Py_UNICODE_ISSPACE(ch)) |
| 7983 | fixed = ' '; |
| 7984 | else { |
| 7985 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7986 | if (decimal >= 0) |
| 7987 | fixed = '0' + decimal; |
| 7988 | } |
| 7989 | if (fixed != 0) { |
| 7990 | if (fixed > maxchar) |
| 7991 | maxchar = fixed; |
| 7992 | PyUnicode_WRITE(kind, data, i, fixed); |
| 7993 | } |
| 7994 | else if (ch > maxchar) |
| 7995 | maxchar = ch; |
| 7996 | } |
| 7997 | else if (ch > maxchar) |
| 7998 | maxchar = ch; |
| 7999 | } |
| 8000 | |
| 8001 | return maxchar; |
| 8002 | } |
| 8003 | |
| 8004 | PyObject * |
| 8005 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8006 | { |
| 8007 | if (!PyUnicode_Check(unicode)) { |
| 8008 | PyErr_BadInternalCall(); |
| 8009 | return NULL; |
| 8010 | } |
| 8011 | if (PyUnicode_READY(unicode) == -1) |
| 8012 | return NULL; |
| 8013 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8014 | /* If the string is already ASCII, just return the same string */ |
| 8015 | Py_INCREF(unicode); |
| 8016 | return unicode; |
| 8017 | } |
| 8018 | return fixup((PyUnicodeObject *)unicode, fix_decimal_and_space_to_ascii); |
| 8019 | } |
| 8020 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8021 | PyObject * |
| 8022 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8023 | Py_ssize_t length) |
| 8024 | { |
| 8025 | PyObject *result; |
| 8026 | Py_UNICODE *p; /* write pointer into result */ |
| 8027 | Py_ssize_t i; |
| 8028 | /* Copy to a new string */ |
| 8029 | result = (PyObject *)_PyUnicode_New(length); |
| 8030 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8031 | if (result == NULL) |
| 8032 | return result; |
| 8033 | p = PyUnicode_AS_UNICODE(result); |
| 8034 | /* Iterate over code points */ |
| 8035 | for (i = 0; i < length; i++) { |
| 8036 | Py_UNICODE ch =s[i]; |
| 8037 | if (ch > 127) { |
| 8038 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8039 | if (decimal >= 0) |
| 8040 | p[i] = '0' + decimal; |
| 8041 | } |
| 8042 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8043 | if (PyUnicode_READY((PyUnicodeObject*)result) == -1) { |
| 8044 | Py_DECREF(result); |
| 8045 | return NULL; |
| 8046 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8047 | return result; |
| 8048 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8049 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8050 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8051 | int |
| 8052 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8053 | Py_ssize_t length, |
| 8054 | char *output, |
| 8055 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8056 | { |
| 8057 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8058 | PyObject *errorHandler = NULL; |
| 8059 | PyObject *exc = NULL; |
| 8060 | const char *encoding = "decimal"; |
| 8061 | const char *reason = "invalid decimal Unicode string"; |
| 8062 | /* the following variable is used for caching string comparisons |
| 8063 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8064 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8065 | |
| 8066 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8067 | PyErr_BadArgument(); |
| 8068 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8069 | } |
| 8070 | |
| 8071 | p = s; |
| 8072 | end = s + length; |
| 8073 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8074 | register Py_UNICODE ch = *p; |
| 8075 | int decimal; |
| 8076 | PyObject *repunicode; |
| 8077 | Py_ssize_t repsize; |
| 8078 | Py_ssize_t newpos; |
| 8079 | Py_UNICODE *uni2; |
| 8080 | Py_UNICODE *collstart; |
| 8081 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8082 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8083 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8084 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8085 | ++p; |
| 8086 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8087 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8088 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8089 | if (decimal >= 0) { |
| 8090 | *output++ = '0' + decimal; |
| 8091 | ++p; |
| 8092 | continue; |
| 8093 | } |
| 8094 | if (0 < ch && ch < 256) { |
| 8095 | *output++ = (char)ch; |
| 8096 | ++p; |
| 8097 | continue; |
| 8098 | } |
| 8099 | /* All other characters are considered unencodable */ |
| 8100 | collstart = p; |
| 8101 | collend = p+1; |
| 8102 | while (collend < end) { |
| 8103 | if ((0 < *collend && *collend < 256) || |
| 8104 | !Py_UNICODE_ISSPACE(*collend) || |
| 8105 | Py_UNICODE_TODECIMAL(*collend)) |
| 8106 | break; |
| 8107 | } |
| 8108 | /* cache callback name lookup |
| 8109 | * (if not done yet, i.e. it's the first error) */ |
| 8110 | if (known_errorHandler==-1) { |
| 8111 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8112 | known_errorHandler = 1; |
| 8113 | else if (!strcmp(errors, "replace")) |
| 8114 | known_errorHandler = 2; |
| 8115 | else if (!strcmp(errors, "ignore")) |
| 8116 | known_errorHandler = 3; |
| 8117 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8118 | known_errorHandler = 4; |
| 8119 | else |
| 8120 | known_errorHandler = 0; |
| 8121 | } |
| 8122 | switch (known_errorHandler) { |
| 8123 | case 1: /* strict */ |
| 8124 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8125 | goto onError; |
| 8126 | case 2: /* replace */ |
| 8127 | for (p = collstart; p < collend; ++p) |
| 8128 | *output++ = '?'; |
| 8129 | /* fall through */ |
| 8130 | case 3: /* ignore */ |
| 8131 | p = collend; |
| 8132 | break; |
| 8133 | case 4: /* xmlcharrefreplace */ |
| 8134 | /* generate replacement (temporarily (mis)uses p) */ |
| 8135 | for (p = collstart; p < collend; ++p) |
| 8136 | output += sprintf(output, "&#%d;", (int)*p); |
| 8137 | p = collend; |
| 8138 | break; |
| 8139 | default: |
| 8140 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 8141 | encoding, reason, s, length, &exc, |
| 8142 | collstart-s, collend-s, &newpos); |
| 8143 | if (repunicode == NULL) |
| 8144 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8145 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8146 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8147 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8148 | Py_DECREF(repunicode); |
| 8149 | goto onError; |
| 8150 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8151 | /* generate replacement */ |
| 8152 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8153 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8154 | Py_UNICODE ch = *uni2; |
| 8155 | if (Py_UNICODE_ISSPACE(ch)) |
| 8156 | *output++ = ' '; |
| 8157 | else { |
| 8158 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8159 | if (decimal >= 0) |
| 8160 | *output++ = '0' + decimal; |
| 8161 | else if (0 < ch && ch < 256) |
| 8162 | *output++ = (char)ch; |
| 8163 | else { |
| 8164 | Py_DECREF(repunicode); |
| 8165 | raise_encode_exception(&exc, encoding, |
| 8166 | s, length, collstart-s, collend-s, reason); |
| 8167 | goto onError; |
| 8168 | } |
| 8169 | } |
| 8170 | } |
| 8171 | p = s + newpos; |
| 8172 | Py_DECREF(repunicode); |
| 8173 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8174 | } |
| 8175 | /* 0-terminate the output string */ |
| 8176 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8177 | Py_XDECREF(exc); |
| 8178 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8179 | return 0; |
| 8180 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8181 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8182 | Py_XDECREF(exc); |
| 8183 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8184 | return -1; |
| 8185 | } |
| 8186 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8187 | /* --- Helpers ------------------------------------------------------------ */ |
| 8188 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8189 | #include "stringlib/ucs1lib.h" |
| 8190 | #include "stringlib/fastsearch.h" |
| 8191 | #include "stringlib/partition.h" |
| 8192 | #include "stringlib/split.h" |
| 8193 | #include "stringlib/count.h" |
| 8194 | #include "stringlib/find.h" |
| 8195 | #include "stringlib/localeutil.h" |
| 8196 | #include "stringlib/undef.h" |
| 8197 | |
| 8198 | #include "stringlib/ucs2lib.h" |
| 8199 | #include "stringlib/fastsearch.h" |
| 8200 | #include "stringlib/partition.h" |
| 8201 | #include "stringlib/split.h" |
| 8202 | #include "stringlib/count.h" |
| 8203 | #include "stringlib/find.h" |
| 8204 | #include "stringlib/localeutil.h" |
| 8205 | #include "stringlib/undef.h" |
| 8206 | |
| 8207 | #include "stringlib/ucs4lib.h" |
| 8208 | #include "stringlib/fastsearch.h" |
| 8209 | #include "stringlib/partition.h" |
| 8210 | #include "stringlib/split.h" |
| 8211 | #include "stringlib/count.h" |
| 8212 | #include "stringlib/find.h" |
| 8213 | #include "stringlib/localeutil.h" |
| 8214 | #include "stringlib/undef.h" |
| 8215 | |
| 8216 | static Py_ssize_t |
| 8217 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, |
| 8218 | const Py_UCS1*, Py_ssize_t, |
| 8219 | Py_ssize_t, Py_ssize_t), |
| 8220 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 8221 | const Py_UCS2*, Py_ssize_t, |
| 8222 | Py_ssize_t, Py_ssize_t), |
| 8223 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 8224 | const Py_UCS4*, Py_ssize_t, |
| 8225 | Py_ssize_t, Py_ssize_t), |
| 8226 | PyObject* s1, PyObject* s2, |
| 8227 | Py_ssize_t start, |
| 8228 | Py_ssize_t end) |
| 8229 | { |
| 8230 | int kind1, kind2, kind; |
| 8231 | void *buf1, *buf2; |
| 8232 | Py_ssize_t len1, len2, result; |
| 8233 | |
| 8234 | kind1 = PyUnicode_KIND(s1); |
| 8235 | kind2 = PyUnicode_KIND(s2); |
| 8236 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8237 | buf1 = PyUnicode_DATA(s1); |
| 8238 | buf2 = PyUnicode_DATA(s2); |
| 8239 | if (kind1 != kind) |
| 8240 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8241 | if (!buf1) |
| 8242 | return -2; |
| 8243 | if (kind2 != kind) |
| 8244 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8245 | if (!buf2) { |
| 8246 | if (kind1 != kind) PyMem_Free(buf1); |
| 8247 | return -2; |
| 8248 | } |
| 8249 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8250 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8251 | |
| 8252 | switch(kind) { |
| 8253 | case PyUnicode_1BYTE_KIND: |
| 8254 | result = ucs1(buf1, len1, buf2, len2, start, end); |
| 8255 | break; |
| 8256 | case PyUnicode_2BYTE_KIND: |
| 8257 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 8258 | break; |
| 8259 | case PyUnicode_4BYTE_KIND: |
| 8260 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 8261 | break; |
| 8262 | default: |
| 8263 | assert(0); result = -2; |
| 8264 | } |
| 8265 | |
| 8266 | if (kind1 != kind) |
| 8267 | PyMem_Free(buf1); |
| 8268 | if (kind2 != kind) |
| 8269 | PyMem_Free(buf2); |
| 8270 | |
| 8271 | return result; |
| 8272 | } |
| 8273 | |
| 8274 | Py_ssize_t |
| 8275 | _PyUnicode_InsertThousandsGrouping(int kind, void *data, |
| 8276 | Py_ssize_t n_buffer, |
| 8277 | void *digits, Py_ssize_t n_digits, |
| 8278 | Py_ssize_t min_width, |
| 8279 | const char *grouping, |
| 8280 | const char *thousands_sep) |
| 8281 | { |
| 8282 | switch(kind) { |
| 8283 | case PyUnicode_1BYTE_KIND: |
| 8284 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 8285 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8286 | min_width, grouping, thousands_sep); |
| 8287 | case PyUnicode_2BYTE_KIND: |
| 8288 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 8289 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 8290 | min_width, grouping, thousands_sep); |
| 8291 | case PyUnicode_4BYTE_KIND: |
| 8292 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 8293 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 8294 | min_width, grouping, thousands_sep); |
| 8295 | } |
| 8296 | assert(0); |
| 8297 | return -1; |
| 8298 | } |
| 8299 | |
| 8300 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8301 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8302 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8303 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8304 | #include "stringlib/count.h" |
| 8305 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8306 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8307 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8308 | #define ADJUST_INDICES(start, end, len) \ |
| 8309 | if (end > len) \ |
| 8310 | end = len; \ |
| 8311 | else if (end < 0) { \ |
| 8312 | end += len; \ |
| 8313 | if (end < 0) \ |
| 8314 | end = 0; \ |
| 8315 | } \ |
| 8316 | if (start < 0) { \ |
| 8317 | start += len; \ |
| 8318 | if (start < 0) \ |
| 8319 | start = 0; \ |
| 8320 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8321 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8322 | Py_ssize_t |
| 8323 | PyUnicode_Count(PyObject *str, |
| 8324 | PyObject *substr, |
| 8325 | Py_ssize_t start, |
| 8326 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8327 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8328 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8329 | PyUnicodeObject* str_obj; |
| 8330 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8331 | int kind1, kind2, kind; |
| 8332 | void *buf1 = NULL, *buf2 = NULL; |
| 8333 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8334 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8335 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8336 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8337 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8338 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8339 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8340 | Py_DECREF(str_obj); |
| 8341 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8342 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8343 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8344 | kind1 = PyUnicode_KIND(str_obj); |
| 8345 | kind2 = PyUnicode_KIND(sub_obj); |
| 8346 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8347 | buf1 = PyUnicode_DATA(str_obj); |
| 8348 | if (kind1 != kind) |
| 8349 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 8350 | if (!buf1) |
| 8351 | goto onError; |
| 8352 | buf2 = PyUnicode_DATA(sub_obj); |
| 8353 | if (kind2 != kind) |
| 8354 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 8355 | if (!buf2) |
| 8356 | goto onError; |
| 8357 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8358 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8359 | |
| 8360 | ADJUST_INDICES(start, end, len1); |
| 8361 | switch(kind) { |
| 8362 | case PyUnicode_1BYTE_KIND: |
| 8363 | result = ucs1lib_count( |
| 8364 | ((Py_UCS1*)buf1) + start, end - start, |
| 8365 | buf2, len2, PY_SSIZE_T_MAX |
| 8366 | ); |
| 8367 | break; |
| 8368 | case PyUnicode_2BYTE_KIND: |
| 8369 | result = ucs2lib_count( |
| 8370 | ((Py_UCS2*)buf1) + start, end - start, |
| 8371 | buf2, len2, PY_SSIZE_T_MAX |
| 8372 | ); |
| 8373 | break; |
| 8374 | case PyUnicode_4BYTE_KIND: |
| 8375 | result = ucs4lib_count( |
| 8376 | ((Py_UCS4*)buf1) + start, end - start, |
| 8377 | buf2, len2, PY_SSIZE_T_MAX |
| 8378 | ); |
| 8379 | break; |
| 8380 | default: |
| 8381 | assert(0); result = 0; |
| 8382 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8383 | |
| 8384 | Py_DECREF(sub_obj); |
| 8385 | Py_DECREF(str_obj); |
| 8386 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8387 | if (kind1 != kind) |
| 8388 | PyMem_Free(buf1); |
| 8389 | if (kind2 != kind) |
| 8390 | PyMem_Free(buf2); |
| 8391 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8392 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8393 | onError: |
| 8394 | Py_DECREF(sub_obj); |
| 8395 | Py_DECREF(str_obj); |
| 8396 | if (kind1 != kind && buf1) |
| 8397 | PyMem_Free(buf1); |
| 8398 | if (kind2 != kind && buf2) |
| 8399 | PyMem_Free(buf2); |
| 8400 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8401 | } |
| 8402 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8403 | Py_ssize_t |
| 8404 | PyUnicode_Find(PyObject *str, |
| 8405 | PyObject *sub, |
| 8406 | Py_ssize_t start, |
| 8407 | Py_ssize_t end, |
| 8408 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8409 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8410 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8411 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8412 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8413 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8414 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8415 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8416 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8417 | Py_DECREF(str); |
| 8418 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8419 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8420 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8421 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8422 | result = any_find_slice( |
| 8423 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 8424 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8425 | ); |
| 8426 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8427 | result = any_find_slice( |
| 8428 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 8429 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8430 | ); |
| 8431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8432 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8433 | Py_DECREF(sub); |
| 8434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8435 | return result; |
| 8436 | } |
| 8437 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8438 | Py_ssize_t |
| 8439 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8440 | Py_ssize_t start, Py_ssize_t end, |
| 8441 | int direction) |
| 8442 | { |
| 8443 | char *result; |
| 8444 | int kind; |
| 8445 | if (PyUnicode_READY(str) == -1) |
| 8446 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8447 | if (start < 0 || end < 0) { |
| 8448 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8449 | return -2; |
| 8450 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8451 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8452 | end = PyUnicode_GET_LENGTH(str); |
| 8453 | kind = PyUnicode_KIND(str); |
| 8454 | result = findchar(PyUnicode_1BYTE_DATA(str) |
| 8455 | + PyUnicode_KIND_SIZE(kind, start), |
| 8456 | kind, |
| 8457 | end-start, ch, direction); |
| 8458 | if (!result) |
| 8459 | return -1; |
| 8460 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8461 | } |
| 8462 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8463 | static int |
| 8464 | tailmatch(PyUnicodeObject *self, |
| 8465 | PyUnicodeObject *substring, |
| 8466 | Py_ssize_t start, |
| 8467 | Py_ssize_t end, |
| 8468 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8469 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8470 | int kind_self; |
| 8471 | int kind_sub; |
| 8472 | void *data_self; |
| 8473 | void *data_sub; |
| 8474 | Py_ssize_t offset; |
| 8475 | Py_ssize_t i; |
| 8476 | Py_ssize_t end_sub; |
| 8477 | |
| 8478 | if (PyUnicode_READY(self) == -1 || |
| 8479 | PyUnicode_READY(substring) == -1) |
| 8480 | return 0; |
| 8481 | |
| 8482 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8483 | return 1; |
| 8484 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8485 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8486 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8487 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8488 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8489 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8490 | kind_self = PyUnicode_KIND(self); |
| 8491 | data_self = PyUnicode_DATA(self); |
| 8492 | kind_sub = PyUnicode_KIND(substring); |
| 8493 | data_sub = PyUnicode_DATA(substring); |
| 8494 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8495 | |
| 8496 | if (direction > 0) |
| 8497 | offset = end; |
| 8498 | else |
| 8499 | offset = start; |
| 8500 | |
| 8501 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8502 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8503 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8504 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8505 | /* If both are of the same kind, memcmp is sufficient */ |
| 8506 | if (kind_self == kind_sub) { |
| 8507 | return ! memcmp((char *)data_self + |
| 8508 | (offset * PyUnicode_CHARACTER_SIZE(substring)), |
| 8509 | data_sub, |
| 8510 | PyUnicode_GET_LENGTH(substring) * |
| 8511 | PyUnicode_CHARACTER_SIZE(substring)); |
| 8512 | } |
| 8513 | /* otherwise we have to compare each character by first accesing it */ |
| 8514 | else { |
| 8515 | /* We do not need to compare 0 and len(substring)-1 because |
| 8516 | the if statement above ensured already that they are equal |
| 8517 | when we end up here. */ |
| 8518 | // TODO: honor direction and do a forward or backwards search |
| 8519 | for (i = 1; i < end_sub; ++i) { |
| 8520 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8521 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8522 | return 0; |
| 8523 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8524 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8525 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8526 | } |
| 8527 | |
| 8528 | return 0; |
| 8529 | } |
| 8530 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8531 | Py_ssize_t |
| 8532 | PyUnicode_Tailmatch(PyObject *str, |
| 8533 | PyObject *substr, |
| 8534 | Py_ssize_t start, |
| 8535 | Py_ssize_t end, |
| 8536 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8537 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8538 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8539 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8540 | str = PyUnicode_FromObject(str); |
| 8541 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8542 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8543 | substr = PyUnicode_FromObject(substr); |
| 8544 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8545 | Py_DECREF(str); |
| 8546 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8547 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8548 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8549 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8550 | (PyUnicodeObject *)substr, |
| 8551 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8552 | Py_DECREF(str); |
| 8553 | Py_DECREF(substr); |
| 8554 | return result; |
| 8555 | } |
| 8556 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8557 | /* Apply fixfct filter to the Unicode object self and return a |
| 8558 | reference to the modified object */ |
| 8559 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8560 | static PyObject * |
| 8561 | fixup(PyUnicodeObject *self, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8562 | Py_UCS4 (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8563 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8564 | PyObject *u; |
| 8565 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8566 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8567 | if (PyUnicode_READY(self) == -1) |
| 8568 | return NULL; |
| 8569 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8570 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8571 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8572 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8573 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8574 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8575 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
| 8576 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8577 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8578 | /* fix functions return the new maximum character in a string, |
| 8579 | if the kind of the resulting unicode object does not change, |
| 8580 | everything is fine. Otherwise we need to change the string kind |
| 8581 | and re-run the fix function. */ |
| 8582 | maxchar_new = fixfct((PyUnicodeObject*)u); |
| 8583 | if (maxchar_new == 0) |
| 8584 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8585 | else if (maxchar_new <= 127) |
| 8586 | maxchar_new = 127; |
| 8587 | else if (maxchar_new <= 255) |
| 8588 | maxchar_new = 255; |
| 8589 | else if (maxchar_new <= 65535) |
| 8590 | maxchar_new = 65535; |
| 8591 | else |
| 8592 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8593 | |
| 8594 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8595 | /* fixfct should return TRUE if it modified the buffer. If |
| 8596 | FALSE, return a reference to the original buffer instead |
| 8597 | (to save space, not time) */ |
| 8598 | Py_INCREF(self); |
| 8599 | Py_DECREF(u); |
| 8600 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8601 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8602 | else if (maxchar_new == maxchar_old) { |
| 8603 | return u; |
| 8604 | } |
| 8605 | else { |
| 8606 | /* In case the maximum character changed, we need to |
| 8607 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8608 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8609 | if (v == NULL) { |
| 8610 | Py_DECREF(u); |
| 8611 | return NULL; |
| 8612 | } |
| 8613 | if (maxchar_new > maxchar_old) { |
| 8614 | /* If the maxchar increased so that the kind changed, not all |
| 8615 | characters are representable anymore and we need to fix the |
| 8616 | string again. This only happens in very few cases. */ |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8617 | if (PyUnicode_CopyCharacters(v, 0, |
| 8618 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8619 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8620 | { |
| 8621 | Py_DECREF(u); |
| 8622 | return NULL; |
| 8623 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8624 | maxchar_old = fixfct((PyUnicodeObject*)v); |
| 8625 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8626 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8627 | else { |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8628 | if (PyUnicode_CopyCharacters(v, 0, |
| 8629 | u, 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 | } |
| 8635 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8636 | |
| 8637 | Py_DECREF(u); |
| 8638 | return v; |
| 8639 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8640 | } |
| 8641 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8642 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8643 | fixupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8644 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8645 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8646 | called as a callback from fixup() which does it already. */ |
| 8647 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8648 | const int kind = PyUnicode_KIND(self); |
| 8649 | void *data = PyUnicode_DATA(self); |
| 8650 | int touched = 0; |
| 8651 | Py_UCS4 maxchar = 0; |
| 8652 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8653 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8654 | for (i = 0; i < len; ++i) { |
| 8655 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8656 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8657 | if (up != ch) { |
| 8658 | if (up > maxchar) |
| 8659 | maxchar = up; |
| 8660 | PyUnicode_WRITE(kind, data, i, up); |
| 8661 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8662 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8663 | else if (ch > maxchar) |
| 8664 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8665 | } |
| 8666 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8667 | if (touched) |
| 8668 | return maxchar; |
| 8669 | else |
| 8670 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8671 | } |
| 8672 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8673 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8674 | fixlower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8675 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8676 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8677 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8678 | const int kind = PyUnicode_KIND(self); |
| 8679 | void *data = PyUnicode_DATA(self); |
| 8680 | int touched = 0; |
| 8681 | Py_UCS4 maxchar = 0; |
| 8682 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8683 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8684 | for(i = 0; i < len; ++i) { |
| 8685 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8686 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8687 | if (lo != ch) { |
| 8688 | if (lo > maxchar) |
| 8689 | maxchar = lo; |
| 8690 | PyUnicode_WRITE(kind, data, i, lo); |
| 8691 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8692 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8693 | else if (ch > maxchar) |
| 8694 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8695 | } |
| 8696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8697 | if (touched) |
| 8698 | return maxchar; |
| 8699 | else |
| 8700 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8701 | } |
| 8702 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8703 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8704 | fixswapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8705 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8706 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8707 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8708 | const int kind = PyUnicode_KIND(self); |
| 8709 | void *data = PyUnicode_DATA(self); |
| 8710 | int touched = 0; |
| 8711 | Py_UCS4 maxchar = 0; |
| 8712 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8713 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8714 | for(i = 0; i < len; ++i) { |
| 8715 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8716 | Py_UCS4 nu = 0; |
| 8717 | |
| 8718 | if (Py_UNICODE_ISUPPER(ch)) |
| 8719 | nu = Py_UNICODE_TOLOWER(ch); |
| 8720 | else if (Py_UNICODE_ISLOWER(ch)) |
| 8721 | nu = Py_UNICODE_TOUPPER(ch); |
| 8722 | |
| 8723 | if (nu != 0) { |
| 8724 | if (nu > maxchar) |
| 8725 | maxchar = nu; |
| 8726 | PyUnicode_WRITE(kind, data, i, nu); |
| 8727 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8728 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8729 | else if (ch > maxchar) |
| 8730 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8731 | } |
| 8732 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8733 | if (touched) |
| 8734 | return maxchar; |
| 8735 | else |
| 8736 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8737 | } |
| 8738 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8739 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8740 | fixcapitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8741 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8742 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8743 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8744 | const int kind = PyUnicode_KIND(self); |
| 8745 | void *data = PyUnicode_DATA(self); |
| 8746 | int touched = 0; |
| 8747 | Py_UCS4 maxchar = 0; |
| 8748 | Py_ssize_t i = 0; |
| 8749 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8750 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8751 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8752 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8753 | |
| 8754 | ch = PyUnicode_READ(kind, data, i); |
| 8755 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 8756 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 8757 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 8758 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8759 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8760 | ++i; |
| 8761 | for(; i < len; ++i) { |
| 8762 | ch = PyUnicode_READ(kind, data, i); |
| 8763 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 8764 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8765 | if (lo > maxchar) |
| 8766 | maxchar = lo; |
| 8767 | PyUnicode_WRITE(kind, data, i, lo); |
| 8768 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8769 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8770 | else if (ch > maxchar) |
| 8771 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8772 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8773 | |
| 8774 | if (touched) |
| 8775 | return maxchar; |
| 8776 | else |
| 8777 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8778 | } |
| 8779 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8780 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8781 | fixtitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8782 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8783 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8784 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8785 | const int kind = PyUnicode_KIND(self); |
| 8786 | void *data = PyUnicode_DATA(self); |
| 8787 | Py_UCS4 maxchar = 0; |
| 8788 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8789 | int previous_is_cased; |
| 8790 | |
| 8791 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8792 | if (len == 1) { |
| 8793 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8794 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 8795 | if (ti != ch) { |
| 8796 | PyUnicode_WRITE(kind, data, i, ti); |
| 8797 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8798 | } |
| 8799 | else |
| 8800 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8801 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8802 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8803 | for(; i < len; ++i) { |
| 8804 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8805 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8806 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8807 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8808 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8809 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8810 | nu = Py_UNICODE_TOTITLE(ch); |
| 8811 | |
| 8812 | if (nu > maxchar) |
| 8813 | maxchar = nu; |
| 8814 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8815 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8816 | if (Py_UNICODE_ISLOWER(ch) || |
| 8817 | Py_UNICODE_ISUPPER(ch) || |
| 8818 | Py_UNICODE_ISTITLE(ch)) |
| 8819 | previous_is_cased = 1; |
| 8820 | else |
| 8821 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8822 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8823 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8824 | } |
| 8825 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8826 | PyObject * |
| 8827 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8828 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8829 | PyObject *sep = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8830 | Py_ssize_t seplen = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8831 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8832 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8833 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 8834 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8835 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8836 | Py_ssize_t sz, i, res_offset; |
| 8837 | Py_UCS4 maxchar = 0; |
| 8838 | Py_UCS4 item_maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8839 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8840 | fseq = PySequence_Fast(seq, ""); |
| 8841 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8842 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8843 | } |
| 8844 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8845 | /* NOTE: the following code can't call back into Python code, |
| 8846 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8847 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8848 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8849 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 8850 | /* If empty sequence, return u"". */ |
| 8851 | if (seqlen == 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8852 | res = PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8853 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8854 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8855 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8856 | /* If singleton sequence with an exact Unicode, return that. */ |
| 8857 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8858 | item = items[0]; |
| 8859 | if (PyUnicode_CheckExact(item)) { |
| 8860 | Py_INCREF(item); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8861 | res = item; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8862 | goto Done; |
| 8863 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8864 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8865 | else { |
| 8866 | /* Set up sep and seplen */ |
| 8867 | if (separator == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8868 | /* fall back to a blank space separator */ |
| 8869 | sep = PyUnicode_FromOrdinal(' '); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8870 | if (!sep) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8871 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8872 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8873 | else { |
| 8874 | if (!PyUnicode_Check(separator)) { |
| 8875 | PyErr_Format(PyExc_TypeError, |
| 8876 | "separator: expected str instance," |
| 8877 | " %.80s found", |
| 8878 | Py_TYPE(separator)->tp_name); |
| 8879 | goto onError; |
| 8880 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8881 | if (PyUnicode_READY(separator)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8882 | goto onError; |
| 8883 | sep = separator; |
| 8884 | seplen = PyUnicode_GET_LENGTH(separator); |
| 8885 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 8886 | /* inc refcount to keep this code path symetric with the |
| 8887 | above case of a blank separator */ |
| 8888 | Py_INCREF(sep); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8889 | } |
| 8890 | } |
| 8891 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8892 | /* There are at least two things to join, or else we have a subclass |
| 8893 | * of str in the sequence. |
| 8894 | * Do a pre-pass to figure out the total amount of space we'll |
| 8895 | * need (sz), and see whether all argument are strings. |
| 8896 | */ |
| 8897 | sz = 0; |
| 8898 | for (i = 0; i < seqlen; i++) { |
| 8899 | const Py_ssize_t old_sz = sz; |
| 8900 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8901 | if (!PyUnicode_Check(item)) { |
| 8902 | PyErr_Format(PyExc_TypeError, |
| 8903 | "sequence item %zd: expected str instance," |
| 8904 | " %.80s found", |
| 8905 | i, Py_TYPE(item)->tp_name); |
| 8906 | goto onError; |
| 8907 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8908 | if (PyUnicode_READY(item) == -1) |
| 8909 | goto onError; |
| 8910 | sz += PyUnicode_GET_LENGTH(item); |
| 8911 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
| 8912 | if (item_maxchar > maxchar) |
| 8913 | maxchar = item_maxchar; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8914 | if (i != 0) |
| 8915 | sz += seplen; |
| 8916 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 8917 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8918 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8919 | goto onError; |
| 8920 | } |
| 8921 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8922 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8923 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8924 | if (res == NULL) |
| 8925 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8926 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8927 | /* Catenate everything. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8928 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8929 | Py_ssize_t itemlen, copied; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8930 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8931 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8932 | if (i && seplen != 0) { |
| 8933 | copied = PyUnicode_CopyCharacters(res, res_offset, |
| 8934 | sep, 0, seplen); |
| 8935 | if (copied < 0) |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8936 | goto onError; |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8937 | #ifdef Py_DEBUG |
| 8938 | res_offset += copied; |
| 8939 | #else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8940 | res_offset += seplen; |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8941 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8942 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8943 | itemlen = PyUnicode_GET_LENGTH(item); |
| 8944 | if (itemlen != 0) { |
| 8945 | copied = PyUnicode_CopyCharacters(res, res_offset, |
| 8946 | item, 0, itemlen); |
| 8947 | if (copied < 0) |
| 8948 | goto onError; |
| 8949 | #ifdef Py_DEBUG |
| 8950 | res_offset += copied; |
| 8951 | #else |
| 8952 | res_offset += itemlen; |
| 8953 | #endif |
| 8954 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8955 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8956 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8957 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8958 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8959 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8960 | Py_XDECREF(sep); |
| 8961 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8962 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8963 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8964 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8965 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8966 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8967 | return NULL; |
| 8968 | } |
| 8969 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8970 | #define FILL(kind, data, value, start, length) \ |
| 8971 | do { \ |
| 8972 | Py_ssize_t i_ = 0; \ |
| 8973 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 8974 | switch ((kind)) { \ |
| 8975 | case PyUnicode_1BYTE_KIND: { \ |
| 8976 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 8977 | memset(to_, (unsigned char)value, length); \ |
| 8978 | break; \ |
| 8979 | } \ |
| 8980 | case PyUnicode_2BYTE_KIND: { \ |
| 8981 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 8982 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8983 | break; \ |
| 8984 | } \ |
| 8985 | default: { \ |
| 8986 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 8987 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8988 | break; \ |
| 8989 | } \ |
| 8990 | } \ |
| 8991 | } while (0) |
| 8992 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8993 | static PyUnicodeObject * |
| 8994 | pad(PyUnicodeObject *self, |
| 8995 | Py_ssize_t left, |
| 8996 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8997 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8998 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8999 | PyObject *u; |
| 9000 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9001 | int kind; |
| 9002 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9003 | |
| 9004 | if (left < 0) |
| 9005 | left = 0; |
| 9006 | if (right < 0) |
| 9007 | right = 0; |
| 9008 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9009 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9010 | Py_INCREF(self); |
| 9011 | return self; |
| 9012 | } |
| 9013 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9014 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9015 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9016 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9017 | return NULL; |
| 9018 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9019 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9020 | if (fill > maxchar) |
| 9021 | maxchar = fill; |
| 9022 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9023 | if (!u) |
| 9024 | return NULL; |
| 9025 | |
| 9026 | kind = PyUnicode_KIND(u); |
| 9027 | data = PyUnicode_DATA(u); |
| 9028 | if (left) |
| 9029 | FILL(kind, data, fill, 0, left); |
| 9030 | if (right) |
| 9031 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9032 | if (PyUnicode_CopyCharacters(u, left, |
| 9033 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9034 | _PyUnicode_LENGTH(self)) < 0) |
| 9035 | { |
| 9036 | Py_DECREF(u); |
| 9037 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9038 | } |
| 9039 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9040 | return (PyUnicodeObject*)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9041 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9042 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9043 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9044 | PyObject * |
| 9045 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9046 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9047 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9048 | |
| 9049 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9050 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9051 | return NULL; |
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 | switch(PyUnicode_KIND(string)) { |
| 9054 | case PyUnicode_1BYTE_KIND: |
| 9055 | list = ucs1lib_splitlines( |
| 9056 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9057 | PyUnicode_GET_LENGTH(string), keepends); |
| 9058 | break; |
| 9059 | case PyUnicode_2BYTE_KIND: |
| 9060 | list = ucs2lib_splitlines( |
| 9061 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 9062 | PyUnicode_GET_LENGTH(string), keepends); |
| 9063 | break; |
| 9064 | case PyUnicode_4BYTE_KIND: |
| 9065 | list = ucs4lib_splitlines( |
| 9066 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 9067 | PyUnicode_GET_LENGTH(string), keepends); |
| 9068 | break; |
| 9069 | default: |
| 9070 | assert(0); |
| 9071 | list = 0; |
| 9072 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9073 | Py_DECREF(string); |
| 9074 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9075 | } |
| 9076 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9077 | static PyObject * |
| 9078 | split(PyUnicodeObject *self, |
| 9079 | PyUnicodeObject *substring, |
| 9080 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9081 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9082 | int kind1, kind2, kind; |
| 9083 | void *buf1, *buf2; |
| 9084 | Py_ssize_t len1, len2; |
| 9085 | PyObject* out; |
| 9086 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9087 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9088 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9089 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9090 | if (PyUnicode_READY(self) == -1) |
| 9091 | return NULL; |
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 | if (substring == NULL) |
| 9094 | switch(PyUnicode_KIND(self)) { |
| 9095 | case PyUnicode_1BYTE_KIND: |
| 9096 | return ucs1lib_split_whitespace( |
| 9097 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9098 | PyUnicode_GET_LENGTH(self), maxcount |
| 9099 | ); |
| 9100 | case PyUnicode_2BYTE_KIND: |
| 9101 | return ucs2lib_split_whitespace( |
| 9102 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9103 | PyUnicode_GET_LENGTH(self), maxcount |
| 9104 | ); |
| 9105 | case PyUnicode_4BYTE_KIND: |
| 9106 | return ucs4lib_split_whitespace( |
| 9107 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9108 | PyUnicode_GET_LENGTH(self), maxcount |
| 9109 | ); |
| 9110 | default: |
| 9111 | assert(0); |
| 9112 | return NULL; |
| 9113 | } |
| 9114 | |
| 9115 | if (PyUnicode_READY(substring) == -1) |
| 9116 | return NULL; |
| 9117 | |
| 9118 | kind1 = PyUnicode_KIND(self); |
| 9119 | kind2 = PyUnicode_KIND(substring); |
| 9120 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9121 | buf1 = PyUnicode_DATA(self); |
| 9122 | buf2 = PyUnicode_DATA(substring); |
| 9123 | if (kind1 != kind) |
| 9124 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9125 | if (!buf1) |
| 9126 | return NULL; |
| 9127 | if (kind2 != kind) |
| 9128 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9129 | if (!buf2) { |
| 9130 | if (kind1 != kind) PyMem_Free(buf1); |
| 9131 | return NULL; |
| 9132 | } |
| 9133 | len1 = PyUnicode_GET_LENGTH(self); |
| 9134 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9135 | |
| 9136 | switch(kind) { |
| 9137 | case PyUnicode_1BYTE_KIND: |
| 9138 | out = ucs1lib_split( |
| 9139 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9140 | break; |
| 9141 | case PyUnicode_2BYTE_KIND: |
| 9142 | out = ucs2lib_split( |
| 9143 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9144 | break; |
| 9145 | case PyUnicode_4BYTE_KIND: |
| 9146 | out = ucs4lib_split( |
| 9147 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9148 | break; |
| 9149 | default: |
| 9150 | out = NULL; |
| 9151 | } |
| 9152 | if (kind1 != kind) |
| 9153 | PyMem_Free(buf1); |
| 9154 | if (kind2 != kind) |
| 9155 | PyMem_Free(buf2); |
| 9156 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9157 | } |
| 9158 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9159 | static PyObject * |
| 9160 | rsplit(PyUnicodeObject *self, |
| 9161 | PyUnicodeObject *substring, |
| 9162 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9163 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9164 | int kind1, kind2, kind; |
| 9165 | void *buf1, *buf2; |
| 9166 | Py_ssize_t len1, len2; |
| 9167 | PyObject* out; |
| 9168 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9169 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9170 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9172 | if (PyUnicode_READY(self) == -1) |
| 9173 | return NULL; |
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 | if (substring == NULL) |
| 9176 | switch(PyUnicode_KIND(self)) { |
| 9177 | case PyUnicode_1BYTE_KIND: |
| 9178 | return ucs1lib_rsplit_whitespace( |
| 9179 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9180 | PyUnicode_GET_LENGTH(self), maxcount |
| 9181 | ); |
| 9182 | case PyUnicode_2BYTE_KIND: |
| 9183 | return ucs2lib_rsplit_whitespace( |
| 9184 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9185 | PyUnicode_GET_LENGTH(self), maxcount |
| 9186 | ); |
| 9187 | case PyUnicode_4BYTE_KIND: |
| 9188 | return ucs4lib_rsplit_whitespace( |
| 9189 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9190 | PyUnicode_GET_LENGTH(self), maxcount |
| 9191 | ); |
| 9192 | default: |
| 9193 | assert(0); |
| 9194 | return NULL; |
| 9195 | } |
| 9196 | |
| 9197 | if (PyUnicode_READY(substring) == -1) |
| 9198 | return NULL; |
| 9199 | |
| 9200 | kind1 = PyUnicode_KIND(self); |
| 9201 | kind2 = PyUnicode_KIND(substring); |
| 9202 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9203 | buf1 = PyUnicode_DATA(self); |
| 9204 | buf2 = PyUnicode_DATA(substring); |
| 9205 | if (kind1 != kind) |
| 9206 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9207 | if (!buf1) |
| 9208 | return NULL; |
| 9209 | if (kind2 != kind) |
| 9210 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9211 | if (!buf2) { |
| 9212 | if (kind1 != kind) PyMem_Free(buf1); |
| 9213 | return NULL; |
| 9214 | } |
| 9215 | len1 = PyUnicode_GET_LENGTH(self); |
| 9216 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9217 | |
| 9218 | switch(kind) { |
| 9219 | case PyUnicode_1BYTE_KIND: |
| 9220 | out = ucs1lib_rsplit( |
| 9221 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9222 | break; |
| 9223 | case PyUnicode_2BYTE_KIND: |
| 9224 | out = ucs2lib_rsplit( |
| 9225 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9226 | break; |
| 9227 | case PyUnicode_4BYTE_KIND: |
| 9228 | out = ucs4lib_rsplit( |
| 9229 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9230 | break; |
| 9231 | default: |
| 9232 | out = NULL; |
| 9233 | } |
| 9234 | if (kind1 != kind) |
| 9235 | PyMem_Free(buf1); |
| 9236 | if (kind2 != kind) |
| 9237 | PyMem_Free(buf2); |
| 9238 | return out; |
| 9239 | } |
| 9240 | |
| 9241 | static Py_ssize_t |
| 9242 | anylib_find(int kind, void *buf1, Py_ssize_t len1, |
| 9243 | void *buf2, Py_ssize_t len2, Py_ssize_t offset) |
| 9244 | { |
| 9245 | switch(kind) { |
| 9246 | case PyUnicode_1BYTE_KIND: |
| 9247 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
| 9248 | case PyUnicode_2BYTE_KIND: |
| 9249 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9250 | case PyUnicode_4BYTE_KIND: |
| 9251 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9252 | } |
| 9253 | assert(0); |
| 9254 | return -1; |
| 9255 | } |
| 9256 | |
| 9257 | static Py_ssize_t |
| 9258 | anylib_count(int kind, void* sbuf, Py_ssize_t slen, |
| 9259 | void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) |
| 9260 | { |
| 9261 | switch(kind) { |
| 9262 | case PyUnicode_1BYTE_KIND: |
| 9263 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9264 | case PyUnicode_2BYTE_KIND: |
| 9265 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9266 | case PyUnicode_4BYTE_KIND: |
| 9267 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9268 | } |
| 9269 | assert(0); |
| 9270 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9271 | } |
| 9272 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9273 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9274 | replace(PyObject *self, PyObject *str1, |
| 9275 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9276 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9277 | PyObject *u; |
| 9278 | char *sbuf = PyUnicode_DATA(self); |
| 9279 | char *buf1 = PyUnicode_DATA(str1); |
| 9280 | char *buf2 = PyUnicode_DATA(str2); |
| 9281 | int srelease = 0, release1 = 0, release2 = 0; |
| 9282 | int skind = PyUnicode_KIND(self); |
| 9283 | int kind1 = PyUnicode_KIND(str1); |
| 9284 | int kind2 = PyUnicode_KIND(str2); |
| 9285 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9286 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9287 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9288 | |
| 9289 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9290 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9291 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9292 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9293 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9294 | if (skind < kind1) |
| 9295 | /* substring too wide to be present */ |
| 9296 | goto nothing; |
| 9297 | |
| 9298 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 9299 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9300 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9301 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9302 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9303 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9304 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9305 | Py_UCS4 u1, u2, maxchar; |
| 9306 | int mayshrink, rkind; |
| 9307 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 9308 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 9309 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9310 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9311 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 9312 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9313 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 9314 | result string. */ |
| 9315 | mayshrink = maxchar > 127; |
| 9316 | if (u2 > maxchar) { |
| 9317 | maxchar = u2; |
| 9318 | mayshrink = 0; |
| 9319 | } |
| 9320 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9321 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9322 | goto error; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9323 | if (PyUnicode_CopyCharacters(u, 0, |
| 9324 | (PyObject*)self, 0, slen) < 0) |
| 9325 | { |
| 9326 | Py_DECREF(u); |
| 9327 | return NULL; |
| 9328 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9329 | rkind = PyUnicode_KIND(u); |
| 9330 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 9331 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9332 | if (--maxcount < 0) |
| 9333 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9334 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9335 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9336 | if (mayshrink) { |
| 9337 | PyObject *tmp = u; |
| 9338 | u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp), |
| 9339 | PyUnicode_GET_LENGTH(tmp)); |
| 9340 | Py_DECREF(tmp); |
| 9341 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9342 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9343 | int rkind = skind; |
| 9344 | char *res; |
| 9345 | if (kind1 < rkind) { |
| 9346 | /* widen substring */ |
| 9347 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9348 | if (!buf1) goto error; |
| 9349 | release1 = 1; |
| 9350 | } |
| 9351 | i = anylib_find(rkind, sbuf, slen, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9352 | if (i < 0) |
| 9353 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9354 | if (rkind > kind2) { |
| 9355 | /* widen replacement */ |
| 9356 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9357 | if (!buf2) goto error; |
| 9358 | release2 = 1; |
| 9359 | } |
| 9360 | else if (rkind < kind2) { |
| 9361 | /* widen self and buf1 */ |
| 9362 | rkind = kind2; |
| 9363 | if (release1) PyMem_Free(buf1); |
| 9364 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9365 | if (!sbuf) goto error; |
| 9366 | srelease = 1; |
| 9367 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9368 | if (!buf1) goto error; |
| 9369 | release1 = 1; |
| 9370 | } |
| 9371 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen)); |
| 9372 | if (!res) { |
| 9373 | PyErr_NoMemory(); |
| 9374 | goto error; |
| 9375 | } |
| 9376 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9377 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9378 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9379 | buf2, |
| 9380 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9381 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9382 | |
| 9383 | while ( --maxcount > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9384 | i = anylib_find(rkind, sbuf+PyUnicode_KIND_SIZE(rkind, i), |
| 9385 | slen-i, |
| 9386 | buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9387 | if (i == -1) |
| 9388 | break; |
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 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9394 | |
| 9395 | u = PyUnicode_FromKindAndData(rkind, res, slen); |
| 9396 | PyMem_Free(res); |
| 9397 | if (!u) goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9398 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9399 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9400 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9401 | Py_ssize_t n, i, j, ires; |
| 9402 | Py_ssize_t product, new_size; |
| 9403 | int rkind = skind; |
| 9404 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9405 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9406 | if (kind1 < rkind) { |
| 9407 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9408 | if (!buf1) goto error; |
| 9409 | release1 = 1; |
| 9410 | } |
| 9411 | n = anylib_count(rkind, sbuf, slen, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9412 | if (n == 0) |
| 9413 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9414 | if (kind2 < rkind) { |
| 9415 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9416 | if (!buf2) goto error; |
| 9417 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9418 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9419 | else if (kind2 > rkind) { |
| 9420 | rkind = kind2; |
| 9421 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9422 | if (!sbuf) goto error; |
| 9423 | srelease = 1; |
| 9424 | if (release1) PyMem_Free(buf1); |
| 9425 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9426 | if (!buf1) goto error; |
| 9427 | release1 = 1; |
| 9428 | } |
| 9429 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9430 | PyUnicode_GET_LENGTH(str1))); */ |
| 9431 | product = n * (len2-len1); |
| 9432 | if ((product / (len2-len1)) != n) { |
| 9433 | PyErr_SetString(PyExc_OverflowError, |
| 9434 | "replace string is too long"); |
| 9435 | goto error; |
| 9436 | } |
| 9437 | new_size = slen + product; |
| 9438 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9439 | PyErr_SetString(PyExc_OverflowError, |
| 9440 | "replace string is too long"); |
| 9441 | goto error; |
| 9442 | } |
| 9443 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size)); |
| 9444 | if (!res) |
| 9445 | goto error; |
| 9446 | ires = i = 0; |
| 9447 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9448 | while (n-- > 0) { |
| 9449 | /* look for next match */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9450 | j = anylib_find(rkind, |
| 9451 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9452 | slen-i, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9453 | if (j == -1) |
| 9454 | break; |
| 9455 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9456 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9457 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9458 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9459 | PyUnicode_KIND_SIZE(rkind, j-i)); |
| 9460 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9461 | } |
| 9462 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9463 | if (len2 > 0) { |
| 9464 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9465 | buf2, |
| 9466 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9467 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9468 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9469 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9470 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9471 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9472 | /* copy tail [i:] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9473 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9474 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9475 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9476 | } else { |
| 9477 | /* interleave */ |
| 9478 | while (n > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9479 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9480 | buf2, |
| 9481 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9482 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9483 | if (--n <= 0) |
| 9484 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9485 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9486 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9487 | PyUnicode_KIND_SIZE(rkind, 1)); |
| 9488 | ires++; |
| 9489 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9490 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9491 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9492 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9493 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9494 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9495 | u = PyUnicode_FromKindAndData(rkind, res, new_size); |
Martin v. Löwis | 0b1d348 | 2011-10-01 16:35:40 +0200 | [diff] [blame] | 9496 | PyMem_Free(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9497 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9498 | if (srelease) |
| 9499 | PyMem_FREE(sbuf); |
| 9500 | if (release1) |
| 9501 | PyMem_FREE(buf1); |
| 9502 | if (release2) |
| 9503 | PyMem_FREE(buf2); |
| 9504 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9505 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9506 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9507 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9508 | if (srelease) |
| 9509 | PyMem_FREE(sbuf); |
| 9510 | if (release1) |
| 9511 | PyMem_FREE(buf1); |
| 9512 | if (release2) |
| 9513 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9514 | if (PyUnicode_CheckExact(self)) { |
| 9515 | Py_INCREF(self); |
| 9516 | return (PyObject *) self; |
| 9517 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9518 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9519 | error: |
| 9520 | if (srelease && sbuf) |
| 9521 | PyMem_FREE(sbuf); |
| 9522 | if (release1 && buf1) |
| 9523 | PyMem_FREE(buf1); |
| 9524 | if (release2 && buf2) |
| 9525 | PyMem_FREE(buf2); |
| 9526 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9527 | } |
| 9528 | |
| 9529 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9531 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9532 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9533 | \n\ |
| 9534 | 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] | 9535 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9536 | |
| 9537 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9538 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9539 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9540 | return fixup(self, fixtitle); |
| 9541 | } |
| 9542 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9543 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9544 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9545 | \n\ |
| 9546 | 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] | 9547 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9548 | |
| 9549 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9550 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9551 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9552 | return fixup(self, fixcapitalize); |
| 9553 | } |
| 9554 | |
| 9555 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9556 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9557 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9558 | \n\ |
| 9559 | 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] | 9560 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9561 | |
| 9562 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9563 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9564 | { |
| 9565 | PyObject *list; |
| 9566 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9567 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9568 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9569 | /* Split into words */ |
| 9570 | list = split(self, NULL, -1); |
| 9571 | if (!list) |
| 9572 | return NULL; |
| 9573 | |
| 9574 | /* Capitalize each word */ |
| 9575 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9576 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9577 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9578 | if (item == NULL) |
| 9579 | goto onError; |
| 9580 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9581 | PyList_SET_ITEM(list, i, item); |
| 9582 | } |
| 9583 | |
| 9584 | /* Join the words to form a new string */ |
| 9585 | item = PyUnicode_Join(NULL, list); |
| 9586 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9587 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9588 | Py_DECREF(list); |
| 9589 | return (PyObject *)item; |
| 9590 | } |
| 9591 | #endif |
| 9592 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9593 | /* Argument converter. Coerces to a single unicode character */ |
| 9594 | |
| 9595 | static int |
| 9596 | convert_uc(PyObject *obj, void *addr) |
| 9597 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9598 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9599 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9600 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9601 | uniobj = PyUnicode_FromObject(obj); |
| 9602 | if (uniobj == NULL) { |
| 9603 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9604 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9605 | return 0; |
| 9606 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9607 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9608 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9609 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9610 | Py_DECREF(uniobj); |
| 9611 | return 0; |
| 9612 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9613 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9614 | Py_DECREF(uniobj); |
| 9615 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9616 | } |
| 9617 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9618 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9619 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9620 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9621 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9622 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9623 | |
| 9624 | static PyObject * |
| 9625 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 9626 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9627 | Py_ssize_t marg, left; |
| 9628 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9629 | Py_UCS4 fillchar = ' '; |
| 9630 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9631 | 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] | 9632 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9633 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9634 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9635 | return NULL; |
| 9636 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9637 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9638 | Py_INCREF(self); |
| 9639 | return (PyObject*) self; |
| 9640 | } |
| 9641 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9642 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9643 | left = marg / 2 + (marg & width & 1); |
| 9644 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9645 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9646 | } |
| 9647 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9648 | #if 0 |
| 9649 | |
| 9650 | /* This code should go into some future Unicode collation support |
| 9651 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9652 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9653 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9654 | /* speedy UTF-16 code point order comparison */ |
| 9655 | /* gleaned from: */ |
| 9656 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 9657 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9658 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9659 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9660 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9661 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 9662 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9663 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9664 | }; |
| 9665 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9666 | static int |
| 9667 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9668 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9669 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9670 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9671 | Py_UNICODE *s1 = str1->str; |
| 9672 | Py_UNICODE *s2 = str2->str; |
| 9673 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9674 | len1 = str1->_base._base.length; |
| 9675 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9676 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9677 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9678 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9679 | |
| 9680 | c1 = *s1++; |
| 9681 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9682 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9683 | if (c1 > (1<<11) * 26) |
| 9684 | c1 += utf16Fixup[c1>>11]; |
| 9685 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9686 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9687 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9688 | |
| 9689 | if (c1 != c2) |
| 9690 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9691 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9692 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9693 | } |
| 9694 | |
| 9695 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9696 | } |
| 9697 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9698 | #else |
| 9699 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9700 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 9701 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9702 | static int |
| 9703 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9704 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9705 | int kind1, kind2; |
| 9706 | void *data1, *data2; |
| 9707 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9708 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9709 | kind1 = PyUnicode_KIND(str1); |
| 9710 | kind2 = PyUnicode_KIND(str2); |
| 9711 | data1 = PyUnicode_DATA(str1); |
| 9712 | data2 = PyUnicode_DATA(str2); |
| 9713 | len1 = PyUnicode_GET_LENGTH(str1); |
| 9714 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9715 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9716 | for (i = 0; i < len1 && i < len2; ++i) { |
| 9717 | Py_UCS4 c1, c2; |
| 9718 | c1 = PyUnicode_READ(kind1, data1, i); |
| 9719 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9720 | |
| 9721 | if (c1 != c2) |
| 9722 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9723 | } |
| 9724 | |
| 9725 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9726 | } |
| 9727 | |
| 9728 | #endif |
| 9729 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9730 | int |
| 9731 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9732 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9733 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9734 | if (PyUnicode_READY(left) == -1 || |
| 9735 | PyUnicode_READY(right) == -1) |
| 9736 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9737 | return unicode_compare((PyUnicodeObject *)left, |
| 9738 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9739 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9740 | PyErr_Format(PyExc_TypeError, |
| 9741 | "Can't compare %.100s and %.100s", |
| 9742 | left->ob_type->tp_name, |
| 9743 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9744 | return -1; |
| 9745 | } |
| 9746 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9747 | int |
| 9748 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 9749 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9750 | Py_ssize_t i; |
| 9751 | int kind; |
| 9752 | void *data; |
| 9753 | Py_UCS4 chr; |
| 9754 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 9755 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9756 | if (PyUnicode_READY(uni) == -1) |
| 9757 | return -1; |
| 9758 | kind = PyUnicode_KIND(uni); |
| 9759 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9760 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9761 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 9762 | if (chr != str[i]) |
| 9763 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 9764 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 9765 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9766 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9767 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9768 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9769 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9770 | return 0; |
| 9771 | } |
| 9772 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9773 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9774 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9775 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9776 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9777 | PyObject * |
| 9778 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9779 | { |
| 9780 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9781 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9782 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9783 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9784 | if (PyUnicode_READY(left) == -1 || |
| 9785 | PyUnicode_READY(right) == -1) |
| 9786 | return NULL; |
| 9787 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 9788 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9789 | if (op == Py_EQ) { |
| 9790 | Py_INCREF(Py_False); |
| 9791 | return Py_False; |
| 9792 | } |
| 9793 | if (op == Py_NE) { |
| 9794 | Py_INCREF(Py_True); |
| 9795 | return Py_True; |
| 9796 | } |
| 9797 | } |
| 9798 | if (left == right) |
| 9799 | result = 0; |
| 9800 | else |
| 9801 | result = unicode_compare((PyUnicodeObject *)left, |
| 9802 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9803 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9804 | /* Convert the return value to a Boolean */ |
| 9805 | switch (op) { |
| 9806 | case Py_EQ: |
| 9807 | v = TEST_COND(result == 0); |
| 9808 | break; |
| 9809 | case Py_NE: |
| 9810 | v = TEST_COND(result != 0); |
| 9811 | break; |
| 9812 | case Py_LE: |
| 9813 | v = TEST_COND(result <= 0); |
| 9814 | break; |
| 9815 | case Py_GE: |
| 9816 | v = TEST_COND(result >= 0); |
| 9817 | break; |
| 9818 | case Py_LT: |
| 9819 | v = TEST_COND(result == -1); |
| 9820 | break; |
| 9821 | case Py_GT: |
| 9822 | v = TEST_COND(result == 1); |
| 9823 | break; |
| 9824 | default: |
| 9825 | PyErr_BadArgument(); |
| 9826 | return NULL; |
| 9827 | } |
| 9828 | Py_INCREF(v); |
| 9829 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9830 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9831 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 9832 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9833 | } |
| 9834 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9835 | int |
| 9836 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9837 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9838 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9839 | int kind1, kind2, kind; |
| 9840 | void *buf1, *buf2; |
| 9841 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9842 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9843 | |
| 9844 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9845 | sub = PyUnicode_FromObject(element); |
| 9846 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9847 | PyErr_Format(PyExc_TypeError, |
| 9848 | "'in <string>' requires string as left operand, not %s", |
| 9849 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9850 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9851 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9852 | if (PyUnicode_READY(sub) == -1) |
| 9853 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9854 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9855 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9856 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9857 | Py_DECREF(sub); |
| 9858 | return -1; |
| 9859 | } |
| 9860 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9861 | kind1 = PyUnicode_KIND(str); |
| 9862 | kind2 = PyUnicode_KIND(sub); |
| 9863 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9864 | buf1 = PyUnicode_DATA(str); |
| 9865 | buf2 = PyUnicode_DATA(sub); |
| 9866 | if (kind1 != kind) |
| 9867 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 9868 | if (!buf1) { |
| 9869 | Py_DECREF(sub); |
| 9870 | return -1; |
| 9871 | } |
| 9872 | if (kind2 != kind) |
| 9873 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 9874 | if (!buf2) { |
| 9875 | Py_DECREF(sub); |
| 9876 | if (kind1 != kind) PyMem_Free(buf1); |
| 9877 | return -1; |
| 9878 | } |
| 9879 | len1 = PyUnicode_GET_LENGTH(str); |
| 9880 | len2 = PyUnicode_GET_LENGTH(sub); |
| 9881 | |
| 9882 | switch(kind) { |
| 9883 | case PyUnicode_1BYTE_KIND: |
| 9884 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9885 | break; |
| 9886 | case PyUnicode_2BYTE_KIND: |
| 9887 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9888 | break; |
| 9889 | case PyUnicode_4BYTE_KIND: |
| 9890 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9891 | break; |
| 9892 | default: |
| 9893 | result = -1; |
| 9894 | assert(0); |
| 9895 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9896 | |
| 9897 | Py_DECREF(str); |
| 9898 | Py_DECREF(sub); |
| 9899 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9900 | if (kind1 != kind) |
| 9901 | PyMem_Free(buf1); |
| 9902 | if (kind2 != kind) |
| 9903 | PyMem_Free(buf2); |
| 9904 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9905 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9906 | } |
| 9907 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9908 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 9909 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9910 | PyObject * |
| 9911 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9912 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9913 | PyObject *u = NULL, *v = NULL, *w; |
| 9914 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9915 | |
| 9916 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9917 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9918 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9919 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9920 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9921 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9922 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9923 | |
| 9924 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9925 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9926 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9927 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9928 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9929 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9930 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9931 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9932 | } |
| 9933 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9934 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 9935 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9936 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9937 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9938 | w = PyUnicode_New( |
| 9939 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 9940 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9941 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9942 | goto onError; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9943 | if (PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)) < 0) |
| 9944 | goto onError; |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9945 | if (PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9946 | v, 0, |
| 9947 | PyUnicode_GET_LENGTH(v)) < 0) |
| 9948 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9949 | Py_DECREF(u); |
| 9950 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9951 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9952 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9953 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9954 | Py_XDECREF(u); |
| 9955 | Py_XDECREF(v); |
| 9956 | return NULL; |
| 9957 | } |
| 9958 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9959 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9960 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9961 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9962 | PyObject *left, *res; |
| 9963 | |
| 9964 | if (p_left == NULL) { |
| 9965 | if (!PyErr_Occurred()) |
| 9966 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9967 | return; |
| 9968 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9969 | left = *p_left; |
| 9970 | if (right == NULL || !PyUnicode_Check(left)) { |
| 9971 | if (!PyErr_Occurred()) |
| 9972 | PyErr_BadInternalCall(); |
| 9973 | goto error; |
| 9974 | } |
| 9975 | |
| 9976 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 9977 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 9978 | && unicode_resizable(left) |
| 9979 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 9980 | || _PyUnicode_WSTR(left) != NULL)) |
| 9981 | { |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 9982 | Py_ssize_t left_len, right_len, new_len; |
| 9983 | #ifdef Py_DEBUG |
| 9984 | Py_ssize_t copied; |
| 9985 | #endif |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9986 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9987 | if (PyUnicode_READY(left)) |
| 9988 | goto error; |
| 9989 | if (PyUnicode_READY(right)) |
| 9990 | goto error; |
| 9991 | |
| 9992 | /* FIXME: support ascii+latin1, PyASCIIObject => PyCompactUnicodeObject */ |
| 9993 | if (PyUnicode_MAX_CHAR_VALUE(right) <= PyUnicode_MAX_CHAR_VALUE(left)) |
| 9994 | { |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 9995 | left_len = PyUnicode_GET_LENGTH(left); |
| 9996 | right_len = PyUnicode_GET_LENGTH(right); |
| 9997 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9998 | PyErr_SetString(PyExc_OverflowError, |
| 9999 | "strings are too large to concat"); |
| 10000 | goto error; |
| 10001 | } |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 10002 | new_len = left_len + right_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10003 | |
| 10004 | /* Now we own the last reference to 'left', so we can resize it |
| 10005 | * in-place. |
| 10006 | */ |
| 10007 | if (unicode_resize(&left, new_len) != 0) { |
| 10008 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10009 | * deallocated so it cannot be put back into |
| 10010 | * 'variable'. The MemoryError is raised when there |
| 10011 | * is no value in 'variable', which might (very |
| 10012 | * remotely) be a cause of incompatibilities. |
| 10013 | */ |
| 10014 | goto error; |
| 10015 | } |
| 10016 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 10017 | #ifdef Py_DEBUG |
| 10018 | copied = PyUnicode_CopyCharacters(left, left_len, |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10019 | right, 0, |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 10020 | right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10021 | assert(0 <= copied); |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 10022 | #else |
| 10023 | PyUnicode_CopyCharacters(left, left_len, right, 0, right_len); |
| 10024 | #endif |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10025 | *p_left = left; |
| 10026 | return; |
| 10027 | } |
| 10028 | } |
| 10029 | |
| 10030 | res = PyUnicode_Concat(left, right); |
| 10031 | if (res == NULL) |
| 10032 | goto error; |
| 10033 | Py_DECREF(left); |
| 10034 | *p_left = res; |
| 10035 | return; |
| 10036 | |
| 10037 | error: |
| 10038 | Py_DECREF(*p_left); |
| 10039 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10040 | } |
| 10041 | |
| 10042 | void |
| 10043 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10044 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10045 | PyUnicode_Append(pleft, right); |
| 10046 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10047 | } |
| 10048 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10049 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10050 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10051 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10052 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10053 | 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] | 10054 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10055 | |
| 10056 | static PyObject * |
| 10057 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 10058 | { |
| 10059 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10060 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10061 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10062 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10063 | int kind1, kind2, kind; |
| 10064 | void *buf1, *buf2; |
| 10065 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10066 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10067 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10068 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10069 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10070 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10071 | kind1 = PyUnicode_KIND(self); |
| 10072 | kind2 = PyUnicode_KIND(substring); |
| 10073 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10074 | buf1 = PyUnicode_DATA(self); |
| 10075 | buf2 = PyUnicode_DATA(substring); |
| 10076 | if (kind1 != kind) |
| 10077 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10078 | if (!buf1) { |
| 10079 | Py_DECREF(substring); |
| 10080 | return NULL; |
| 10081 | } |
| 10082 | if (kind2 != kind) |
| 10083 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10084 | if (!buf2) { |
| 10085 | Py_DECREF(substring); |
| 10086 | if (kind1 != kind) PyMem_Free(buf1); |
| 10087 | return NULL; |
| 10088 | } |
| 10089 | len1 = PyUnicode_GET_LENGTH(self); |
| 10090 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10091 | |
| 10092 | ADJUST_INDICES(start, end, len1); |
| 10093 | switch(kind) { |
| 10094 | case PyUnicode_1BYTE_KIND: |
| 10095 | iresult = ucs1lib_count( |
| 10096 | ((Py_UCS1*)buf1) + start, end - start, |
| 10097 | buf2, len2, PY_SSIZE_T_MAX |
| 10098 | ); |
| 10099 | break; |
| 10100 | case PyUnicode_2BYTE_KIND: |
| 10101 | iresult = ucs2lib_count( |
| 10102 | ((Py_UCS2*)buf1) + start, end - start, |
| 10103 | buf2, len2, PY_SSIZE_T_MAX |
| 10104 | ); |
| 10105 | break; |
| 10106 | case PyUnicode_4BYTE_KIND: |
| 10107 | iresult = ucs4lib_count( |
| 10108 | ((Py_UCS4*)buf1) + start, end - start, |
| 10109 | buf2, len2, PY_SSIZE_T_MAX |
| 10110 | ); |
| 10111 | break; |
| 10112 | default: |
| 10113 | assert(0); iresult = 0; |
| 10114 | } |
| 10115 | |
| 10116 | result = PyLong_FromSsize_t(iresult); |
| 10117 | |
| 10118 | if (kind1 != kind) |
| 10119 | PyMem_Free(buf1); |
| 10120 | if (kind2 != kind) |
| 10121 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10122 | |
| 10123 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10125 | return result; |
| 10126 | } |
| 10127 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10128 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10129 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10130 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10131 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10132 | 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] | 10133 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10134 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10135 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10136 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10137 | |
| 10138 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10139 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10140 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10141 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10142 | char *encoding = NULL; |
| 10143 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10144 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10145 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10146 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10147 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 10148 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10149 | } |
| 10150 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10151 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10152 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10153 | \n\ |
| 10154 | 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] | 10155 | 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] | 10156 | |
| 10157 | static PyObject* |
| 10158 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 10159 | { |
| 10160 | Py_UNICODE *e; |
| 10161 | Py_UNICODE *p; |
| 10162 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10163 | Py_UNICODE *qe; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10164 | Py_ssize_t i, j, incr, wstr_length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10165 | PyUnicodeObject *u; |
| 10166 | int tabsize = 8; |
| 10167 | |
| 10168 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10169 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10170 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10171 | if (PyUnicode_AsUnicodeAndSize((PyObject *)self, &wstr_length) == NULL) |
| 10172 | return NULL; |
| 10173 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10174 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10175 | i = 0; /* chars up to and including most recent \n or \r */ |
| 10176 | 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] | 10177 | e = _PyUnicode_WSTR(self) + wstr_length; /* end of input */ |
| 10178 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10179 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10180 | if (tabsize > 0) { |
| 10181 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 10182 | if (j > PY_SSIZE_T_MAX - incr) |
| 10183 | goto overflow1; |
| 10184 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10185 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10186 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10187 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10188 | if (j > PY_SSIZE_T_MAX - 1) |
| 10189 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10190 | j++; |
| 10191 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10192 | if (i > PY_SSIZE_T_MAX - j) |
| 10193 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10194 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10195 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10196 | } |
| 10197 | } |
| 10198 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10199 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10200 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10201 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10202 | /* Second pass: create output string and fill it */ |
| 10203 | u = _PyUnicode_New(i + j); |
| 10204 | if (!u) |
| 10205 | return NULL; |
| 10206 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10207 | j = 0; /* same as in first pass */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10208 | q = _PyUnicode_WSTR(u); /* next output char */ |
| 10209 | qe = _PyUnicode_WSTR(u) + PyUnicode_GET_SIZE(u); /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10210 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10211 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10212 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10213 | if (tabsize > 0) { |
| 10214 | i = tabsize - (j % tabsize); |
| 10215 | j += i; |
| 10216 | while (i--) { |
| 10217 | if (q >= qe) |
| 10218 | goto overflow2; |
| 10219 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10220 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10221 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10222 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10223 | else { |
| 10224 | if (q >= qe) |
| 10225 | goto overflow2; |
| 10226 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10227 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10228 | if (*p == '\n' || *p == '\r') |
| 10229 | j = 0; |
| 10230 | } |
| 10231 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 10232 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10233 | Py_DECREF(u); |
| 10234 | return NULL; |
| 10235 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10236 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10237 | |
| 10238 | overflow2: |
| 10239 | Py_DECREF(u); |
| 10240 | overflow1: |
| 10241 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10242 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10243 | } |
| 10244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10245 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10246 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10247 | \n\ |
| 10248 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10249 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10250 | arguments start and end are interpreted as in slice notation.\n\ |
| 10251 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10252 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10253 | |
| 10254 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10255 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10256 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10257 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10258 | Py_ssize_t start; |
| 10259 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10260 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10261 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10262 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10263 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10264 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10265 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10266 | if (PyUnicode_READY(self) == -1) |
| 10267 | return NULL; |
| 10268 | if (PyUnicode_READY(substring) == -1) |
| 10269 | return NULL; |
| 10270 | |
| 10271 | result = any_find_slice( |
| 10272 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 10273 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10274 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10275 | |
| 10276 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10278 | if (result == -2) |
| 10279 | return NULL; |
| 10280 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10281 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10282 | } |
| 10283 | |
| 10284 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10285 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10286 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10287 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 10288 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10289 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10290 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10291 | } |
| 10292 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10293 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10294 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10295 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 10296 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10297 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10298 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10299 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10300 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10301 | if (_PyUnicode_HASH(self) != -1) |
| 10302 | return _PyUnicode_HASH(self); |
| 10303 | if (PyUnicode_READY(self) == -1) |
| 10304 | return -1; |
| 10305 | len = PyUnicode_GET_LENGTH(self); |
| 10306 | |
| 10307 | /* The hash function as a macro, gets expanded three times below. */ |
| 10308 | #define HASH(P) \ |
| 10309 | x = (Py_uhash_t)*P << 7; \ |
| 10310 | while (--len >= 0) \ |
| 10311 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 10312 | |
| 10313 | switch (PyUnicode_KIND(self)) { |
| 10314 | case PyUnicode_1BYTE_KIND: { |
| 10315 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10316 | HASH(c); |
| 10317 | break; |
| 10318 | } |
| 10319 | case PyUnicode_2BYTE_KIND: { |
| 10320 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10321 | HASH(s); |
| 10322 | break; |
| 10323 | } |
| 10324 | default: { |
| 10325 | Py_UCS4 *l; |
| 10326 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10327 | "Impossible switch case in unicode_hash"); |
| 10328 | l = PyUnicode_4BYTE_DATA(self); |
| 10329 | HASH(l); |
| 10330 | break; |
| 10331 | } |
| 10332 | } |
| 10333 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 10334 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10335 | if (x == -1) |
| 10336 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10337 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10338 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10339 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10340 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10341 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10342 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10343 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10344 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10345 | 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] | 10346 | |
| 10347 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10348 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10349 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10350 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10351 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10352 | Py_ssize_t start; |
| 10353 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10354 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10355 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10356 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10357 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10358 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10359 | if (PyUnicode_READY(self) == -1) |
| 10360 | return NULL; |
| 10361 | if (PyUnicode_READY(substring) == -1) |
| 10362 | return NULL; |
| 10363 | |
| 10364 | result = any_find_slice( |
| 10365 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 10366 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10367 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10368 | |
| 10369 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10370 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10371 | if (result == -2) |
| 10372 | return NULL; |
| 10373 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10374 | if (result < 0) { |
| 10375 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10376 | return NULL; |
| 10377 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10378 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10379 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10380 | } |
| 10381 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10382 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10383 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10384 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10385 | 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] | 10386 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10387 | |
| 10388 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10389 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10390 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10391 | Py_ssize_t i, length; |
| 10392 | int kind; |
| 10393 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10394 | int cased; |
| 10395 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10396 | if (PyUnicode_READY(self) == -1) |
| 10397 | return NULL; |
| 10398 | length = PyUnicode_GET_LENGTH(self); |
| 10399 | kind = PyUnicode_KIND(self); |
| 10400 | data = PyUnicode_DATA(self); |
| 10401 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10402 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10403 | if (length == 1) |
| 10404 | return PyBool_FromLong( |
| 10405 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10406 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10407 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10408 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10409 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10410 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10411 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10412 | for (i = 0; i < length; i++) { |
| 10413 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10414 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10415 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10416 | return PyBool_FromLong(0); |
| 10417 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 10418 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10419 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10420 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10421 | } |
| 10422 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10423 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10424 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10425 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10426 | 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] | 10427 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10428 | |
| 10429 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10430 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10431 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10432 | Py_ssize_t i, length; |
| 10433 | int kind; |
| 10434 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10435 | int cased; |
| 10436 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10437 | if (PyUnicode_READY(self) == -1) |
| 10438 | return NULL; |
| 10439 | length = PyUnicode_GET_LENGTH(self); |
| 10440 | kind = PyUnicode_KIND(self); |
| 10441 | data = PyUnicode_DATA(self); |
| 10442 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10443 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10444 | if (length == 1) |
| 10445 | return PyBool_FromLong( |
| 10446 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10447 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10448 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10449 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10450 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10452 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10453 | for (i = 0; i < length; i++) { |
| 10454 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10455 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10456 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10457 | return PyBool_FromLong(0); |
| 10458 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 10459 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10460 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10461 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10462 | } |
| 10463 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10464 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10465 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10466 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10467 | Return True if S is a titlecased string and there is at least one\n\ |
| 10468 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 10469 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 10470 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10471 | |
| 10472 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10473 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10474 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10475 | Py_ssize_t i, length; |
| 10476 | int kind; |
| 10477 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10478 | int cased, previous_is_cased; |
| 10479 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10480 | if (PyUnicode_READY(self) == -1) |
| 10481 | return NULL; |
| 10482 | length = PyUnicode_GET_LENGTH(self); |
| 10483 | kind = PyUnicode_KIND(self); |
| 10484 | data = PyUnicode_DATA(self); |
| 10485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10486 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10487 | if (length == 1) { |
| 10488 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10489 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10490 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10491 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10492 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10493 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10494 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10495 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10496 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10497 | cased = 0; |
| 10498 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10499 | for (i = 0; i < length; i++) { |
| 10500 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10501 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10502 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10503 | if (previous_is_cased) |
| 10504 | return PyBool_FromLong(0); |
| 10505 | previous_is_cased = 1; |
| 10506 | cased = 1; |
| 10507 | } |
| 10508 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10509 | if (!previous_is_cased) |
| 10510 | return PyBool_FromLong(0); |
| 10511 | previous_is_cased = 1; |
| 10512 | cased = 1; |
| 10513 | } |
| 10514 | else |
| 10515 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10516 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10517 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10518 | } |
| 10519 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10520 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10521 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10522 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10523 | Return True if all characters in S are whitespace\n\ |
| 10524 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10525 | |
| 10526 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10527 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10528 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10529 | Py_ssize_t i, length; |
| 10530 | int kind; |
| 10531 | void *data; |
| 10532 | |
| 10533 | if (PyUnicode_READY(self) == -1) |
| 10534 | return NULL; |
| 10535 | length = PyUnicode_GET_LENGTH(self); |
| 10536 | kind = PyUnicode_KIND(self); |
| 10537 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10538 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10539 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10540 | if (length == 1) |
| 10541 | return PyBool_FromLong( |
| 10542 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10543 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10544 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10545 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10546 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10547 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10548 | for (i = 0; i < length; i++) { |
| 10549 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10550 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10551 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10552 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10553 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10554 | } |
| 10555 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10556 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10557 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10558 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10559 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10560 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10561 | |
| 10562 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10563 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10564 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10565 | Py_ssize_t i, length; |
| 10566 | int kind; |
| 10567 | void *data; |
| 10568 | |
| 10569 | if (PyUnicode_READY(self) == -1) |
| 10570 | return NULL; |
| 10571 | length = PyUnicode_GET_LENGTH(self); |
| 10572 | kind = PyUnicode_KIND(self); |
| 10573 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10574 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10575 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10576 | if (length == 1) |
| 10577 | return PyBool_FromLong( |
| 10578 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10579 | |
| 10580 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10581 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10582 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10583 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10584 | for (i = 0; i < length; i++) { |
| 10585 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10586 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10587 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10588 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10589 | } |
| 10590 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10591 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10592 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10593 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10594 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10595 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10596 | |
| 10597 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10598 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10599 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10600 | int kind; |
| 10601 | void *data; |
| 10602 | Py_ssize_t len, i; |
| 10603 | |
| 10604 | if (PyUnicode_READY(self) == -1) |
| 10605 | return NULL; |
| 10606 | |
| 10607 | kind = PyUnicode_KIND(self); |
| 10608 | data = PyUnicode_DATA(self); |
| 10609 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10610 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10611 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10612 | if (len == 1) { |
| 10613 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10614 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10615 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10616 | |
| 10617 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10618 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10619 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10620 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10621 | for (i = 0; i < len; i++) { |
| 10622 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10623 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10624 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10625 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10626 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10627 | } |
| 10628 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10629 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10630 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10631 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10632 | 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] | 10633 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10634 | |
| 10635 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10636 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10637 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10638 | Py_ssize_t i, length; |
| 10639 | int kind; |
| 10640 | void *data; |
| 10641 | |
| 10642 | if (PyUnicode_READY(self) == -1) |
| 10643 | return NULL; |
| 10644 | length = PyUnicode_GET_LENGTH(self); |
| 10645 | kind = PyUnicode_KIND(self); |
| 10646 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10648 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10649 | if (length == 1) |
| 10650 | return PyBool_FromLong( |
| 10651 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10652 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10653 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10654 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10655 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10656 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10657 | for (i = 0; i < length; i++) { |
| 10658 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10659 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10660 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10661 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10662 | } |
| 10663 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10664 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10665 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10666 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10667 | Return True if all characters in S are digits\n\ |
| 10668 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10669 | |
| 10670 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10671 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10672 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10673 | Py_ssize_t i, length; |
| 10674 | int kind; |
| 10675 | void *data; |
| 10676 | |
| 10677 | if (PyUnicode_READY(self) == -1) |
| 10678 | return NULL; |
| 10679 | length = PyUnicode_GET_LENGTH(self); |
| 10680 | kind = PyUnicode_KIND(self); |
| 10681 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10682 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10683 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10684 | if (length == 1) { |
| 10685 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10686 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 10687 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10688 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10689 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10690 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10691 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10692 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10693 | for (i = 0; i < length; i++) { |
| 10694 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10695 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10696 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10697 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10698 | } |
| 10699 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10700 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10701 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10702 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10703 | 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] | 10704 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10705 | |
| 10706 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10707 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10708 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10709 | Py_ssize_t i, length; |
| 10710 | int kind; |
| 10711 | void *data; |
| 10712 | |
| 10713 | if (PyUnicode_READY(self) == -1) |
| 10714 | return NULL; |
| 10715 | length = PyUnicode_GET_LENGTH(self); |
| 10716 | kind = PyUnicode_KIND(self); |
| 10717 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10719 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10720 | if (length == 1) |
| 10721 | return PyBool_FromLong( |
| 10722 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10723 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10724 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10725 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10726 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10727 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10728 | for (i = 0; i < length; i++) { |
| 10729 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10730 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10731 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10732 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10733 | } |
| 10734 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10735 | int |
| 10736 | PyUnicode_IsIdentifier(PyObject *self) |
| 10737 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10738 | int kind; |
| 10739 | void *data; |
| 10740 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10741 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10742 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10743 | if (PyUnicode_READY(self) == -1) { |
| 10744 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10745 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10746 | } |
| 10747 | |
| 10748 | /* Special case for empty strings */ |
| 10749 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10750 | return 0; |
| 10751 | kind = PyUnicode_KIND(self); |
| 10752 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10753 | |
| 10754 | /* PEP 3131 says that the first character must be in |
| 10755 | XID_Start and subsequent characters in XID_Continue, |
| 10756 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10757 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10758 | letters, digits, underscore). However, given the current |
| 10759 | definition of XID_Start and XID_Continue, it is sufficient |
| 10760 | to check just for these, except that _ must be allowed |
| 10761 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10762 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 10763 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10764 | return 0; |
| 10765 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 10766 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10767 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10768 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10769 | return 1; |
| 10770 | } |
| 10771 | |
| 10772 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10773 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10774 | \n\ |
| 10775 | Return True if S is a valid identifier according\n\ |
| 10776 | to the language definition."); |
| 10777 | |
| 10778 | static PyObject* |
| 10779 | unicode_isidentifier(PyObject *self) |
| 10780 | { |
| 10781 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 10782 | } |
| 10783 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10784 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10785 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10786 | \n\ |
| 10787 | Return True if all characters in S are considered\n\ |
| 10788 | printable in repr() or S is empty, False otherwise."); |
| 10789 | |
| 10790 | static PyObject* |
| 10791 | unicode_isprintable(PyObject *self) |
| 10792 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10793 | Py_ssize_t i, length; |
| 10794 | int kind; |
| 10795 | void *data; |
| 10796 | |
| 10797 | if (PyUnicode_READY(self) == -1) |
| 10798 | return NULL; |
| 10799 | length = PyUnicode_GET_LENGTH(self); |
| 10800 | kind = PyUnicode_KIND(self); |
| 10801 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10802 | |
| 10803 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10804 | if (length == 1) |
| 10805 | return PyBool_FromLong( |
| 10806 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10807 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10808 | for (i = 0; i < length; i++) { |
| 10809 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10810 | Py_RETURN_FALSE; |
| 10811 | } |
| 10812 | } |
| 10813 | Py_RETURN_TRUE; |
| 10814 | } |
| 10815 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10816 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 10817 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10818 | \n\ |
| 10819 | 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] | 10820 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10821 | |
| 10822 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10823 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10824 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10825 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10826 | } |
| 10827 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10828 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10829 | unicode_length(PyUnicodeObject *self) |
| 10830 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10831 | if (PyUnicode_READY(self) == -1) |
| 10832 | return -1; |
| 10833 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10834 | } |
| 10835 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10836 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10837 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10838 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10839 | 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] | 10840 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10841 | |
| 10842 | static PyObject * |
| 10843 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 10844 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10845 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10846 | Py_UCS4 fillchar = ' '; |
| 10847 | |
| 10848 | if (PyUnicode_READY(self) == -1) |
| 10849 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10850 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10851 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10852 | return NULL; |
| 10853 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10854 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10855 | Py_INCREF(self); |
| 10856 | return (PyObject*) self; |
| 10857 | } |
| 10858 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10859 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10860 | } |
| 10861 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10862 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10863 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10864 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10865 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10866 | |
| 10867 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10868 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10869 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10870 | return fixup(self, fixlower); |
| 10871 | } |
| 10872 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10873 | #define LEFTSTRIP 0 |
| 10874 | #define RIGHTSTRIP 1 |
| 10875 | #define BOTHSTRIP 2 |
| 10876 | |
| 10877 | /* Arrays indexed by above */ |
| 10878 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 10879 | |
| 10880 | #define STRIPNAME(i) (stripformat[i]+3) |
| 10881 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10882 | /* externally visible for str.strip(unicode) */ |
| 10883 | PyObject * |
| 10884 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 10885 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10886 | void *data; |
| 10887 | int kind; |
| 10888 | Py_ssize_t i, j, len; |
| 10889 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10890 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10891 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 10892 | return NULL; |
| 10893 | |
| 10894 | kind = PyUnicode_KIND(self); |
| 10895 | data = PyUnicode_DATA(self); |
| 10896 | len = PyUnicode_GET_LENGTH(self); |
| 10897 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 10898 | PyUnicode_DATA(sepobj), |
| 10899 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10900 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10901 | i = 0; |
| 10902 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10903 | while (i < len && |
| 10904 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10905 | i++; |
| 10906 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10907 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10908 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10909 | j = len; |
| 10910 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10911 | do { |
| 10912 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10913 | } while (j >= i && |
| 10914 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10915 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10916 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10917 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10918 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10919 | } |
| 10920 | |
| 10921 | PyObject* |
| 10922 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 10923 | { |
| 10924 | unsigned char *data; |
| 10925 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10926 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10927 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10928 | if (PyUnicode_READY(self) == -1) |
| 10929 | return NULL; |
| 10930 | |
| 10931 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 10932 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10933 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10934 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10935 | if (PyUnicode_CheckExact(self)) { |
| 10936 | Py_INCREF(self); |
| 10937 | return self; |
| 10938 | } |
| 10939 | else |
| 10940 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10941 | } |
| 10942 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10943 | length = end - start; |
| 10944 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10945 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10946 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10947 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10948 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10949 | return NULL; |
| 10950 | } |
| 10951 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10952 | kind = PyUnicode_KIND(self); |
| 10953 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10954 | return PyUnicode_FromKindAndData(kind, |
| 10955 | data + PyUnicode_KIND_SIZE(kind, start), |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10956 | length); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10957 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10958 | |
| 10959 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10960 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10961 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10962 | int kind; |
| 10963 | void *data; |
| 10964 | Py_ssize_t len, i, j; |
| 10965 | |
| 10966 | if (PyUnicode_READY(self) == -1) |
| 10967 | return NULL; |
| 10968 | |
| 10969 | kind = PyUnicode_KIND(self); |
| 10970 | data = PyUnicode_DATA(self); |
| 10971 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10972 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10973 | i = 0; |
| 10974 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10975 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10976 | i++; |
| 10977 | } |
| 10978 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10979 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10980 | j = len; |
| 10981 | if (striptype != LEFTSTRIP) { |
| 10982 | do { |
| 10983 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10984 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10985 | j++; |
| 10986 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10987 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10988 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10989 | } |
| 10990 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10991 | |
| 10992 | static PyObject * |
| 10993 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 10994 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10995 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10996 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10997 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 10998 | return NULL; |
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 | if (sep != NULL && sep != Py_None) { |
| 11001 | if (PyUnicode_Check(sep)) |
| 11002 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11003 | else { |
| 11004 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11005 | "%s arg must be None or str", |
| 11006 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11007 | return NULL; |
| 11008 | } |
| 11009 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11010 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11011 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11012 | } |
| 11013 | |
| 11014 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11015 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11016 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11017 | \n\ |
| 11018 | Return a copy of the string S with leading and trailing\n\ |
| 11019 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11020 | 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] | 11021 | |
| 11022 | static PyObject * |
| 11023 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 11024 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11025 | if (PyTuple_GET_SIZE(args) == 0) |
| 11026 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11027 | else |
| 11028 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11029 | } |
| 11030 | |
| 11031 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11032 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11033 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11034 | \n\ |
| 11035 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11036 | 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] | 11037 | |
| 11038 | static PyObject * |
| 11039 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 11040 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11041 | if (PyTuple_GET_SIZE(args) == 0) |
| 11042 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11043 | else |
| 11044 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11045 | } |
| 11046 | |
| 11047 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11048 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11049 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11050 | \n\ |
| 11051 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11052 | 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] | 11053 | |
| 11054 | static PyObject * |
| 11055 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 11056 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11057 | if (PyTuple_GET_SIZE(args) == 0) |
| 11058 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11059 | else |
| 11060 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11061 | } |
| 11062 | |
| 11063 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11064 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11065 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11066 | { |
| 11067 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11068 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11069 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11070 | if (len < 1) { |
| 11071 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11072 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11073 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11074 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11075 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11076 | /* no repeat, return original string */ |
| 11077 | Py_INCREF(str); |
| 11078 | return (PyObject*) str; |
| 11079 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11080 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11081 | if (PyUnicode_READY(str) == -1) |
| 11082 | return NULL; |
| 11083 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11084 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11085 | PyErr_SetString(PyExc_OverflowError, |
| 11086 | "repeated string is too long"); |
| 11087 | return NULL; |
| 11088 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11089 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11090 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11091 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11092 | if (!u) |
| 11093 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11094 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11095 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11096 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11097 | const int kind = PyUnicode_KIND(str); |
| 11098 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11099 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11100 | if (kind == PyUnicode_1BYTE_KIND) |
| 11101 | memset(to, (unsigned char)fill_char, len); |
| 11102 | else { |
| 11103 | for (n = 0; n < len; ++n) |
| 11104 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11105 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11106 | } |
| 11107 | else { |
| 11108 | /* number of characters copied this far */ |
| 11109 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
| 11110 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); |
| 11111 | char *to = (char *) PyUnicode_DATA(u); |
| 11112 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11113 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11114 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11115 | n = (done <= nchars-done) ? done : nchars-done; |
| 11116 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11117 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11118 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11119 | } |
| 11120 | |
| 11121 | return (PyObject*) u; |
| 11122 | } |
| 11123 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11124 | PyObject * |
| 11125 | PyUnicode_Replace(PyObject *obj, |
| 11126 | PyObject *subobj, |
| 11127 | PyObject *replobj, |
| 11128 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11129 | { |
| 11130 | PyObject *self; |
| 11131 | PyObject *str1; |
| 11132 | PyObject *str2; |
| 11133 | PyObject *result; |
| 11134 | |
| 11135 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11136 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11137 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11138 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11139 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11140 | Py_DECREF(self); |
| 11141 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11142 | } |
| 11143 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11144 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11145 | Py_DECREF(self); |
| 11146 | Py_DECREF(str1); |
| 11147 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11148 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11149 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11150 | Py_DECREF(self); |
| 11151 | Py_DECREF(str1); |
| 11152 | Py_DECREF(str2); |
| 11153 | return result; |
| 11154 | } |
| 11155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11156 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11157 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11158 | \n\ |
| 11159 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11160 | old replaced by new. If the optional argument count is\n\ |
| 11161 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11162 | |
| 11163 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11164 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11165 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11166 | PyObject *str1; |
| 11167 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11168 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11169 | PyObject *result; |
| 11170 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11171 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11172 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11173 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11174 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11175 | str1 = PyUnicode_FromObject(str1); |
| 11176 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11177 | return NULL; |
| 11178 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11179 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11180 | Py_DECREF(str1); |
| 11181 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11182 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11183 | |
| 11184 | result = replace(self, str1, str2, maxcount); |
| 11185 | |
| 11186 | Py_DECREF(str1); |
| 11187 | Py_DECREF(str2); |
| 11188 | return result; |
| 11189 | } |
| 11190 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11191 | static PyObject * |
| 11192 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11193 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11194 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11195 | Py_ssize_t isize; |
| 11196 | Py_ssize_t osize, squote, dquote, i, o; |
| 11197 | Py_UCS4 max, quote; |
| 11198 | int ikind, okind; |
| 11199 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11200 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11201 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11202 | return NULL; |
| 11203 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11204 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11205 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11206 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11207 | /* Compute length of output, quote characters, and |
| 11208 | maximum character */ |
| 11209 | osize = 2; /* quotes */ |
| 11210 | max = 127; |
| 11211 | squote = dquote = 0; |
| 11212 | ikind = PyUnicode_KIND(unicode); |
| 11213 | for (i = 0; i < isize; i++) { |
| 11214 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11215 | switch (ch) { |
| 11216 | case '\'': squote++; osize++; break; |
| 11217 | case '"': dquote++; osize++; break; |
| 11218 | case '\\': case '\t': case '\r': case '\n': |
| 11219 | osize += 2; break; |
| 11220 | default: |
| 11221 | /* Fast-path ASCII */ |
| 11222 | if (ch < ' ' || ch == 0x7f) |
| 11223 | osize += 4; /* \xHH */ |
| 11224 | else if (ch < 0x7f) |
| 11225 | osize++; |
| 11226 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11227 | osize++; |
| 11228 | max = ch > max ? ch : max; |
| 11229 | } |
| 11230 | else if (ch < 0x100) |
| 11231 | osize += 4; /* \xHH */ |
| 11232 | else if (ch < 0x10000) |
| 11233 | osize += 6; /* \uHHHH */ |
| 11234 | else |
| 11235 | osize += 10; /* \uHHHHHHHH */ |
| 11236 | } |
| 11237 | } |
| 11238 | |
| 11239 | quote = '\''; |
| 11240 | if (squote) { |
| 11241 | if (dquote) |
| 11242 | /* Both squote and dquote present. Use squote, |
| 11243 | and escape them */ |
| 11244 | osize += squote; |
| 11245 | else |
| 11246 | quote = '"'; |
| 11247 | } |
| 11248 | |
| 11249 | repr = PyUnicode_New(osize, max); |
| 11250 | if (repr == NULL) |
| 11251 | return NULL; |
| 11252 | okind = PyUnicode_KIND(repr); |
| 11253 | odata = PyUnicode_DATA(repr); |
| 11254 | |
| 11255 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11256 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11257 | |
| 11258 | for (i = 0, o = 1; i < isize; i++) { |
| 11259 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11260 | |
| 11261 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11262 | if ((ch == quote) || (ch == '\\')) { |
| 11263 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11264 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11265 | continue; |
| 11266 | } |
| 11267 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11268 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11269 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11270 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11271 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11272 | } |
| 11273 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11274 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11275 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11276 | } |
| 11277 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11278 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11279 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11280 | } |
| 11281 | |
| 11282 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11283 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11284 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11285 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11286 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11287 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11288 | } |
| 11289 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11290 | /* Copy ASCII characters as-is */ |
| 11291 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11292 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11293 | } |
| 11294 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11295 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11296 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11297 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11298 | (categories Z* and C* except ASCII space) |
| 11299 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11300 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11301 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11302 | if (ch <= 0xff) { |
| 11303 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11304 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11305 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11306 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11307 | } |
| 11308 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11309 | else if (ch >= 0x10000) { |
| 11310 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11311 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11312 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 11313 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 11314 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 11315 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 11316 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11317 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11318 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11319 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11320 | } |
| 11321 | /* Map 16-bit characters to '\uxxxx' */ |
| 11322 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11323 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11324 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 11325 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11326 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11327 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11328 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11329 | } |
| 11330 | } |
| 11331 | /* Copy characters as-is */ |
| 11332 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11333 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11334 | } |
| 11335 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11336 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11337 | /* Closing quote already added at the beginning */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11338 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11339 | } |
| 11340 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11341 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11342 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11343 | \n\ |
| 11344 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11345 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11346 | arguments start and end are interpreted as in slice notation.\n\ |
| 11347 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11348 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11349 | |
| 11350 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11351 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11352 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11353 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11354 | Py_ssize_t start; |
| 11355 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11356 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11357 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11358 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11359 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11360 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11361 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11362 | if (PyUnicode_READY(self) == -1) |
| 11363 | return NULL; |
| 11364 | if (PyUnicode_READY(substring) == -1) |
| 11365 | return NULL; |
| 11366 | |
| 11367 | result = any_find_slice( |
| 11368 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 11369 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11370 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11371 | |
| 11372 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11373 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11374 | if (result == -2) |
| 11375 | return NULL; |
| 11376 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11377 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11378 | } |
| 11379 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11380 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11381 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11382 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11383 | 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] | 11384 | |
| 11385 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11386 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11387 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11388 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11389 | Py_ssize_t start; |
| 11390 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11391 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11392 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11393 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 11394 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11395 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11396 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11397 | if (PyUnicode_READY(self) == -1) |
| 11398 | return NULL; |
| 11399 | if (PyUnicode_READY(substring) == -1) |
| 11400 | return NULL; |
| 11401 | |
| 11402 | result = any_find_slice( |
| 11403 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 11404 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11405 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11406 | |
| 11407 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11408 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11409 | if (result == -2) |
| 11410 | return NULL; |
| 11411 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11412 | if (result < 0) { |
| 11413 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11414 | return NULL; |
| 11415 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11416 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11417 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11418 | } |
| 11419 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11420 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11421 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11422 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11423 | 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] | 11424 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11425 | |
| 11426 | static PyObject * |
| 11427 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 11428 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11429 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11430 | Py_UCS4 fillchar = ' '; |
| 11431 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11432 | 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] | 11433 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11434 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11435 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11436 | return NULL; |
| 11437 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11438 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | Py_INCREF(self); |
| 11440 | return (PyObject*) self; |
| 11441 | } |
| 11442 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11443 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11444 | } |
| 11445 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11446 | PyObject * |
| 11447 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11448 | { |
| 11449 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11450 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11451 | s = PyUnicode_FromObject(s); |
| 11452 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11453 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11454 | if (sep != NULL) { |
| 11455 | sep = PyUnicode_FromObject(sep); |
| 11456 | if (sep == NULL) { |
| 11457 | Py_DECREF(s); |
| 11458 | return NULL; |
| 11459 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11460 | } |
| 11461 | |
| 11462 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11463 | |
| 11464 | Py_DECREF(s); |
| 11465 | Py_XDECREF(sep); |
| 11466 | return result; |
| 11467 | } |
| 11468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11469 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11470 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11471 | \n\ |
| 11472 | Return a list of the words in S, using sep as the\n\ |
| 11473 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11474 | 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] | 11475 | whitespace string is a separator and empty strings are\n\ |
| 11476 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11477 | |
| 11478 | static PyObject* |
| 11479 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 11480 | { |
| 11481 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11482 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11483 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11484 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11485 | return NULL; |
| 11486 | |
| 11487 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11488 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11489 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11490 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11491 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11492 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11493 | } |
| 11494 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11495 | PyObject * |
| 11496 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11497 | { |
| 11498 | PyObject* str_obj; |
| 11499 | PyObject* sep_obj; |
| 11500 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11501 | int kind1, kind2, kind; |
| 11502 | void *buf1 = NULL, *buf2 = NULL; |
| 11503 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11504 | |
| 11505 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11506 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11507 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11508 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11509 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11510 | Py_DECREF(str_obj); |
| 11511 | return NULL; |
| 11512 | } |
| 11513 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11514 | kind1 = PyUnicode_KIND(str_in); |
| 11515 | kind2 = PyUnicode_KIND(sep_obj); |
| 11516 | kind = kind1 > kind2 ? kind1 : kind2; |
| 11517 | buf1 = PyUnicode_DATA(str_in); |
| 11518 | if (kind1 != kind) |
| 11519 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11520 | if (!buf1) |
| 11521 | goto onError; |
| 11522 | buf2 = PyUnicode_DATA(sep_obj); |
| 11523 | if (kind2 != kind) |
| 11524 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11525 | if (!buf2) |
| 11526 | goto onError; |
| 11527 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11528 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11529 | |
| 11530 | switch(PyUnicode_KIND(str_in)) { |
| 11531 | case PyUnicode_1BYTE_KIND: |
| 11532 | out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11533 | break; |
| 11534 | case PyUnicode_2BYTE_KIND: |
| 11535 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11536 | break; |
| 11537 | case PyUnicode_4BYTE_KIND: |
| 11538 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11539 | break; |
| 11540 | default: |
| 11541 | assert(0); |
| 11542 | out = 0; |
| 11543 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11544 | |
| 11545 | Py_DECREF(sep_obj); |
| 11546 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11547 | if (kind1 != kind) |
| 11548 | PyMem_Free(buf1); |
| 11549 | if (kind2 != kind) |
| 11550 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11551 | |
| 11552 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11553 | onError: |
| 11554 | Py_DECREF(sep_obj); |
| 11555 | Py_DECREF(str_obj); |
| 11556 | if (kind1 != kind && buf1) |
| 11557 | PyMem_Free(buf1); |
| 11558 | if (kind2 != kind && buf2) |
| 11559 | PyMem_Free(buf2); |
| 11560 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11561 | } |
| 11562 | |
| 11563 | |
| 11564 | PyObject * |
| 11565 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11566 | { |
| 11567 | PyObject* str_obj; |
| 11568 | PyObject* sep_obj; |
| 11569 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11570 | int kind1, kind2, kind; |
| 11571 | void *buf1 = NULL, *buf2 = NULL; |
| 11572 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11573 | |
| 11574 | str_obj = PyUnicode_FromObject(str_in); |
| 11575 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11576 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11577 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11578 | if (!sep_obj) { |
| 11579 | Py_DECREF(str_obj); |
| 11580 | return NULL; |
| 11581 | } |
| 11582 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11583 | kind1 = PyUnicode_KIND(str_in); |
| 11584 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11585 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11586 | buf1 = PyUnicode_DATA(str_in); |
| 11587 | if (kind1 != kind) |
| 11588 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11589 | if (!buf1) |
| 11590 | goto onError; |
| 11591 | buf2 = PyUnicode_DATA(sep_obj); |
| 11592 | if (kind2 != kind) |
| 11593 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11594 | if (!buf2) |
| 11595 | goto onError; |
| 11596 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11597 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11598 | |
| 11599 | switch(PyUnicode_KIND(str_in)) { |
| 11600 | case PyUnicode_1BYTE_KIND: |
| 11601 | out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11602 | break; |
| 11603 | case PyUnicode_2BYTE_KIND: |
| 11604 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11605 | break; |
| 11606 | case PyUnicode_4BYTE_KIND: |
| 11607 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11608 | break; |
| 11609 | default: |
| 11610 | assert(0); |
| 11611 | out = 0; |
| 11612 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11613 | |
| 11614 | Py_DECREF(sep_obj); |
| 11615 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11616 | if (kind1 != kind) |
| 11617 | PyMem_Free(buf1); |
| 11618 | if (kind2 != kind) |
| 11619 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11620 | |
| 11621 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11622 | onError: |
| 11623 | Py_DECREF(sep_obj); |
| 11624 | Py_DECREF(str_obj); |
| 11625 | if (kind1 != kind && buf1) |
| 11626 | PyMem_Free(buf1); |
| 11627 | if (kind2 != kind && buf2) |
| 11628 | PyMem_Free(buf2); |
| 11629 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11630 | } |
| 11631 | |
| 11632 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11633 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11634 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11635 | 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] | 11636 | 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] | 11637 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11638 | |
| 11639 | static PyObject* |
| 11640 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 11641 | { |
| 11642 | return PyUnicode_Partition((PyObject *)self, separator); |
| 11643 | } |
| 11644 | |
| 11645 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11646 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11647 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11648 | 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] | 11649 | 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] | 11650 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11651 | |
| 11652 | static PyObject* |
| 11653 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 11654 | { |
| 11655 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 11656 | } |
| 11657 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11658 | PyObject * |
| 11659 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11660 | { |
| 11661 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11662 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11663 | s = PyUnicode_FromObject(s); |
| 11664 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11665 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11666 | if (sep != NULL) { |
| 11667 | sep = PyUnicode_FromObject(sep); |
| 11668 | if (sep == NULL) { |
| 11669 | Py_DECREF(s); |
| 11670 | return NULL; |
| 11671 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11672 | } |
| 11673 | |
| 11674 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11675 | |
| 11676 | Py_DECREF(s); |
| 11677 | Py_XDECREF(sep); |
| 11678 | return result; |
| 11679 | } |
| 11680 | |
| 11681 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11682 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11683 | \n\ |
| 11684 | Return a list of the words in S, using sep as the\n\ |
| 11685 | delimiter string, starting at the end of the string and\n\ |
| 11686 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 11687 | splits are done. If sep is not specified, any whitespace string\n\ |
| 11688 | is a separator."); |
| 11689 | |
| 11690 | static PyObject* |
| 11691 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 11692 | { |
| 11693 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11694 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11695 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11696 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11697 | return NULL; |
| 11698 | |
| 11699 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11700 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11701 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11702 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11703 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11704 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11705 | } |
| 11706 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11707 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11708 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11709 | \n\ |
| 11710 | 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] | 11711 | 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] | 11712 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11713 | |
| 11714 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11715 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11716 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11717 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11718 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11719 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11720 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 11721 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11722 | return NULL; |
| 11723 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11724 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11725 | } |
| 11726 | |
| 11727 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 11728 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11729 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 11730 | if (PyUnicode_CheckExact(self)) { |
| 11731 | Py_INCREF(self); |
| 11732 | return self; |
| 11733 | } else |
| 11734 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11735 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11736 | } |
| 11737 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11738 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11739 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11740 | \n\ |
| 11741 | 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] | 11742 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11743 | |
| 11744 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11745 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11746 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11747 | return fixup(self, fixswapcase); |
| 11748 | } |
| 11749 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11750 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11751 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11752 | \n\ |
| 11753 | Return a translation table usable for str.translate().\n\ |
| 11754 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 11755 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11756 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11757 | If there are two arguments, they must be strings of equal length, and\n\ |
| 11758 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 11759 | character at the same position in y. If there is a third argument, it\n\ |
| 11760 | must be a string, whose characters will be mapped to None in the result."); |
| 11761 | |
| 11762 | static PyObject* |
| 11763 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 11764 | { |
| 11765 | PyObject *x, *y = NULL, *z = NULL; |
| 11766 | PyObject *new = NULL, *key, *value; |
| 11767 | Py_ssize_t i = 0; |
| 11768 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11769 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11770 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 11771 | return NULL; |
| 11772 | new = PyDict_New(); |
| 11773 | if (!new) |
| 11774 | return NULL; |
| 11775 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11776 | int x_kind, y_kind, z_kind; |
| 11777 | void *x_data, *y_data, *z_data; |
| 11778 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11779 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11780 | if (!PyUnicode_Check(x)) { |
| 11781 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 11782 | "be a string if there is a second argument"); |
| 11783 | goto err; |
| 11784 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11785 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11786 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 11787 | "arguments must have equal length"); |
| 11788 | goto err; |
| 11789 | } |
| 11790 | /* 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] | 11791 | x_kind = PyUnicode_KIND(x); |
| 11792 | y_kind = PyUnicode_KIND(y); |
| 11793 | x_data = PyUnicode_DATA(x); |
| 11794 | y_data = PyUnicode_DATA(y); |
| 11795 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 11796 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 11797 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11798 | if (!key || !value) |
| 11799 | goto err; |
| 11800 | res = PyDict_SetItem(new, key, value); |
| 11801 | Py_DECREF(key); |
| 11802 | Py_DECREF(value); |
| 11803 | if (res < 0) |
| 11804 | goto err; |
| 11805 | } |
| 11806 | /* create entries for deleting chars in z */ |
| 11807 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11808 | z_kind = PyUnicode_KIND(z); |
| 11809 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11810 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11811 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11812 | if (!key) |
| 11813 | goto err; |
| 11814 | res = PyDict_SetItem(new, key, Py_None); |
| 11815 | Py_DECREF(key); |
| 11816 | if (res < 0) |
| 11817 | goto err; |
| 11818 | } |
| 11819 | } |
| 11820 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11821 | int kind; |
| 11822 | void *data; |
| 11823 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11824 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 11825 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11826 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 11827 | "to maketrans it must be a dict"); |
| 11828 | goto err; |
| 11829 | } |
| 11830 | /* copy entries into the new dict, converting string keys to int keys */ |
| 11831 | while (PyDict_Next(x, &i, &key, &value)) { |
| 11832 | if (PyUnicode_Check(key)) { |
| 11833 | /* convert string keys to integer keys */ |
| 11834 | PyObject *newkey; |
| 11835 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 11836 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 11837 | "table must be of length 1"); |
| 11838 | goto err; |
| 11839 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11840 | kind = PyUnicode_KIND(key); |
| 11841 | data = PyUnicode_DATA(key); |
| 11842 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11843 | if (!newkey) |
| 11844 | goto err; |
| 11845 | res = PyDict_SetItem(new, newkey, value); |
| 11846 | Py_DECREF(newkey); |
| 11847 | if (res < 0) |
| 11848 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11849 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11850 | /* just keep integer keys */ |
| 11851 | if (PyDict_SetItem(new, key, value) < 0) |
| 11852 | goto err; |
| 11853 | } else { |
| 11854 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 11855 | "be strings or integers"); |
| 11856 | goto err; |
| 11857 | } |
| 11858 | } |
| 11859 | } |
| 11860 | return new; |
| 11861 | err: |
| 11862 | Py_DECREF(new); |
| 11863 | return NULL; |
| 11864 | } |
| 11865 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11866 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11867 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11868 | \n\ |
| 11869 | Return a copy of the string S, where all characters have been mapped\n\ |
| 11870 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11871 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 11872 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 11873 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11874 | |
| 11875 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11876 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11877 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11878 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11879 | } |
| 11880 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11881 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11882 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11883 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11884 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11885 | |
| 11886 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11887 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11888 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11889 | return fixup(self, fixupper); |
| 11890 | } |
| 11891 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11892 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11893 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11894 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 11895 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 11896 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11897 | |
| 11898 | static PyObject * |
| 11899 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 11900 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11901 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11902 | PyUnicodeObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11903 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11904 | int kind; |
| 11905 | void *data; |
| 11906 | Py_UCS4 chr; |
| 11907 | |
| 11908 | if (PyUnicode_READY(self) == -1) |
| 11909 | return NULL; |
| 11910 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11911 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11912 | return NULL; |
| 11913 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11914 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 11915 | if (PyUnicode_CheckExact(self)) { |
| 11916 | Py_INCREF(self); |
| 11917 | return (PyObject*) self; |
| 11918 | } |
| 11919 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 11920 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11921 | } |
| 11922 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11923 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11924 | |
| 11925 | u = pad(self, fill, 0, '0'); |
| 11926 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11927 | if (u == NULL) |
| 11928 | return NULL; |
| 11929 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11930 | kind = PyUnicode_KIND(u); |
| 11931 | data = PyUnicode_DATA(u); |
| 11932 | chr = PyUnicode_READ(kind, data, fill); |
| 11933 | |
| 11934 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11935 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11936 | PyUnicode_WRITE(kind, data, 0, chr); |
| 11937 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11938 | } |
| 11939 | |
| 11940 | return (PyObject*) u; |
| 11941 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11942 | |
| 11943 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11944 | static PyObject * |
| 11945 | unicode__decimal2ascii(PyObject *self) |
| 11946 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11947 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11948 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11949 | #endif |
| 11950 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11951 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11952 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11953 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11954 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 11955 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11956 | With optional end, stop comparing S at that position.\n\ |
| 11957 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11958 | |
| 11959 | static PyObject * |
| 11960 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11961 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11962 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11963 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11964 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11965 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11966 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11967 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11968 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11969 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11970 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11971 | if (PyTuple_Check(subobj)) { |
| 11972 | Py_ssize_t i; |
| 11973 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11974 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11975 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11976 | if (substring == NULL) |
| 11977 | return NULL; |
| 11978 | result = tailmatch(self, substring, start, end, -1); |
| 11979 | Py_DECREF(substring); |
| 11980 | if (result) { |
| 11981 | Py_RETURN_TRUE; |
| 11982 | } |
| 11983 | } |
| 11984 | /* nothing matched */ |
| 11985 | Py_RETURN_FALSE; |
| 11986 | } |
| 11987 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11988 | if (substring == NULL) { |
| 11989 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11990 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 11991 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11992 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11993 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11994 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11995 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11996 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11997 | } |
| 11998 | |
| 11999 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12000 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12001 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12002 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12003 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12004 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12005 | With optional end, stop comparing S at that position.\n\ |
| 12006 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12007 | |
| 12008 | static PyObject * |
| 12009 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12010 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12011 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12012 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12013 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12014 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12015 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12016 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12017 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12018 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12019 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12020 | if (PyTuple_Check(subobj)) { |
| 12021 | Py_ssize_t i; |
| 12022 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12023 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12024 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12025 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12026 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12027 | result = tailmatch(self, substring, start, end, +1); |
| 12028 | Py_DECREF(substring); |
| 12029 | if (result) { |
| 12030 | Py_RETURN_TRUE; |
| 12031 | } |
| 12032 | } |
| 12033 | Py_RETURN_FALSE; |
| 12034 | } |
| 12035 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12036 | if (substring == NULL) { |
| 12037 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12038 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12039 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12040 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12041 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12042 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12043 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12044 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12045 | } |
| 12046 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12047 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12048 | |
| 12049 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12050 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12051 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12052 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12053 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12054 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12055 | PyDoc_STRVAR(format_map__doc__, |
| 12056 | "S.format_map(mapping) -> str\n\ |
| 12057 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12058 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12059 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12060 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12061 | static PyObject * |
| 12062 | unicode__format__(PyObject* self, PyObject* args) |
| 12063 | { |
| 12064 | PyObject *format_spec; |
| 12065 | |
| 12066 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12067 | return NULL; |
| 12068 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12069 | return _PyUnicode_FormatAdvanced(self, format_spec, 0, |
| 12070 | PyUnicode_GET_LENGTH(format_spec)); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12071 | } |
| 12072 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12073 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12074 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12075 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12076 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12077 | |
| 12078 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12079 | unicode__sizeof__(PyUnicodeObject *v) |
| 12080 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12081 | Py_ssize_t size; |
| 12082 | |
| 12083 | /* If it's a compact object, account for base structure + |
| 12084 | character data. */ |
| 12085 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12086 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12087 | else if (PyUnicode_IS_COMPACT(v)) |
| 12088 | size = sizeof(PyCompactUnicodeObject) + |
| 12089 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); |
| 12090 | else { |
| 12091 | /* If it is a two-block object, account for base object, and |
| 12092 | for character block if present. */ |
| 12093 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12094 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12095 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
| 12096 | PyUnicode_CHARACTER_SIZE(v); |
| 12097 | } |
| 12098 | /* 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] | 12099 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12100 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12101 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12102 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12103 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12104 | |
| 12105 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12106 | } |
| 12107 | |
| 12108 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12109 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12110 | |
| 12111 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12112 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12113 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12114 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12115 | if (!copy) |
| 12116 | return NULL; |
| 12117 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12118 | } |
| 12119 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12120 | static PyMethodDef unicode_methods[] = { |
| 12121 | |
| 12122 | /* Order is according to common usage: often used methods should |
| 12123 | appear first, since lookup is done sequentially. */ |
| 12124 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12125 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12126 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12127 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12128 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12129 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12130 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12131 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12132 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12133 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12134 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12135 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12136 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12137 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12138 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12139 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12140 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12141 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12142 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12143 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12144 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12145 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12146 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12147 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12148 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12149 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12150 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12151 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12152 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12153 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12154 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12155 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12156 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12157 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12158 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12159 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12160 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12161 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12162 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12163 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12164 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12165 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12166 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12167 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12168 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12169 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12170 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12171 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12172 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12173 | #endif |
| 12174 | |
| 12175 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12176 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12177 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12178 | #endif |
| 12179 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12180 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12181 | {NULL, NULL} |
| 12182 | }; |
| 12183 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12184 | static PyObject * |
| 12185 | unicode_mod(PyObject *v, PyObject *w) |
| 12186 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12187 | if (!PyUnicode_Check(v)) |
| 12188 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12189 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12190 | } |
| 12191 | |
| 12192 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12193 | 0, /*nb_add*/ |
| 12194 | 0, /*nb_subtract*/ |
| 12195 | 0, /*nb_multiply*/ |
| 12196 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12197 | }; |
| 12198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12199 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12200 | (lenfunc) unicode_length, /* sq_length */ |
| 12201 | PyUnicode_Concat, /* sq_concat */ |
| 12202 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12203 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12204 | 0, /* sq_slice */ |
| 12205 | 0, /* sq_ass_item */ |
| 12206 | 0, /* sq_ass_slice */ |
| 12207 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12208 | }; |
| 12209 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12210 | static PyObject* |
| 12211 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 12212 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12213 | if (PyUnicode_READY(self) == -1) |
| 12214 | return NULL; |
| 12215 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12216 | if (PyIndex_Check(item)) { |
| 12217 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12218 | if (i == -1 && PyErr_Occurred()) |
| 12219 | return NULL; |
| 12220 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12221 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12222 | return unicode_getitem((PyObject*)self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12223 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12224 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12225 | const Py_UNICODE* source_buf; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12226 | Py_UNICODE* result_buf; |
| 12227 | PyObject* result; |
| 12228 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12229 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12230 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12231 | return NULL; |
| 12232 | } |
| 12233 | |
| 12234 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12235 | return PyUnicode_New(0, 0); |
| 12236 | } else if (start == 0 && step == 1 && |
| 12237 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12238 | PyUnicode_CheckExact(self)) { |
| 12239 | Py_INCREF(self); |
| 12240 | return (PyObject *)self; |
| 12241 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12242 | return PyUnicode_Substring((PyObject*)self, |
| 12243 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12244 | } else { |
| 12245 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 12246 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 12247 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12248 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12249 | if (result_buf == NULL) |
| 12250 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12251 | |
| 12252 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 12253 | result_buf[i] = source_buf[cur]; |
| 12254 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12255 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12256 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 12257 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12258 | return result; |
| 12259 | } |
| 12260 | } else { |
| 12261 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 12262 | return NULL; |
| 12263 | } |
| 12264 | } |
| 12265 | |
| 12266 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12267 | (lenfunc)unicode_length, /* mp_length */ |
| 12268 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 12269 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12270 | }; |
| 12271 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12272 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12273 | /* Helpers for PyUnicode_Format() */ |
| 12274 | |
| 12275 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12276 | 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] | 12277 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12278 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12279 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12280 | (*p_argidx)++; |
| 12281 | if (arglen < 0) |
| 12282 | return args; |
| 12283 | else |
| 12284 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12285 | } |
| 12286 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12287 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12288 | return NULL; |
| 12289 | } |
| 12290 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12291 | /* 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] | 12292 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12293 | static PyObject * |
| 12294 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12295 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12296 | char *p; |
| 12297 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12298 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12299 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12300 | x = PyFloat_AsDouble(v); |
| 12301 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12302 | return NULL; |
| 12303 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12304 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12305 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12306 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12307 | p = PyOS_double_to_string(x, type, prec, |
| 12308 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12309 | if (p == NULL) |
| 12310 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12311 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12312 | PyMem_Free(p); |
| 12313 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12314 | } |
| 12315 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12316 | static PyObject* |
| 12317 | formatlong(PyObject *val, int flags, int prec, int type) |
| 12318 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12319 | char *buf; |
| 12320 | int len; |
| 12321 | PyObject *str; /* temporary string object. */ |
| 12322 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12323 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12324 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 12325 | if (!str) |
| 12326 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12327 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12328 | Py_DECREF(str); |
| 12329 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12330 | } |
| 12331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12332 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12333 | formatchar(Py_UCS4 *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12334 | size_t buflen, |
| 12335 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12336 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12337 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12338 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12339 | if (PyUnicode_GET_LENGTH(v) == 1) { |
| 12340 | buf[0] = PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12341 | buf[1] = '\0'; |
| 12342 | return 1; |
| 12343 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12344 | goto onError; |
| 12345 | } |
| 12346 | else { |
| 12347 | /* Integer input truncated to a character */ |
| 12348 | long x; |
| 12349 | x = PyLong_AsLong(v); |
| 12350 | if (x == -1 && PyErr_Occurred()) |
| 12351 | goto onError; |
| 12352 | |
| 12353 | if (x < 0 || x > 0x10ffff) { |
| 12354 | PyErr_SetString(PyExc_OverflowError, |
| 12355 | "%c arg not in range(0x110000)"); |
| 12356 | return -1; |
| 12357 | } |
| 12358 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12359 | buf[0] = (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12360 | buf[1] = '\0'; |
| 12361 | return 1; |
| 12362 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12363 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12364 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12365 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12366 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12367 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12368 | } |
| 12369 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12370 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12371 | 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] | 12372 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12373 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12374 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12375 | PyObject * |
| 12376 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12377 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12378 | void *fmt; |
| 12379 | int fmtkind; |
| 12380 | PyObject *result; |
| 12381 | Py_UCS4 *res, *res0; |
| 12382 | Py_UCS4 max; |
| 12383 | int kind; |
| 12384 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12385 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12386 | PyObject *dict = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12387 | PyUnicodeObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12389 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12390 | PyErr_BadInternalCall(); |
| 12391 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12392 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12393 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 12394 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12395 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12396 | fmt = PyUnicode_DATA(uformat); |
| 12397 | fmtkind = PyUnicode_KIND(uformat); |
| 12398 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 12399 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12400 | |
| 12401 | reslen = rescnt = fmtcnt + 100; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12402 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); |
| 12403 | if (res0 == NULL) { |
| 12404 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12405 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12406 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12407 | |
| 12408 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12409 | arglen = PyTuple_Size(args); |
| 12410 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12411 | } |
| 12412 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12413 | arglen = -1; |
| 12414 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12415 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12416 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12417 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12418 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12419 | |
| 12420 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12421 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12422 | if (--rescnt < 0) { |
| 12423 | rescnt = fmtcnt + 100; |
| 12424 | reslen += rescnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12425 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12426 | if (res0 == NULL){ |
| 12427 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12428 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12429 | } |
| 12430 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12431 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12432 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12433 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12434 | } |
| 12435 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12436 | /* Got a format specifier */ |
| 12437 | int flags = 0; |
| 12438 | Py_ssize_t width = -1; |
| 12439 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12440 | Py_UCS4 c = '\0'; |
| 12441 | Py_UCS4 fill; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12442 | int isnumok; |
| 12443 | PyObject *v = NULL; |
| 12444 | PyObject *temp = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12445 | void *pbuf; |
| 12446 | Py_ssize_t pindex; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12447 | Py_UNICODE sign; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12448 | Py_ssize_t len, len1; |
| 12449 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12450 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12451 | fmtpos++; |
| 12452 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12453 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12454 | Py_ssize_t keylen; |
| 12455 | PyObject *key; |
| 12456 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12457 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12458 | if (dict == NULL) { |
| 12459 | PyErr_SetString(PyExc_TypeError, |
| 12460 | "format requires a mapping"); |
| 12461 | goto onError; |
| 12462 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12463 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12464 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12465 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12466 | /* Skip over balanced parentheses */ |
| 12467 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12468 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12469 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12470 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12471 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12472 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12473 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12474 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12475 | if (fmtcnt < 0 || pcount > 0) { |
| 12476 | PyErr_SetString(PyExc_ValueError, |
| 12477 | "incomplete format key"); |
| 12478 | goto onError; |
| 12479 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12480 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12481 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12482 | if (key == NULL) |
| 12483 | goto onError; |
| 12484 | if (args_owned) { |
| 12485 | Py_DECREF(args); |
| 12486 | args_owned = 0; |
| 12487 | } |
| 12488 | args = PyObject_GetItem(dict, key); |
| 12489 | Py_DECREF(key); |
| 12490 | if (args == NULL) { |
| 12491 | goto onError; |
| 12492 | } |
| 12493 | args_owned = 1; |
| 12494 | arglen = -1; |
| 12495 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12496 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12497 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12498 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12499 | case '-': flags |= F_LJUST; continue; |
| 12500 | case '+': flags |= F_SIGN; continue; |
| 12501 | case ' ': flags |= F_BLANK; continue; |
| 12502 | case '#': flags |= F_ALT; continue; |
| 12503 | case '0': flags |= F_ZERO; continue; |
| 12504 | } |
| 12505 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12506 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12507 | if (c == '*') { |
| 12508 | v = getnextarg(args, arglen, &argidx); |
| 12509 | if (v == NULL) |
| 12510 | goto onError; |
| 12511 | if (!PyLong_Check(v)) { |
| 12512 | PyErr_SetString(PyExc_TypeError, |
| 12513 | "* wants int"); |
| 12514 | goto onError; |
| 12515 | } |
| 12516 | width = PyLong_AsLong(v); |
| 12517 | if (width == -1 && PyErr_Occurred()) |
| 12518 | goto onError; |
| 12519 | if (width < 0) { |
| 12520 | flags |= F_LJUST; |
| 12521 | width = -width; |
| 12522 | } |
| 12523 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12524 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12525 | } |
| 12526 | else if (c >= '0' && c <= '9') { |
| 12527 | width = c - '0'; |
| 12528 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12529 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12530 | if (c < '0' || c > '9') |
| 12531 | break; |
| 12532 | if ((width*10) / 10 != width) { |
| 12533 | PyErr_SetString(PyExc_ValueError, |
| 12534 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12535 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12536 | } |
| 12537 | width = width*10 + (c - '0'); |
| 12538 | } |
| 12539 | } |
| 12540 | if (c == '.') { |
| 12541 | prec = 0; |
| 12542 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12543 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12544 | if (c == '*') { |
| 12545 | v = getnextarg(args, arglen, &argidx); |
| 12546 | if (v == NULL) |
| 12547 | goto onError; |
| 12548 | if (!PyLong_Check(v)) { |
| 12549 | PyErr_SetString(PyExc_TypeError, |
| 12550 | "* wants int"); |
| 12551 | goto onError; |
| 12552 | } |
| 12553 | prec = PyLong_AsLong(v); |
| 12554 | if (prec == -1 && PyErr_Occurred()) |
| 12555 | goto onError; |
| 12556 | if (prec < 0) |
| 12557 | prec = 0; |
| 12558 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12559 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12560 | } |
| 12561 | else if (c >= '0' && c <= '9') { |
| 12562 | prec = c - '0'; |
| 12563 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12564 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12565 | if (c < '0' || c > '9') |
| 12566 | break; |
| 12567 | if ((prec*10) / 10 != prec) { |
| 12568 | PyErr_SetString(PyExc_ValueError, |
| 12569 | "prec too big"); |
| 12570 | goto onError; |
| 12571 | } |
| 12572 | prec = prec*10 + (c - '0'); |
| 12573 | } |
| 12574 | } |
| 12575 | } /* prec */ |
| 12576 | if (fmtcnt >= 0) { |
| 12577 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12578 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12579 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12580 | } |
| 12581 | } |
| 12582 | if (fmtcnt < 0) { |
| 12583 | PyErr_SetString(PyExc_ValueError, |
| 12584 | "incomplete format"); |
| 12585 | goto onError; |
| 12586 | } |
| 12587 | if (c != '%') { |
| 12588 | v = getnextarg(args, arglen, &argidx); |
| 12589 | if (v == NULL) |
| 12590 | goto onError; |
| 12591 | } |
| 12592 | sign = 0; |
| 12593 | fill = ' '; |
| 12594 | switch (c) { |
| 12595 | |
| 12596 | case '%': |
| 12597 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12598 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12599 | /* presume that buffer length is at least 1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12600 | PyUnicode_WRITE(kind, pbuf, 0, '%'); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12601 | len = 1; |
| 12602 | break; |
| 12603 | |
| 12604 | case 's': |
| 12605 | case 'r': |
| 12606 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12607 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12608 | temp = v; |
| 12609 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12610 | } |
| 12611 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12612 | if (c == 's') |
| 12613 | temp = PyObject_Str(v); |
| 12614 | else if (c == 'r') |
| 12615 | temp = PyObject_Repr(v); |
| 12616 | else |
| 12617 | temp = PyObject_ASCII(v); |
| 12618 | if (temp == NULL) |
| 12619 | goto onError; |
| 12620 | if (PyUnicode_Check(temp)) |
| 12621 | /* nothing to do */; |
| 12622 | else { |
| 12623 | Py_DECREF(temp); |
| 12624 | PyErr_SetString(PyExc_TypeError, |
| 12625 | "%s argument has non-string str()"); |
| 12626 | goto onError; |
| 12627 | } |
| 12628 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12629 | if (PyUnicode_READY(temp) == -1) { |
| 12630 | Py_CLEAR(temp); |
| 12631 | goto onError; |
| 12632 | } |
| 12633 | pbuf = PyUnicode_DATA(temp); |
| 12634 | kind = PyUnicode_KIND(temp); |
| 12635 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12636 | if (prec >= 0 && len > prec) |
| 12637 | len = prec; |
| 12638 | break; |
| 12639 | |
| 12640 | case 'i': |
| 12641 | case 'd': |
| 12642 | case 'u': |
| 12643 | case 'o': |
| 12644 | case 'x': |
| 12645 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12646 | isnumok = 0; |
| 12647 | if (PyNumber_Check(v)) { |
| 12648 | PyObject *iobj=NULL; |
| 12649 | |
| 12650 | if (PyLong_Check(v)) { |
| 12651 | iobj = v; |
| 12652 | Py_INCREF(iobj); |
| 12653 | } |
| 12654 | else { |
| 12655 | iobj = PyNumber_Long(v); |
| 12656 | } |
| 12657 | if (iobj!=NULL) { |
| 12658 | if (PyLong_Check(iobj)) { |
| 12659 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 12660 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12661 | Py_DECREF(iobj); |
| 12662 | if (!temp) |
| 12663 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12664 | if (PyUnicode_READY(temp) == -1) { |
| 12665 | Py_CLEAR(temp); |
| 12666 | goto onError; |
| 12667 | } |
| 12668 | pbuf = PyUnicode_DATA(temp); |
| 12669 | kind = PyUnicode_KIND(temp); |
| 12670 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12671 | sign = 1; |
| 12672 | } |
| 12673 | else { |
| 12674 | Py_DECREF(iobj); |
| 12675 | } |
| 12676 | } |
| 12677 | } |
| 12678 | if (!isnumok) { |
| 12679 | PyErr_Format(PyExc_TypeError, |
| 12680 | "%%%c format: a number is required, " |
| 12681 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 12682 | goto onError; |
| 12683 | } |
| 12684 | if (flags & F_ZERO) |
| 12685 | fill = '0'; |
| 12686 | break; |
| 12687 | |
| 12688 | case 'e': |
| 12689 | case 'E': |
| 12690 | case 'f': |
| 12691 | case 'F': |
| 12692 | case 'g': |
| 12693 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12694 | temp = formatfloat(v, flags, prec, c); |
| 12695 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12696 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12697 | if (PyUnicode_READY(temp) == -1) { |
| 12698 | Py_CLEAR(temp); |
| 12699 | goto onError; |
| 12700 | } |
| 12701 | pbuf = PyUnicode_DATA(temp); |
| 12702 | kind = PyUnicode_KIND(temp); |
| 12703 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12704 | sign = 1; |
| 12705 | if (flags & F_ZERO) |
| 12706 | fill = '0'; |
| 12707 | break; |
| 12708 | |
| 12709 | case 'c': |
| 12710 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12711 | kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | b9dcffb | 2011-09-29 00:39:24 +0200 | [diff] [blame] | 12712 | len = formatchar(pbuf, Py_ARRAY_LENGTH(formatbuf), v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12713 | if (len < 0) |
| 12714 | goto onError; |
| 12715 | break; |
| 12716 | |
| 12717 | default: |
| 12718 | PyErr_Format(PyExc_ValueError, |
| 12719 | "unsupported format character '%c' (0x%x) " |
| 12720 | "at index %zd", |
| 12721 | (31<=c && c<=126) ? (char)c : '?', |
| 12722 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12723 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12724 | goto onError; |
| 12725 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12726 | /* pbuf is initialized here. */ |
| 12727 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12728 | if (sign) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12729 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || |
| 12730 | PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 12731 | sign = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12732 | len--; |
| 12733 | } |
| 12734 | else if (flags & F_SIGN) |
| 12735 | sign = '+'; |
| 12736 | else if (flags & F_BLANK) |
| 12737 | sign = ' '; |
| 12738 | else |
| 12739 | sign = 0; |
| 12740 | } |
| 12741 | if (width < len) |
| 12742 | width = len; |
| 12743 | if (rescnt - (sign != 0) < width) { |
| 12744 | reslen -= rescnt; |
| 12745 | rescnt = width + fmtcnt + 100; |
| 12746 | reslen += rescnt; |
| 12747 | if (reslen < 0) { |
| 12748 | Py_XDECREF(temp); |
| 12749 | PyErr_NoMemory(); |
| 12750 | goto onError; |
| 12751 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12752 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12753 | if (res0 == 0) { |
| 12754 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12755 | Py_XDECREF(temp); |
| 12756 | goto onError; |
| 12757 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12758 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12759 | } |
| 12760 | if (sign) { |
| 12761 | if (fill != ' ') |
| 12762 | *res++ = sign; |
| 12763 | rescnt--; |
| 12764 | if (width > len) |
| 12765 | width--; |
| 12766 | } |
| 12767 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12768 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12769 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12770 | if (fill != ' ') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12771 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12772 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12773 | } |
| 12774 | rescnt -= 2; |
| 12775 | width -= 2; |
| 12776 | if (width < 0) |
| 12777 | width = 0; |
| 12778 | len -= 2; |
| 12779 | } |
| 12780 | if (width > len && !(flags & F_LJUST)) { |
| 12781 | do { |
| 12782 | --rescnt; |
| 12783 | *res++ = fill; |
| 12784 | } while (--width > len); |
| 12785 | } |
| 12786 | if (fill == ' ') { |
| 12787 | if (sign) |
| 12788 | *res++ = sign; |
| 12789 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12790 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12791 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
| 12792 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12793 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12794 | } |
| 12795 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12796 | /* Copy all characters, preserving len */ |
| 12797 | len1 = len; |
| 12798 | while (len1--) { |
| 12799 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12800 | rescnt--; |
| 12801 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12802 | while (--width >= len) { |
| 12803 | --rescnt; |
| 12804 | *res++ = ' '; |
| 12805 | } |
| 12806 | if (dict && (argidx < arglen) && c != '%') { |
| 12807 | PyErr_SetString(PyExc_TypeError, |
| 12808 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 12809 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12810 | goto onError; |
| 12811 | } |
| 12812 | Py_XDECREF(temp); |
| 12813 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12814 | } /* until end */ |
| 12815 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12816 | PyErr_SetString(PyExc_TypeError, |
| 12817 | "not all arguments converted during string formatting"); |
| 12818 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12819 | } |
| 12820 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12821 | |
| 12822 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) |
| 12823 | if (*res > max) |
| 12824 | max = *res; |
| 12825 | result = PyUnicode_New(reslen - rescnt, max); |
| 12826 | if (!result) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12827 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12828 | kind = PyUnicode_KIND(result); |
| 12829 | for (res = res0; res < res0+reslen-rescnt; res++) |
| 12830 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); |
| 12831 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12832 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12833 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12834 | } |
| 12835 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12836 | return (PyObject *)result; |
| 12837 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12838 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12839 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12840 | Py_DECREF(uformat); |
| 12841 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12842 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12843 | } |
| 12844 | return NULL; |
| 12845 | } |
| 12846 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 12847 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12848 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 12849 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12850 | static PyObject * |
| 12851 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12852 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12853 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12854 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 12855 | char *encoding = NULL; |
| 12856 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12857 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12858 | if (type != &PyUnicode_Type) |
| 12859 | return unicode_subtype_new(type, args, kwds); |
| 12860 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12861 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12862 | return NULL; |
| 12863 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12864 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12865 | if (encoding == NULL && errors == NULL) |
| 12866 | return PyObject_Str(x); |
| 12867 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12868 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12869 | } |
| 12870 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12871 | static PyObject * |
| 12872 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12873 | { |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12874 | PyUnicodeObject *unicode, *self; |
| 12875 | Py_ssize_t length, char_size; |
| 12876 | int share_wstr, share_utf8; |
| 12877 | unsigned int kind; |
| 12878 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12879 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12880 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12881 | |
| 12882 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 12883 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12884 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 12885 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 12886 | if (_PyUnicode_READY_REPLACE(&unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12887 | return NULL; |
| 12888 | |
| 12889 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); |
| 12890 | if (self == NULL) { |
| 12891 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12892 | return NULL; |
| 12893 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12894 | kind = PyUnicode_KIND(unicode); |
| 12895 | length = PyUnicode_GET_LENGTH(unicode); |
| 12896 | |
| 12897 | _PyUnicode_LENGTH(self) = length; |
| 12898 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 12899 | _PyUnicode_STATE(self).interned = 0; |
| 12900 | _PyUnicode_STATE(self).kind = kind; |
| 12901 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 12902 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12903 | _PyUnicode_STATE(self).ready = 1; |
| 12904 | _PyUnicode_WSTR(self) = NULL; |
| 12905 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 12906 | _PyUnicode_UTF8(self) = NULL; |
| 12907 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12908 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12909 | |
| 12910 | share_utf8 = 0; |
| 12911 | share_wstr = 0; |
| 12912 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12913 | char_size = 1; |
| 12914 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 12915 | share_utf8 = 1; |
| 12916 | } |
| 12917 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12918 | char_size = 2; |
| 12919 | if (sizeof(wchar_t) == 2) |
| 12920 | share_wstr = 1; |
| 12921 | } |
| 12922 | else { |
| 12923 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12924 | char_size = 4; |
| 12925 | if (sizeof(wchar_t) == 4) |
| 12926 | share_wstr = 1; |
| 12927 | } |
| 12928 | |
| 12929 | /* Ensure we won't overflow the length. */ |
| 12930 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 12931 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12932 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12933 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12934 | data = PyObject_MALLOC((length + 1) * char_size); |
| 12935 | if (data == NULL) { |
| 12936 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12937 | goto onError; |
| 12938 | } |
| 12939 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12940 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12941 | if (share_utf8) { |
| 12942 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 12943 | _PyUnicode_UTF8(self) = data; |
| 12944 | } |
| 12945 | if (share_wstr) { |
| 12946 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 12947 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 12948 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12949 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12950 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
| 12951 | PyUnicode_KIND_SIZE(kind, length + 1)); |
| 12952 | Py_DECREF(unicode); |
| 12953 | return (PyObject *)self; |
| 12954 | |
| 12955 | onError: |
| 12956 | Py_DECREF(unicode); |
| 12957 | Py_DECREF(self); |
| 12958 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12959 | } |
| 12960 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12961 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12962 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12963 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 12964 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 12965 | encoding defaults to the current default string encoding.\n\ |
| 12966 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12967 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12968 | static PyObject *unicode_iter(PyObject *seq); |
| 12969 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12970 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 12971 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12972 | "str", /* tp_name */ |
| 12973 | sizeof(PyUnicodeObject), /* tp_size */ |
| 12974 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12975 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12976 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 12977 | 0, /* tp_print */ |
| 12978 | 0, /* tp_getattr */ |
| 12979 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12980 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12981 | unicode_repr, /* tp_repr */ |
| 12982 | &unicode_as_number, /* tp_as_number */ |
| 12983 | &unicode_as_sequence, /* tp_as_sequence */ |
| 12984 | &unicode_as_mapping, /* tp_as_mapping */ |
| 12985 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 12986 | 0, /* tp_call*/ |
| 12987 | (reprfunc) unicode_str, /* tp_str */ |
| 12988 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 12989 | 0, /* tp_setattro */ |
| 12990 | 0, /* tp_as_buffer */ |
| 12991 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12992 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12993 | unicode_doc, /* tp_doc */ |
| 12994 | 0, /* tp_traverse */ |
| 12995 | 0, /* tp_clear */ |
| 12996 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 12997 | 0, /* tp_weaklistoffset */ |
| 12998 | unicode_iter, /* tp_iter */ |
| 12999 | 0, /* tp_iternext */ |
| 13000 | unicode_methods, /* tp_methods */ |
| 13001 | 0, /* tp_members */ |
| 13002 | 0, /* tp_getset */ |
| 13003 | &PyBaseObject_Type, /* tp_base */ |
| 13004 | 0, /* tp_dict */ |
| 13005 | 0, /* tp_descr_get */ |
| 13006 | 0, /* tp_descr_set */ |
| 13007 | 0, /* tp_dictoffset */ |
| 13008 | 0, /* tp_init */ |
| 13009 | 0, /* tp_alloc */ |
| 13010 | unicode_new, /* tp_new */ |
| 13011 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13012 | }; |
| 13013 | |
| 13014 | /* Initialize the Unicode implementation */ |
| 13015 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13016 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13017 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13018 | int i; |
| 13019 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13020 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13021 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13022 | 0x000A, /* LINE FEED */ |
| 13023 | 0x000D, /* CARRIAGE RETURN */ |
| 13024 | 0x001C, /* FILE SEPARATOR */ |
| 13025 | 0x001D, /* GROUP SEPARATOR */ |
| 13026 | 0x001E, /* RECORD SEPARATOR */ |
| 13027 | 0x0085, /* NEXT LINE */ |
| 13028 | 0x2028, /* LINE SEPARATOR */ |
| 13029 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13030 | }; |
| 13031 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13032 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13033 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13034 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13035 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13036 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13037 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13038 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13039 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13040 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13041 | |
| 13042 | /* initialize the linebreak bloom filter */ |
| 13043 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13044 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13045 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13046 | |
| 13047 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13048 | } |
| 13049 | |
| 13050 | /* Finalize the Unicode implementation */ |
| 13051 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13052 | int |
| 13053 | PyUnicode_ClearFreeList(void) |
| 13054 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13055 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13056 | } |
| 13057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13058 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13059 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13060 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13061 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13062 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13063 | Py_XDECREF(unicode_empty); |
| 13064 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13065 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13066 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13067 | if (unicode_latin1[i]) { |
| 13068 | Py_DECREF(unicode_latin1[i]); |
| 13069 | unicode_latin1[i] = NULL; |
| 13070 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13071 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13072 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13073 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13074 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13075 | void |
| 13076 | PyUnicode_InternInPlace(PyObject **p) |
| 13077 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13078 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 13079 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13080 | #ifdef Py_DEBUG |
| 13081 | assert(s != NULL); |
| 13082 | assert(_PyUnicode_CHECK(s)); |
| 13083 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13084 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13085 | return; |
| 13086 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13087 | /* If it's a subclass, we don't really know what putting |
| 13088 | it in the interned dict might do. */ |
| 13089 | if (!PyUnicode_CheckExact(s)) |
| 13090 | return; |
| 13091 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13092 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13093 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13094 | assert(0 && "PyUnicode_READY fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13095 | return; |
| 13096 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13097 | s = (PyUnicodeObject *)(*p); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13098 | if (interned == NULL) { |
| 13099 | interned = PyDict_New(); |
| 13100 | if (interned == NULL) { |
| 13101 | PyErr_Clear(); /* Don't leave an exception */ |
| 13102 | return; |
| 13103 | } |
| 13104 | } |
| 13105 | /* It might be that the GetItem call fails even |
| 13106 | though the key is present in the dictionary, |
| 13107 | namely when this happens during a stack overflow. */ |
| 13108 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13109 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13110 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13111 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13112 | if (t) { |
| 13113 | Py_INCREF(t); |
| 13114 | Py_DECREF(*p); |
| 13115 | *p = t; |
| 13116 | return; |
| 13117 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13118 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13119 | PyThreadState_GET()->recursion_critical = 1; |
| 13120 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 13121 | PyErr_Clear(); |
| 13122 | PyThreadState_GET()->recursion_critical = 0; |
| 13123 | return; |
| 13124 | } |
| 13125 | PyThreadState_GET()->recursion_critical = 0; |
| 13126 | /* The two references in interned are not counted by refcnt. |
| 13127 | The deallocator will take care of this */ |
| 13128 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13129 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13130 | } |
| 13131 | |
| 13132 | void |
| 13133 | PyUnicode_InternImmortal(PyObject **p) |
| 13134 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13135 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 13136 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13137 | PyUnicode_InternInPlace(p); |
| 13138 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13139 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13140 | Py_INCREF(*p); |
| 13141 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13142 | } |
| 13143 | |
| 13144 | PyObject * |
| 13145 | PyUnicode_InternFromString(const char *cp) |
| 13146 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13147 | PyObject *s = PyUnicode_FromString(cp); |
| 13148 | if (s == NULL) |
| 13149 | return NULL; |
| 13150 | PyUnicode_InternInPlace(&s); |
| 13151 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13152 | } |
| 13153 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13154 | void |
| 13155 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13156 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13157 | PyObject *keys; |
| 13158 | PyUnicodeObject *s; |
| 13159 | Py_ssize_t i, n; |
| 13160 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13161 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13162 | if (interned == NULL || !PyDict_Check(interned)) |
| 13163 | return; |
| 13164 | keys = PyDict_Keys(interned); |
| 13165 | if (keys == NULL || !PyList_Check(keys)) { |
| 13166 | PyErr_Clear(); |
| 13167 | return; |
| 13168 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13169 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13170 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 13171 | detector, interned unicode strings are not forcibly deallocated; |
| 13172 | rather, we give them their stolen references back, and then clear |
| 13173 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13174 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13175 | n = PyList_GET_SIZE(keys); |
| 13176 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13177 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13178 | for (i = 0; i < n; i++) { |
| 13179 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13180 | if (PyUnicode_READY(s) == -1) |
| 13181 | fprintf(stderr, "could not ready string\n"); |
| 13182 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13183 | case SSTATE_NOT_INTERNED: |
| 13184 | /* XXX Shouldn't happen */ |
| 13185 | break; |
| 13186 | case SSTATE_INTERNED_IMMORTAL: |
| 13187 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13188 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13189 | break; |
| 13190 | case SSTATE_INTERNED_MORTAL: |
| 13191 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13192 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13193 | break; |
| 13194 | default: |
| 13195 | Py_FatalError("Inconsistent interned string state."); |
| 13196 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13197 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13198 | } |
| 13199 | fprintf(stderr, "total size of all interned strings: " |
| 13200 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 13201 | "mortal/immortal\n", mortal_size, immortal_size); |
| 13202 | Py_DECREF(keys); |
| 13203 | PyDict_Clear(interned); |
| 13204 | Py_DECREF(interned); |
| 13205 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13206 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13207 | |
| 13208 | |
| 13209 | /********************* Unicode Iterator **************************/ |
| 13210 | |
| 13211 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13212 | PyObject_HEAD |
| 13213 | Py_ssize_t it_index; |
| 13214 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13215 | } unicodeiterobject; |
| 13216 | |
| 13217 | static void |
| 13218 | unicodeiter_dealloc(unicodeiterobject *it) |
| 13219 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13220 | _PyObject_GC_UNTRACK(it); |
| 13221 | Py_XDECREF(it->it_seq); |
| 13222 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13223 | } |
| 13224 | |
| 13225 | static int |
| 13226 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 13227 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13228 | Py_VISIT(it->it_seq); |
| 13229 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13230 | } |
| 13231 | |
| 13232 | static PyObject * |
| 13233 | unicodeiter_next(unicodeiterobject *it) |
| 13234 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13235 | PyUnicodeObject *seq; |
| 13236 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13237 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13238 | assert(it != NULL); |
| 13239 | seq = it->it_seq; |
| 13240 | if (seq == NULL) |
| 13241 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13242 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13243 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13244 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 13245 | int kind = PyUnicode_KIND(seq); |
| 13246 | void *data = PyUnicode_DATA(seq); |
| 13247 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 13248 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13249 | if (item != NULL) |
| 13250 | ++it->it_index; |
| 13251 | return item; |
| 13252 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13253 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13254 | Py_DECREF(seq); |
| 13255 | it->it_seq = NULL; |
| 13256 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13257 | } |
| 13258 | |
| 13259 | static PyObject * |
| 13260 | unicodeiter_len(unicodeiterobject *it) |
| 13261 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13262 | Py_ssize_t len = 0; |
| 13263 | if (it->it_seq) |
| 13264 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 13265 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13266 | } |
| 13267 | |
| 13268 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 13269 | |
| 13270 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13271 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13272 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13273 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13274 | }; |
| 13275 | |
| 13276 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13277 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 13278 | "str_iterator", /* tp_name */ |
| 13279 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 13280 | 0, /* tp_itemsize */ |
| 13281 | /* methods */ |
| 13282 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 13283 | 0, /* tp_print */ |
| 13284 | 0, /* tp_getattr */ |
| 13285 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13286 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13287 | 0, /* tp_repr */ |
| 13288 | 0, /* tp_as_number */ |
| 13289 | 0, /* tp_as_sequence */ |
| 13290 | 0, /* tp_as_mapping */ |
| 13291 | 0, /* tp_hash */ |
| 13292 | 0, /* tp_call */ |
| 13293 | 0, /* tp_str */ |
| 13294 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13295 | 0, /* tp_setattro */ |
| 13296 | 0, /* tp_as_buffer */ |
| 13297 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 13298 | 0, /* tp_doc */ |
| 13299 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 13300 | 0, /* tp_clear */ |
| 13301 | 0, /* tp_richcompare */ |
| 13302 | 0, /* tp_weaklistoffset */ |
| 13303 | PyObject_SelfIter, /* tp_iter */ |
| 13304 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 13305 | unicodeiter_methods, /* tp_methods */ |
| 13306 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13307 | }; |
| 13308 | |
| 13309 | static PyObject * |
| 13310 | unicode_iter(PyObject *seq) |
| 13311 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13312 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13313 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13314 | if (!PyUnicode_Check(seq)) { |
| 13315 | PyErr_BadInternalCall(); |
| 13316 | return NULL; |
| 13317 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13318 | if (PyUnicode_READY(seq) == -1) |
| 13319 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13320 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 13321 | if (it == NULL) |
| 13322 | return NULL; |
| 13323 | it->it_index = 0; |
| 13324 | Py_INCREF(seq); |
| 13325 | it->it_seq = (PyUnicodeObject *)seq; |
| 13326 | _PyObject_GC_TRACK(it); |
| 13327 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13328 | } |
| 13329 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13330 | #define UNIOP(x) Py_UNICODE_##x |
| 13331 | #define UNIOP_t Py_UNICODE |
| 13332 | #include "uniops.h" |
| 13333 | #undef UNIOP |
| 13334 | #undef UNIOP_t |
| 13335 | #define UNIOP(x) Py_UCS4_##x |
| 13336 | #define UNIOP_t Py_UCS4 |
| 13337 | #include "uniops.h" |
| 13338 | #undef UNIOP |
| 13339 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 13340 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13341 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 13342 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13343 | { |
| 13344 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 13345 | Py_UNICODE *copy; |
| 13346 | Py_ssize_t size; |
| 13347 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13348 | if (!PyUnicode_Check(unicode)) { |
| 13349 | PyErr_BadArgument(); |
| 13350 | return NULL; |
| 13351 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13352 | /* Ensure we won't overflow the size. */ |
| 13353 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 13354 | PyErr_NoMemory(); |
| 13355 | return NULL; |
| 13356 | } |
| 13357 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 13358 | size *= sizeof(Py_UNICODE); |
| 13359 | copy = PyMem_Malloc(size); |
| 13360 | if (copy == NULL) { |
| 13361 | PyErr_NoMemory(); |
| 13362 | return NULL; |
| 13363 | } |
| 13364 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 13365 | return copy; |
| 13366 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13367 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13368 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 13369 | to the string.Formatter class implemented in Python. */ |
| 13370 | |
| 13371 | static PyMethodDef _string_methods[] = { |
| 13372 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 13373 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 13374 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 13375 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 13376 | {NULL, NULL} |
| 13377 | }; |
| 13378 | |
| 13379 | static struct PyModuleDef _string_module = { |
| 13380 | PyModuleDef_HEAD_INIT, |
| 13381 | "_string", |
| 13382 | PyDoc_STR("string helper module"), |
| 13383 | 0, |
| 13384 | _string_methods, |
| 13385 | NULL, |
| 13386 | NULL, |
| 13387 | NULL, |
| 13388 | NULL |
| 13389 | }; |
| 13390 | |
| 13391 | PyMODINIT_FUNC |
| 13392 | PyInit__string(void) |
| 13393 | { |
| 13394 | return PyModule_Create(&_string_module); |
| 13395 | } |
| 13396 | |
| 13397 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13398 | #ifdef __cplusplus |
| 13399 | } |
| 13400 | #endif |