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 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame^] | 303 | else { |
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 | |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame^] | 307 | if (ascii->state.compact == 1) { |
| 308 | data = compact + 1; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 309 | assert(kind == PyUnicode_1BYTE_KIND |
| 310 | || kind == PyUnicode_2BYTE_KIND |
| 311 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame^] | 312 | assert(ascii->state.ascii == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 313 | assert(ascii->state.ready == 1); |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame^] | 314 | assert (compact->utf8 != data); |
| 315 | } else { |
| 316 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 317 | |
| 318 | data = unicode->data.any; |
| 319 | if (kind == PyUnicode_WCHAR_KIND) { |
| 320 | assert(ascii->state.compact == 0); |
| 321 | assert(ascii->state.ascii == 0); |
| 322 | assert(ascii->state.ready == 0); |
| 323 | assert(ascii->wstr != NULL); |
| 324 | assert(data == NULL); |
| 325 | assert(compact->utf8 == NULL); |
| 326 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
| 327 | } |
| 328 | else { |
| 329 | assert(kind == PyUnicode_1BYTE_KIND |
| 330 | || kind == PyUnicode_2BYTE_KIND |
| 331 | || kind == PyUnicode_4BYTE_KIND); |
| 332 | assert(ascii->state.compact == 0); |
| 333 | assert(ascii->state.ready == 1); |
| 334 | assert(data != NULL); |
| 335 | if (ascii->state.ascii) { |
| 336 | assert (compact->utf8 == data); |
| 337 | assert (compact->utf8_length == ascii->length); |
| 338 | } |
| 339 | else |
| 340 | assert (compact->utf8 != data); |
| 341 | } |
| 342 | } |
| 343 | if (kind != PyUnicode_WCHAR_KIND) { |
Victor Stinner | 7f11ad4 | 2011-10-04 00:00:20 +0200 | [diff] [blame] | 344 | if ( |
| 345 | #if SIZEOF_WCHAR_T == 2 |
| 346 | kind == PyUnicode_2BYTE_KIND |
| 347 | #else |
| 348 | kind == PyUnicode_4BYTE_KIND |
| 349 | #endif |
| 350 | ) |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame^] | 351 | { |
| 352 | assert(ascii->wstr == data); |
| 353 | assert(compact->wstr_length == ascii->length); |
| 354 | } else |
| 355 | assert(ascii->wstr != data); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 356 | } |
Victor Stinner | a41463c | 2011-10-04 01:05:08 +0200 | [diff] [blame^] | 357 | |
| 358 | if (compact->utf8 == NULL) |
| 359 | assert(compact->utf8_length == 0); |
| 360 | if (ascii->wstr == NULL) |
| 361 | assert(compact->wstr_length == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 362 | } |
| 363 | return 1; |
| 364 | } |
| 365 | #endif |
| 366 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 367 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 368 | |
| 369 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 370 | to keep things simple, we use a single bitmask, using the least 5 |
| 371 | bits from each unicode characters as the bit index. */ |
| 372 | |
| 373 | /* the linebreak mask is set up by Unicode_Init below */ |
| 374 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 375 | #if LONG_BIT >= 128 |
| 376 | #define BLOOM_WIDTH 128 |
| 377 | #elif LONG_BIT >= 64 |
| 378 | #define BLOOM_WIDTH 64 |
| 379 | #elif LONG_BIT >= 32 |
| 380 | #define BLOOM_WIDTH 32 |
| 381 | #else |
| 382 | #error "LONG_BIT is smaller than 32" |
| 383 | #endif |
| 384 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 385 | #define BLOOM_MASK unsigned long |
| 386 | |
| 387 | static BLOOM_MASK bloom_linebreak; |
| 388 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 389 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 390 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 391 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 392 | #define BLOOM_LINEBREAK(ch) \ |
| 393 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 394 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 395 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 396 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 397 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 398 | { |
| 399 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 400 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 401 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 402 | Py_ssize_t i; |
| 403 | |
| 404 | mask = 0; |
| 405 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 406 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 407 | |
| 408 | return mask; |
| 409 | } |
| 410 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 411 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 412 | (BLOOM(mask, chr) \ |
| 413 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 414 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 415 | /* --- Unicode Object ----------------------------------------------------- */ |
| 416 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 417 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 418 | fixup(PyUnicodeObject *self, Py_UCS4 (*fixfct)(PyUnicodeObject *s)); |
| 419 | |
| 420 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 421 | Py_ssize_t size, Py_UCS4 ch, |
| 422 | int direction) |
| 423 | { |
| 424 | /* like wcschr, but doesn't stop at NULL characters */ |
| 425 | Py_ssize_t i; |
| 426 | if (direction == 1) { |
| 427 | for(i = 0; i < size; i++) |
| 428 | if (PyUnicode_READ(kind, s, i) == ch) |
| 429 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 430 | } |
| 431 | else { |
| 432 | for(i = size-1; i >= 0; i--) |
| 433 | if (PyUnicode_READ(kind, s, i) == ch) |
| 434 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 435 | } |
| 436 | return NULL; |
| 437 | } |
| 438 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 439 | static PyObject* |
| 440 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 441 | { |
| 442 | Py_ssize_t char_size; |
| 443 | Py_ssize_t struct_size; |
| 444 | Py_ssize_t new_size; |
| 445 | int share_wstr; |
| 446 | |
| 447 | assert(PyUnicode_IS_READY(unicode)); |
| 448 | char_size = PyUnicode_CHARACTER_SIZE(unicode); |
| 449 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 450 | struct_size = sizeof(PyASCIIObject); |
| 451 | else |
| 452 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 453 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 454 | |
| 455 | _Py_DEC_REFTOTAL; |
| 456 | _Py_ForgetReference(unicode); |
| 457 | |
| 458 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 459 | PyErr_NoMemory(); |
| 460 | return NULL; |
| 461 | } |
| 462 | new_size = (struct_size + (length + 1) * char_size); |
| 463 | |
| 464 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 465 | if (unicode == NULL) { |
| 466 | PyObject_Del(unicode); |
| 467 | PyErr_NoMemory(); |
| 468 | return NULL; |
| 469 | } |
| 470 | _Py_NewReference(unicode); |
| 471 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 472 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 473 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 474 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 475 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 476 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 477 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 478 | length, 0); |
| 479 | return unicode; |
| 480 | } |
| 481 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 482 | static int |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 483 | resize_inplace(PyUnicodeObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 484 | { |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 485 | wchar_t *wstr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 486 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 487 | assert(Py_REFCNT(unicode) == 1); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 488 | |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 489 | _PyUnicode_DIRTY(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 490 | |
| 491 | if (PyUnicode_IS_READY(unicode)) { |
| 492 | Py_ssize_t char_size; |
| 493 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 494 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 495 | void *data; |
| 496 | |
| 497 | data = _PyUnicode_DATA_ANY(unicode); |
| 498 | assert(data != NULL); |
| 499 | char_size = PyUnicode_CHARACTER_SIZE(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 500 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 501 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 502 | if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 503 | { |
| 504 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 505 | _PyUnicode_UTF8(unicode) = NULL; |
| 506 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
| 507 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 508 | |
| 509 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 510 | PyErr_NoMemory(); |
| 511 | return -1; |
| 512 | } |
| 513 | new_size = (length + 1) * char_size; |
| 514 | |
| 515 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 516 | if (data == NULL) { |
| 517 | PyErr_NoMemory(); |
| 518 | return -1; |
| 519 | } |
| 520 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 521 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 522 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 523 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 524 | } |
| 525 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 526 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 527 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 528 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 529 | _PyUnicode_LENGTH(unicode) = length; |
| 530 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 531 | if (share_wstr || _PyUnicode_WSTR(unicode) == NULL) { |
| 532 | _PyUnicode_CHECK(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 533 | return 0; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 534 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 535 | } |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 536 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 537 | |
| 538 | /* check for integer overflow */ |
| 539 | if (length > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) { |
| 540 | PyErr_NoMemory(); |
| 541 | return -1; |
| 542 | } |
| 543 | wstr = _PyUnicode_WSTR(unicode); |
| 544 | wstr = PyObject_REALLOC(wstr, sizeof(wchar_t) * (length + 1)); |
| 545 | if (!wstr) { |
| 546 | PyErr_NoMemory(); |
| 547 | return -1; |
| 548 | } |
| 549 | _PyUnicode_WSTR(unicode) = wstr; |
| 550 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 551 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 552 | _PyUnicode_CHECK(unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 556 | static PyObject* |
| 557 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 558 | { |
| 559 | Py_ssize_t copy_length; |
| 560 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 561 | PyObject *copy; |
| 562 | assert(PyUnicode_IS_READY(unicode)); |
| 563 | |
| 564 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 565 | if (copy == NULL) |
| 566 | return NULL; |
| 567 | |
| 568 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
| 569 | if (PyUnicode_CopyCharacters(copy, 0, |
| 570 | unicode, 0, |
| 571 | copy_length) < 0) |
| 572 | { |
| 573 | Py_DECREF(copy); |
| 574 | return NULL; |
| 575 | } |
| 576 | return copy; |
Victor Stinner | 8cfcbed | 2011-10-03 23:19:21 +0200 | [diff] [blame] | 577 | } |
| 578 | else { |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 579 | PyUnicodeObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 580 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 581 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 582 | w = _PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 583 | if (w == NULL) |
| 584 | return NULL; |
| 585 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 586 | copy_length = Py_MIN(copy_length, length); |
| 587 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 588 | copy_length); |
| 589 | return (PyObject*)w; |
| 590 | } |
| 591 | } |
| 592 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 593 | /* 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] | 594 | Ux0000 terminated; some code (e.g. new_identifier) |
| 595 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 596 | |
| 597 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 598 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 599 | |
| 600 | */ |
| 601 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 602 | #ifdef Py_DEBUG |
| 603 | int unicode_old_new_calls = 0; |
| 604 | #endif |
| 605 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 606 | static PyUnicodeObject * |
| 607 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 608 | { |
| 609 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 610 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 611 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 612 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 613 | if (length == 0 && unicode_empty != NULL) { |
| 614 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 615 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 618 | /* Ensure we won't overflow the size. */ |
| 619 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 620 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 621 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 622 | if (length < 0) { |
| 623 | PyErr_SetString(PyExc_SystemError, |
| 624 | "Negative size passed to _PyUnicode_New"); |
| 625 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 628 | #ifdef Py_DEBUG |
| 629 | ++unicode_old_new_calls; |
| 630 | #endif |
| 631 | |
| 632 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 633 | if (unicode == NULL) |
| 634 | return NULL; |
| 635 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 636 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 637 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 638 | PyErr_NoMemory(); |
| 639 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 640 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 641 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 642 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 643 | * the caller fails before initializing str -- unicode_resize() |
| 644 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 645 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 646 | * We don't want unicode_resize to read uninitialized memory in |
| 647 | * that case. |
| 648 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 649 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 650 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 651 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 652 | _PyUnicode_HASH(unicode) = -1; |
| 653 | _PyUnicode_STATE(unicode).interned = 0; |
| 654 | _PyUnicode_STATE(unicode).kind = 0; |
| 655 | _PyUnicode_STATE(unicode).compact = 0; |
| 656 | _PyUnicode_STATE(unicode).ready = 0; |
| 657 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 658 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 659 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 660 | _PyUnicode_UTF8(unicode) = NULL; |
| 661 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 662 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 663 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 664 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 665 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 666 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 667 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 668 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 669 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 672 | static const char* |
| 673 | unicode_kind_name(PyObject *unicode) |
| 674 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame] | 675 | /* don't check consistency: unicode_kind_name() is called from |
| 676 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 677 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 678 | { |
| 679 | if (!PyUnicode_IS_READY(unicode)) |
| 680 | return "wstr"; |
| 681 | switch(PyUnicode_KIND(unicode)) |
| 682 | { |
| 683 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 684 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 685 | return "legacy ascii"; |
| 686 | else |
| 687 | return "legacy latin1"; |
| 688 | case PyUnicode_2BYTE_KIND: |
| 689 | return "legacy UCS2"; |
| 690 | case PyUnicode_4BYTE_KIND: |
| 691 | return "legacy UCS4"; |
| 692 | default: |
| 693 | return "<legacy invalid kind>"; |
| 694 | } |
| 695 | } |
| 696 | assert(PyUnicode_IS_READY(unicode)); |
| 697 | switch(PyUnicode_KIND(unicode)) |
| 698 | { |
| 699 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 700 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 701 | return "ascii"; |
| 702 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 703 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 704 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 705 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 706 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 707 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 708 | default: |
| 709 | return "<invalid compact kind>"; |
| 710 | } |
| 711 | } |
| 712 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 713 | #ifdef Py_DEBUG |
| 714 | int unicode_new_new_calls = 0; |
| 715 | |
| 716 | /* Functions wrapping macros for use in debugger */ |
| 717 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 718 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | void *_PyUnicode_compact_data(void *unicode) { |
| 722 | return _PyUnicode_COMPACT_DATA(unicode); |
| 723 | } |
| 724 | void *_PyUnicode_data(void *unicode){ |
| 725 | printf("obj %p\n", unicode); |
| 726 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 727 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 728 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 729 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 730 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 731 | return PyUnicode_DATA(unicode); |
| 732 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 733 | |
| 734 | void |
| 735 | _PyUnicode_Dump(PyObject *op) |
| 736 | { |
| 737 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 738 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 739 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 740 | void *data; |
| 741 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 742 | if (ascii->state.compact) |
| 743 | data = (compact + 1); |
| 744 | else |
| 745 | data = unicode->data.any; |
| 746 | if (ascii->wstr == data) |
| 747 | printf("shared "); |
| 748 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 749 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 750 | printf(" (%zu), ", compact->wstr_length); |
| 751 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 752 | printf("shared "); |
| 753 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 754 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 755 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 756 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 757 | #endif |
| 758 | |
| 759 | PyObject * |
| 760 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 761 | { |
| 762 | PyObject *obj; |
| 763 | PyCompactUnicodeObject *unicode; |
| 764 | void *data; |
| 765 | int kind_state; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 766 | int is_sharing, is_ascii; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 767 | Py_ssize_t char_size; |
| 768 | Py_ssize_t struct_size; |
| 769 | |
| 770 | /* Optimization for empty strings */ |
| 771 | if (size == 0 && unicode_empty != NULL) { |
| 772 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 773 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | #ifdef Py_DEBUG |
| 777 | ++unicode_new_new_calls; |
| 778 | #endif |
| 779 | |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 780 | is_ascii = 0; |
| 781 | is_sharing = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 782 | struct_size = sizeof(PyCompactUnicodeObject); |
| 783 | if (maxchar < 128) { |
| 784 | kind_state = PyUnicode_1BYTE_KIND; |
| 785 | char_size = 1; |
| 786 | is_ascii = 1; |
| 787 | struct_size = sizeof(PyASCIIObject); |
| 788 | } |
| 789 | else if (maxchar < 256) { |
| 790 | kind_state = PyUnicode_1BYTE_KIND; |
| 791 | char_size = 1; |
| 792 | } |
| 793 | else if (maxchar < 65536) { |
| 794 | kind_state = PyUnicode_2BYTE_KIND; |
| 795 | char_size = 2; |
| 796 | if (sizeof(wchar_t) == 2) |
| 797 | is_sharing = 1; |
| 798 | } |
| 799 | else { |
| 800 | kind_state = PyUnicode_4BYTE_KIND; |
| 801 | char_size = 4; |
| 802 | if (sizeof(wchar_t) == 4) |
| 803 | is_sharing = 1; |
| 804 | } |
| 805 | |
| 806 | /* Ensure we won't overflow the size. */ |
| 807 | if (size < 0) { |
| 808 | PyErr_SetString(PyExc_SystemError, |
| 809 | "Negative size passed to PyUnicode_New"); |
| 810 | return NULL; |
| 811 | } |
| 812 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 813 | return PyErr_NoMemory(); |
| 814 | |
| 815 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 816 | * PyObject_New() so we are able to allocate space for the object and |
| 817 | * it's data buffer. |
| 818 | */ |
| 819 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 820 | if (obj == NULL) |
| 821 | return PyErr_NoMemory(); |
| 822 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 823 | if (obj == NULL) |
| 824 | return NULL; |
| 825 | |
| 826 | unicode = (PyCompactUnicodeObject *)obj; |
| 827 | if (is_ascii) |
| 828 | data = ((PyASCIIObject*)obj) + 1; |
| 829 | else |
| 830 | data = unicode + 1; |
| 831 | _PyUnicode_LENGTH(unicode) = size; |
| 832 | _PyUnicode_HASH(unicode) = -1; |
| 833 | _PyUnicode_STATE(unicode).interned = 0; |
| 834 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 835 | _PyUnicode_STATE(unicode).compact = 1; |
| 836 | _PyUnicode_STATE(unicode).ready = 1; |
| 837 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 838 | if (is_ascii) { |
| 839 | ((char*)data)[size] = 0; |
| 840 | _PyUnicode_WSTR(unicode) = NULL; |
| 841 | } |
| 842 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 843 | ((char*)data)[size] = 0; |
| 844 | _PyUnicode_WSTR(unicode) = NULL; |
| 845 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 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 | } |
| 849 | else { |
| 850 | unicode->utf8 = NULL; |
Victor Stinner | 9e9d689 | 2011-10-04 01:02:02 +0200 | [diff] [blame] | 851 | unicode->utf8_length = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 852 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 853 | ((Py_UCS2*)data)[size] = 0; |
| 854 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 855 | ((Py_UCS4*)data)[size] = 0; |
| 856 | if (is_sharing) { |
| 857 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 858 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 859 | } |
| 860 | else { |
| 861 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 862 | _PyUnicode_WSTR(unicode) = NULL; |
| 863 | } |
| 864 | } |
| 865 | return obj; |
| 866 | } |
| 867 | |
| 868 | #if SIZEOF_WCHAR_T == 2 |
| 869 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 870 | will decode surrogate pairs, the other conversions are implemented as macros |
| 871 | for efficency. |
| 872 | |
| 873 | This function assumes that unicode can hold one more code point than wstr |
| 874 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 875 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 876 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 877 | PyUnicodeObject *unicode) |
| 878 | { |
| 879 | const wchar_t *iter; |
| 880 | Py_UCS4 *ucs4_out; |
| 881 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 882 | assert(unicode != NULL); |
| 883 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 884 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 885 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 886 | |
| 887 | for (iter = begin; iter < end; ) { |
| 888 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 889 | _PyUnicode_GET_LENGTH(unicode))); |
| 890 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 891 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 892 | { |
| 893 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 894 | iter += 2; |
| 895 | } |
| 896 | else { |
| 897 | *ucs4_out++ = *iter; |
| 898 | iter++; |
| 899 | } |
| 900 | } |
| 901 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 902 | _PyUnicode_GET_LENGTH(unicode))); |
| 903 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 904 | } |
| 905 | #endif |
| 906 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 907 | static int |
| 908 | _PyUnicode_Dirty(PyObject *unicode) |
| 909 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 910 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 911 | if (Py_REFCNT(unicode) != 1) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 912 | PyErr_SetString(PyExc_SystemError, |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 913 | "Cannot modify a string having more than 1 reference"); |
| 914 | return -1; |
| 915 | } |
| 916 | _PyUnicode_DIRTY(unicode); |
| 917 | return 0; |
| 918 | } |
| 919 | |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 920 | Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 921 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 922 | PyObject *from, Py_ssize_t from_start, |
| 923 | Py_ssize_t how_many) |
| 924 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 925 | unsigned int from_kind, to_kind; |
| 926 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 927 | |
Victor Stinner | b153615 | 2011-09-30 02:26:10 +0200 | [diff] [blame] | 928 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 929 | PyErr_BadInternalCall(); |
| 930 | return -1; |
| 931 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 932 | |
| 933 | if (PyUnicode_READY(from)) |
| 934 | return -1; |
| 935 | if (PyUnicode_READY(to)) |
| 936 | return -1; |
| 937 | |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 938 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 939 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 940 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 941 | "Cannot write %zi characters at %zi " |
| 942 | "in a string of %zi characters", |
| 943 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 944 | return -1; |
| 945 | } |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 946 | if (how_many == 0) |
| 947 | return 0; |
| 948 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 949 | if (_PyUnicode_Dirty(to)) |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 950 | return -1; |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 951 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 952 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 953 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 954 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 955 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 956 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 957 | if (from_kind == to_kind |
| 958 | /* deny latin1 => ascii */ |
| 959 | && PyUnicode_MAX_CHAR_VALUE(to) >= PyUnicode_MAX_CHAR_VALUE(from)) |
| 960 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 961 | Py_MEMCPY((char*)to_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 962 | + PyUnicode_KIND_SIZE(to_kind, to_start), |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 963 | (char*)from_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 964 | + PyUnicode_KIND_SIZE(from_kind, from_start), |
| 965 | PyUnicode_KIND_SIZE(to_kind, how_many)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 966 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 967 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 968 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 969 | { |
| 970 | _PyUnicode_CONVERT_BYTES( |
| 971 | Py_UCS1, Py_UCS2, |
| 972 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 973 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 974 | PyUnicode_2BYTE_DATA(to) + to_start |
| 975 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 976 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 977 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 978 | && to_kind == PyUnicode_4BYTE_KIND) |
| 979 | { |
| 980 | _PyUnicode_CONVERT_BYTES( |
| 981 | Py_UCS1, Py_UCS4, |
| 982 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 983 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 984 | PyUnicode_4BYTE_DATA(to) + to_start |
| 985 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 986 | } |
| 987 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 988 | && to_kind == PyUnicode_4BYTE_KIND) |
| 989 | { |
| 990 | _PyUnicode_CONVERT_BYTES( |
| 991 | Py_UCS2, Py_UCS4, |
| 992 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 993 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 994 | PyUnicode_4BYTE_DATA(to) + to_start |
| 995 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 996 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 997 | else { |
| 998 | int invalid_kinds; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 999 | |
| 1000 | /* check if max_char(from substring) <= max_char(to) */ |
| 1001 | if (from_kind > to_kind |
| 1002 | /* latin1 => ascii */ |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1003 | || (PyUnicode_IS_ASCII(to) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1004 | && to_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1005 | && !PyUnicode_IS_ASCII(from))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1006 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1007 | /* slow path to check for character overflow */ |
| 1008 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 1009 | Py_UCS4 ch, maxchar; |
| 1010 | Py_ssize_t i; |
| 1011 | |
| 1012 | maxchar = 0; |
| 1013 | invalid_kinds = 0; |
| 1014 | for (i=0; i < how_many; i++) { |
| 1015 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 1016 | if (ch > maxchar) { |
| 1017 | maxchar = ch; |
| 1018 | if (maxchar > to_maxchar) { |
| 1019 | invalid_kinds = 1; |
| 1020 | break; |
| 1021 | } |
| 1022 | } |
| 1023 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 1024 | } |
| 1025 | } |
| 1026 | else |
| 1027 | invalid_kinds = 1; |
| 1028 | if (invalid_kinds) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1029 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 1030 | "Cannot copy %s characters " |
| 1031 | "into a string of %s characters", |
| 1032 | unicode_kind_name(from), |
| 1033 | unicode_kind_name(to)); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 1034 | return -1; |
| 1035 | } |
| 1036 | } |
| 1037 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1040 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 1041 | correct string length can be computed before converting a string to UCS4. |
| 1042 | This function counts single surrogates as a character and not as a pair. |
| 1043 | |
| 1044 | Return 0 on success, or -1 on error. */ |
| 1045 | static int |
| 1046 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 1047 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | { |
| 1049 | const wchar_t *iter; |
| 1050 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1051 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1052 | if (num_surrogates == NULL || maxchar == NULL) { |
| 1053 | PyErr_SetString(PyExc_SystemError, |
| 1054 | "unexpected NULL arguments to " |
| 1055 | "PyUnicode_FindMaxCharAndNumSurrogatePairs"); |
| 1056 | return -1; |
| 1057 | } |
| 1058 | |
| 1059 | *num_surrogates = 0; |
| 1060 | *maxchar = 0; |
| 1061 | |
| 1062 | for (iter = begin; iter < end; ) { |
| 1063 | if (*iter > *maxchar) |
| 1064 | *maxchar = *iter; |
| 1065 | #if SIZEOF_WCHAR_T == 2 |
| 1066 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1067 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1068 | { |
| 1069 | Py_UCS4 surrogate_val; |
| 1070 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1071 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1072 | ++(*num_surrogates); |
| 1073 | if (surrogate_val > *maxchar) |
| 1074 | *maxchar = surrogate_val; |
| 1075 | iter += 2; |
| 1076 | } |
| 1077 | else |
| 1078 | iter++; |
| 1079 | #else |
| 1080 | iter++; |
| 1081 | #endif |
| 1082 | } |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | #ifdef Py_DEBUG |
| 1087 | int unicode_ready_calls = 0; |
| 1088 | #endif |
| 1089 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1090 | static int |
| 1091 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1092 | { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1093 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1094 | wchar_t *end; |
| 1095 | Py_UCS4 maxchar = 0; |
| 1096 | Py_ssize_t num_surrogates; |
| 1097 | #if SIZEOF_WCHAR_T == 2 |
| 1098 | Py_ssize_t length_wo_surrogates; |
| 1099 | #endif |
| 1100 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1101 | assert(p_obj != NULL); |
| 1102 | unicode = (PyUnicodeObject *)*p_obj; |
| 1103 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1104 | /* _PyUnicode_Ready() is only intented for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1105 | strings were created using _PyObject_New() and where no canonical |
| 1106 | representation (the str field) has been set yet aka strings |
| 1107 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1108 | assert(_PyUnicode_CHECK(unicode)); |
| 1109 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1110 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1111 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1112 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1113 | /* Actually, it should neither be interned nor be anything else: */ |
| 1114 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1115 | |
| 1116 | #ifdef Py_DEBUG |
| 1117 | ++unicode_ready_calls; |
| 1118 | #endif |
| 1119 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1120 | #ifdef Py_DEBUG |
| 1121 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1122 | #else |
| 1123 | if (replace && Py_REFCNT(unicode) != 1) |
| 1124 | replace = 0; |
| 1125 | #endif |
| 1126 | if (replace) { |
| 1127 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1128 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1129 | /* Optimization for empty strings */ |
| 1130 | if (len == 0) { |
| 1131 | Py_INCREF(unicode_empty); |
| 1132 | Py_DECREF(*p_obj); |
| 1133 | *p_obj = unicode_empty; |
| 1134 | return 0; |
| 1135 | } |
| 1136 | if (len == 1 && wstr[0] < 256) { |
| 1137 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1138 | if (latin1_char == NULL) |
| 1139 | return -1; |
| 1140 | Py_DECREF(*p_obj); |
| 1141 | *p_obj = latin1_char; |
| 1142 | return 0; |
| 1143 | } |
| 1144 | } |
| 1145 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1147 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1148 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1149 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1150 | |
| 1151 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1152 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1153 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1154 | PyErr_NoMemory(); |
| 1155 | return -1; |
| 1156 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1157 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1158 | _PyUnicode_WSTR(unicode), end, |
| 1159 | PyUnicode_1BYTE_DATA(unicode)); |
| 1160 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1161 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1162 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1163 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1164 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1165 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1166 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1167 | } |
| 1168 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1169 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1170 | _PyUnicode_UTF8(unicode) = NULL; |
| 1171 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1172 | } |
| 1173 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1174 | _PyUnicode_WSTR(unicode) = NULL; |
| 1175 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1176 | } |
| 1177 | /* In this case we might have to convert down from 4-byte native |
| 1178 | wchar_t to 2-byte unicode. */ |
| 1179 | else if (maxchar < 65536) { |
| 1180 | assert(num_surrogates == 0 && |
| 1181 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1182 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1183 | #if SIZEOF_WCHAR_T == 2 |
| 1184 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1185 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1186 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1187 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1188 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1189 | _PyUnicode_UTF8(unicode) = NULL; |
| 1190 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1191 | #else |
| 1192 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1193 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1194 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1195 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1196 | PyErr_NoMemory(); |
| 1197 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1198 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1199 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1200 | _PyUnicode_WSTR(unicode), end, |
| 1201 | PyUnicode_2BYTE_DATA(unicode)); |
| 1202 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1203 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1204 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1205 | _PyUnicode_UTF8(unicode) = NULL; |
| 1206 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1207 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1208 | _PyUnicode_WSTR(unicode) = NULL; |
| 1209 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1210 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1211 | } |
| 1212 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1213 | else { |
| 1214 | #if SIZEOF_WCHAR_T == 2 |
| 1215 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1216 | new normalized 4-byte version. */ |
| 1217 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1218 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1219 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1220 | PyErr_NoMemory(); |
| 1221 | return -1; |
| 1222 | } |
| 1223 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1224 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1225 | _PyUnicode_UTF8(unicode) = NULL; |
| 1226 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1227 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1228 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1229 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1230 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1231 | _PyUnicode_WSTR(unicode) = NULL; |
| 1232 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1233 | #else |
| 1234 | assert(num_surrogates == 0); |
| 1235 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1236 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1237 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1238 | _PyUnicode_UTF8(unicode) = NULL; |
| 1239 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1240 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1241 | #endif |
| 1242 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1243 | } |
| 1244 | _PyUnicode_STATE(unicode).ready = 1; |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1248 | int |
| 1249 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1250 | { |
| 1251 | return unicode_ready(op, 1); |
| 1252 | } |
| 1253 | |
| 1254 | int |
| 1255 | _PyUnicode_Ready(PyObject *op) |
| 1256 | { |
| 1257 | return unicode_ready(&op, 0); |
| 1258 | } |
| 1259 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1260 | static void |
| 1261 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1262 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1263 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1264 | case SSTATE_NOT_INTERNED: |
| 1265 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1266 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1267 | case SSTATE_INTERNED_MORTAL: |
| 1268 | /* revive dead object temporarily for DelItem */ |
| 1269 | Py_REFCNT(unicode) = 3; |
| 1270 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 1271 | Py_FatalError( |
| 1272 | "deletion of interned string failed"); |
| 1273 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1274 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1275 | case SSTATE_INTERNED_IMMORTAL: |
| 1276 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1277 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1278 | default: |
| 1279 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 1282 | if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1283 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1284 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1285 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1286 | |
| 1287 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1288 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1289 | } |
| 1290 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1291 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1292 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1293 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1297 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1298 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1299 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1300 | if (Py_REFCNT(unicode) != 1) |
| 1301 | return 0; |
| 1302 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1303 | return 0; |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1304 | assert (unicode != unicode_empty); |
| 1305 | #ifdef Py_DEBUG |
| 1306 | if (_PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND |
| 1307 | && PyUnicode_GET_LENGTH(unicode) == 1) |
| 1308 | { |
| 1309 | Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1310 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1311 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1312 | } |
Victor Stinner | 77bb47b | 2011-10-03 20:06:05 +0200 | [diff] [blame] | 1313 | #endif |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1314 | return 1; |
| 1315 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1316 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1317 | static int |
| 1318 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1319 | { |
| 1320 | PyObject *unicode; |
| 1321 | Py_ssize_t old_length; |
| 1322 | |
| 1323 | assert(p_unicode != NULL); |
| 1324 | unicode = *p_unicode; |
| 1325 | |
| 1326 | assert(unicode != NULL); |
| 1327 | assert(PyUnicode_Check(unicode)); |
| 1328 | assert(0 <= length); |
| 1329 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1330 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1331 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1332 | else |
| 1333 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1334 | if (old_length == length) |
| 1335 | return 0; |
| 1336 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1337 | if (!unicode_resizable(unicode)) { |
| 1338 | PyObject *copy = resize_copy(unicode, length); |
| 1339 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1340 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1341 | Py_DECREF(*p_unicode); |
| 1342 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1343 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1346 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1347 | *p_unicode = resize_compact(unicode, length); |
| 1348 | if (*p_unicode == NULL) |
| 1349 | return -1; |
Victor Stinner | 9566311 | 2011-10-04 01:03:50 +0200 | [diff] [blame] | 1350 | _PyUnicode_CHECK(*p_unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1351 | return 0; |
| 1352 | } else |
| 1353 | return resize_inplace((PyUnicodeObject*)unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1356 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1357 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1358 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1359 | PyObject *unicode; |
| 1360 | if (p_unicode == NULL) { |
| 1361 | PyErr_BadInternalCall(); |
| 1362 | return -1; |
| 1363 | } |
| 1364 | unicode = *p_unicode; |
| 1365 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 |
| 1366 | || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) |
| 1367 | { |
| 1368 | PyErr_BadInternalCall(); |
| 1369 | return -1; |
| 1370 | } |
| 1371 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1372 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1373 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1374 | static PyObject* |
| 1375 | get_latin1_char(unsigned char ch) |
| 1376 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1377 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1378 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1379 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1380 | if (!unicode) |
| 1381 | return NULL; |
| 1382 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
| 1383 | unicode_latin1[ch] = unicode; |
| 1384 | } |
| 1385 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1386 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1389 | PyObject * |
| 1390 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1391 | { |
| 1392 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1393 | Py_UCS4 maxchar = 0; |
| 1394 | Py_ssize_t num_surrogates; |
| 1395 | |
| 1396 | if (u == NULL) |
| 1397 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1398 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1399 | /* If the Unicode data is known at construction time, we can apply |
| 1400 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1401 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1402 | /* Optimization for empty strings */ |
| 1403 | if (size == 0 && unicode_empty != NULL) { |
| 1404 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1405 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1406 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1407 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1408 | /* Single character Unicode objects in the Latin-1 range are |
| 1409 | shared when using this constructor */ |
| 1410 | if (size == 1 && *u < 256) |
| 1411 | return get_latin1_char((unsigned char)*u); |
| 1412 | |
| 1413 | /* If not empty and not single character, copy the Unicode data |
| 1414 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1415 | if (find_maxchar_surrogates(u, u + size, |
| 1416 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1417 | return NULL; |
| 1418 | |
| 1419 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1420 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1421 | if (!unicode) |
| 1422 | return NULL; |
| 1423 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1424 | switch (PyUnicode_KIND(unicode)) { |
| 1425 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1426 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1427 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1428 | break; |
| 1429 | case PyUnicode_2BYTE_KIND: |
| 1430 | #if Py_UNICODE_SIZE == 2 |
| 1431 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1432 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1433 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1434 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1435 | #endif |
| 1436 | break; |
| 1437 | case PyUnicode_4BYTE_KIND: |
| 1438 | #if SIZEOF_WCHAR_T == 2 |
| 1439 | /* This is the only case which has to process surrogates, thus |
| 1440 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1441 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1442 | #else |
| 1443 | assert(num_surrogates == 0); |
| 1444 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1445 | #endif |
| 1446 | break; |
| 1447 | default: |
| 1448 | assert(0 && "Impossible state"); |
| 1449 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1450 | |
| 1451 | return (PyObject *)unicode; |
| 1452 | } |
| 1453 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1454 | PyObject * |
| 1455 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1456 | { |
| 1457 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1458 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1459 | if (size < 0) { |
| 1460 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1461 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1462 | return NULL; |
| 1463 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1464 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1465 | /* 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] | 1466 | some optimizations which share commonly used objects. |
| 1467 | Also, this means the input must be UTF-8, so fall back to the |
| 1468 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1469 | if (u != NULL) { |
| 1470 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1471 | /* Optimization for empty strings */ |
| 1472 | if (size == 0 && unicode_empty != NULL) { |
| 1473 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1474 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1475 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1476 | |
| 1477 | /* Single characters are shared when using this constructor. |
| 1478 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1479 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1480 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1481 | |
| 1482 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1485 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1486 | if (!unicode) |
| 1487 | return NULL; |
| 1488 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1489 | return (PyObject *)unicode; |
| 1490 | } |
| 1491 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1492 | PyObject * |
| 1493 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1494 | { |
| 1495 | size_t size = strlen(u); |
| 1496 | if (size > PY_SSIZE_T_MAX) { |
| 1497 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1498 | return NULL; |
| 1499 | } |
| 1500 | |
| 1501 | return PyUnicode_FromStringAndSize(u, size); |
| 1502 | } |
| 1503 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1504 | static PyObject* |
| 1505 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1506 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1507 | PyObject *res; |
| 1508 | unsigned char max = 127; |
| 1509 | Py_ssize_t i; |
| 1510 | for (i = 0; i < size; i++) { |
| 1511 | if (u[i] & 0x80) { |
| 1512 | max = 255; |
| 1513 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1514 | } |
| 1515 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1516 | res = PyUnicode_New(size, max); |
| 1517 | if (!res) |
| 1518 | return NULL; |
| 1519 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
| 1520 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1523 | static PyObject* |
| 1524 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1525 | { |
| 1526 | PyObject *res; |
| 1527 | Py_UCS2 max = 0; |
| 1528 | Py_ssize_t i; |
| 1529 | for (i = 0; i < size; i++) |
| 1530 | if (u[i] > max) |
| 1531 | max = u[i]; |
| 1532 | res = PyUnicode_New(size, max); |
| 1533 | if (!res) |
| 1534 | return NULL; |
| 1535 | if (max >= 256) |
| 1536 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1537 | else |
| 1538 | for (i = 0; i < size; i++) |
| 1539 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
| 1540 | return res; |
| 1541 | } |
| 1542 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1543 | static PyObject* |
| 1544 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1545 | { |
| 1546 | PyObject *res; |
| 1547 | Py_UCS4 max = 0; |
| 1548 | Py_ssize_t i; |
| 1549 | for (i = 0; i < size; i++) |
| 1550 | if (u[i] > max) |
| 1551 | max = u[i]; |
| 1552 | res = PyUnicode_New(size, max); |
| 1553 | if (!res) |
| 1554 | return NULL; |
| 1555 | if (max >= 0x10000) |
| 1556 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1557 | else { |
| 1558 | int kind = PyUnicode_KIND(res); |
| 1559 | void *data = PyUnicode_DATA(res); |
| 1560 | for (i = 0; i < size; i++) |
| 1561 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1562 | } |
| 1563 | return res; |
| 1564 | } |
| 1565 | |
| 1566 | PyObject* |
| 1567 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1568 | { |
| 1569 | switch(kind) { |
| 1570 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1571 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1572 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1573 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1574 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1575 | return _PyUnicode_FromUCS4(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1576 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1577 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1578 | return NULL; |
| 1579 | } |
| 1580 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1581 | PyObject* |
| 1582 | PyUnicode_Copy(PyObject *unicode) |
| 1583 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1584 | Py_ssize_t size; |
| 1585 | PyObject *copy; |
| 1586 | void *data; |
| 1587 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1588 | if (!PyUnicode_Check(unicode)) { |
| 1589 | PyErr_BadInternalCall(); |
| 1590 | return NULL; |
| 1591 | } |
| 1592 | if (PyUnicode_READY(unicode)) |
| 1593 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1594 | |
| 1595 | size = PyUnicode_GET_LENGTH(unicode); |
| 1596 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1597 | if (!copy) |
| 1598 | return NULL; |
| 1599 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1600 | |
| 1601 | data = PyUnicode_DATA(unicode); |
| 1602 | switch (PyUnicode_KIND(unicode)) |
| 1603 | { |
| 1604 | case PyUnicode_1BYTE_KIND: |
| 1605 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1606 | break; |
| 1607 | case PyUnicode_2BYTE_KIND: |
| 1608 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1609 | break; |
| 1610 | case PyUnicode_4BYTE_KIND: |
| 1611 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1612 | break; |
| 1613 | default: |
| 1614 | assert(0); |
| 1615 | break; |
| 1616 | } |
| 1617 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1618 | } |
| 1619 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1620 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1621 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1622 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1623 | |
| 1624 | void* |
| 1625 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1626 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1627 | Py_ssize_t len; |
| 1628 | void *result; |
| 1629 | unsigned int skind; |
| 1630 | |
| 1631 | if (PyUnicode_READY(s)) |
| 1632 | return NULL; |
| 1633 | |
| 1634 | len = PyUnicode_GET_LENGTH(s); |
| 1635 | skind = PyUnicode_KIND(s); |
| 1636 | if (skind >= kind) { |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1637 | PyErr_SetString(PyExc_SystemError, "invalid widening attempt"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1638 | return NULL; |
| 1639 | } |
| 1640 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1641 | case PyUnicode_2BYTE_KIND: |
| 1642 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1643 | if (!result) |
| 1644 | return PyErr_NoMemory(); |
| 1645 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1646 | _PyUnicode_CONVERT_BYTES( |
| 1647 | Py_UCS1, Py_UCS2, |
| 1648 | PyUnicode_1BYTE_DATA(s), |
| 1649 | PyUnicode_1BYTE_DATA(s) + len, |
| 1650 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1651 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1652 | case PyUnicode_4BYTE_KIND: |
| 1653 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1654 | if (!result) |
| 1655 | return PyErr_NoMemory(); |
| 1656 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1657 | _PyUnicode_CONVERT_BYTES( |
| 1658 | Py_UCS2, Py_UCS4, |
| 1659 | PyUnicode_2BYTE_DATA(s), |
| 1660 | PyUnicode_2BYTE_DATA(s) + len, |
| 1661 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1662 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1663 | else { |
| 1664 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1665 | _PyUnicode_CONVERT_BYTES( |
| 1666 | Py_UCS1, Py_UCS4, |
| 1667 | PyUnicode_1BYTE_DATA(s), |
| 1668 | PyUnicode_1BYTE_DATA(s) + len, |
| 1669 | result); |
| 1670 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1671 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1672 | default: |
| 1673 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1674 | } |
Victor Stinner | 0169804 | 2011-10-04 00:04:26 +0200 | [diff] [blame] | 1675 | PyErr_SetString(PyExc_SystemError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1676 | return NULL; |
| 1677 | } |
| 1678 | |
| 1679 | static Py_UCS4* |
| 1680 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1681 | int copy_null) |
| 1682 | { |
| 1683 | int kind; |
| 1684 | void *data; |
| 1685 | Py_ssize_t len, targetlen; |
| 1686 | if (PyUnicode_READY(string) == -1) |
| 1687 | return NULL; |
| 1688 | kind = PyUnicode_KIND(string); |
| 1689 | data = PyUnicode_DATA(string); |
| 1690 | len = PyUnicode_GET_LENGTH(string); |
| 1691 | targetlen = len; |
| 1692 | if (copy_null) |
| 1693 | targetlen++; |
| 1694 | if (!target) { |
| 1695 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1696 | PyErr_NoMemory(); |
| 1697 | return NULL; |
| 1698 | } |
| 1699 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1700 | if (!target) { |
| 1701 | PyErr_NoMemory(); |
| 1702 | return NULL; |
| 1703 | } |
| 1704 | } |
| 1705 | else { |
| 1706 | if (targetsize < targetlen) { |
| 1707 | PyErr_Format(PyExc_SystemError, |
| 1708 | "string is longer than the buffer"); |
| 1709 | if (copy_null && 0 < targetsize) |
| 1710 | target[0] = 0; |
| 1711 | return NULL; |
| 1712 | } |
| 1713 | } |
| 1714 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1715 | Py_ssize_t i; |
| 1716 | for (i = 0; i < len; i++) |
| 1717 | target[i] = PyUnicode_READ(kind, data, i); |
| 1718 | } |
| 1719 | else |
| 1720 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1721 | if (copy_null) |
| 1722 | target[len] = 0; |
| 1723 | return target; |
| 1724 | } |
| 1725 | |
| 1726 | Py_UCS4* |
| 1727 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1728 | int copy_null) |
| 1729 | { |
| 1730 | if (target == NULL || targetsize < 1) { |
| 1731 | PyErr_BadInternalCall(); |
| 1732 | return NULL; |
| 1733 | } |
| 1734 | return as_ucs4(string, target, targetsize, copy_null); |
| 1735 | } |
| 1736 | |
| 1737 | Py_UCS4* |
| 1738 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1739 | { |
| 1740 | return as_ucs4(string, NULL, 0, 1); |
| 1741 | } |
| 1742 | |
| 1743 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1744 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1745 | PyObject * |
| 1746 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1747 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1748 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1749 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1750 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1751 | PyErr_BadInternalCall(); |
| 1752 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1755 | if (size == -1) { |
| 1756 | size = wcslen(w); |
| 1757 | } |
| 1758 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1759 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1762 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1763 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1764 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1765 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1766 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1767 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1768 | *fmt++ = '%'; |
| 1769 | if (width) { |
| 1770 | if (zeropad) |
| 1771 | *fmt++ = '0'; |
| 1772 | fmt += sprintf(fmt, "%d", width); |
| 1773 | } |
| 1774 | if (precision) |
| 1775 | fmt += sprintf(fmt, ".%d", precision); |
| 1776 | if (longflag) |
| 1777 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1778 | else if (longlongflag) { |
| 1779 | /* longlongflag should only ever be nonzero on machines with |
| 1780 | HAVE_LONG_LONG defined */ |
| 1781 | #ifdef HAVE_LONG_LONG |
| 1782 | char *f = PY_FORMAT_LONG_LONG; |
| 1783 | while (*f) |
| 1784 | *fmt++ = *f++; |
| 1785 | #else |
| 1786 | /* we shouldn't ever get here */ |
| 1787 | assert(0); |
| 1788 | *fmt++ = 'l'; |
| 1789 | #endif |
| 1790 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1791 | else if (size_tflag) { |
| 1792 | char *f = PY_FORMAT_SIZE_T; |
| 1793 | while (*f) |
| 1794 | *fmt++ = *f++; |
| 1795 | } |
| 1796 | *fmt++ = c; |
| 1797 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1800 | /* helper for PyUnicode_FromFormatV() */ |
| 1801 | |
| 1802 | static const char* |
| 1803 | parse_format_flags(const char *f, |
| 1804 | int *p_width, int *p_precision, |
| 1805 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 1806 | { |
| 1807 | int width, precision, longflag, longlongflag, size_tflag; |
| 1808 | |
| 1809 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 1810 | f++; |
| 1811 | width = 0; |
| 1812 | while (Py_ISDIGIT((unsigned)*f)) |
| 1813 | width = (width*10) + *f++ - '0'; |
| 1814 | precision = 0; |
| 1815 | if (*f == '.') { |
| 1816 | f++; |
| 1817 | while (Py_ISDIGIT((unsigned)*f)) |
| 1818 | precision = (precision*10) + *f++ - '0'; |
| 1819 | if (*f == '%') { |
| 1820 | /* "%.3%s" => f points to "3" */ |
| 1821 | f--; |
| 1822 | } |
| 1823 | } |
| 1824 | if (*f == '\0') { |
| 1825 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 1826 | f--; |
| 1827 | } |
| 1828 | if (p_width != NULL) |
| 1829 | *p_width = width; |
| 1830 | if (p_precision != NULL) |
| 1831 | *p_precision = precision; |
| 1832 | |
| 1833 | /* Handle %ld, %lu, %lld and %llu. */ |
| 1834 | longflag = 0; |
| 1835 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 1836 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1837 | |
| 1838 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1839 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1840 | longflag = 1; |
| 1841 | ++f; |
| 1842 | } |
| 1843 | #ifdef HAVE_LONG_LONG |
| 1844 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1845 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1846 | longlongflag = 1; |
| 1847 | f += 2; |
| 1848 | } |
| 1849 | #endif |
| 1850 | } |
| 1851 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1852 | 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] | 1853 | size_tflag = 1; |
| 1854 | ++f; |
| 1855 | } |
| 1856 | if (p_longflag != NULL) |
| 1857 | *p_longflag = longflag; |
| 1858 | if (p_longlongflag != NULL) |
| 1859 | *p_longlongflag = longlongflag; |
| 1860 | if (p_size_tflag != NULL) |
| 1861 | *p_size_tflag = size_tflag; |
| 1862 | return f; |
| 1863 | } |
| 1864 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1865 | /* maximum number of characters required for output of %ld. 21 characters |
| 1866 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 1867 | #define MAX_LONG_CHARS 21 |
| 1868 | /* maximum number of characters required for output of %lld. |
| 1869 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 1870 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 1871 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 1872 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1873 | PyObject * |
| 1874 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 1875 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1876 | va_list count; |
| 1877 | Py_ssize_t callcount = 0; |
| 1878 | PyObject **callresults = NULL; |
| 1879 | PyObject **callresult = NULL; |
| 1880 | Py_ssize_t n = 0; |
| 1881 | int width = 0; |
| 1882 | int precision = 0; |
| 1883 | int zeropad; |
| 1884 | const char* f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1885 | PyUnicodeObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1886 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1887 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1888 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 1889 | Py_UCS4 argmaxchar; |
| 1890 | Py_ssize_t numbersize = 0; |
| 1891 | char *numberresults = NULL; |
| 1892 | char *numberresult = NULL; |
| 1893 | Py_ssize_t i; |
| 1894 | int kind; |
| 1895 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1896 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1897 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1898 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 1899 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 1900 | * 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] | 1901 | * result in an array) |
| 1902 | * also esimate a upper bound for all the number formats in the string, |
| 1903 | * numbers will be formated in step 3 and be keept in a '\0'-separated |
| 1904 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1905 | for (f = format; *f; f++) { |
| 1906 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1907 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1908 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 1909 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 1910 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 1911 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1912 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1913 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1914 | #ifdef HAVE_LONG_LONG |
| 1915 | if (longlongflag) { |
| 1916 | if (width < MAX_LONG_LONG_CHARS) |
| 1917 | width = MAX_LONG_LONG_CHARS; |
| 1918 | } |
| 1919 | else |
| 1920 | #endif |
| 1921 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 1922 | including sign. Decimal takes the most space. This |
| 1923 | isn't enough for octal. If a width is specified we |
| 1924 | need more (which we allocate later). */ |
| 1925 | if (width < MAX_LONG_CHARS) |
| 1926 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1927 | |
| 1928 | /* account for the size + '\0' to separate numbers |
| 1929 | inside of the numberresults buffer */ |
| 1930 | numbersize += (width + 1); |
| 1931 | } |
| 1932 | } |
| 1933 | else if ((unsigned char)*f > 127) { |
| 1934 | PyErr_Format(PyExc_ValueError, |
| 1935 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 1936 | "string, got a non-ASCII byte: 0x%02x", |
| 1937 | (unsigned char)*f); |
| 1938 | return NULL; |
| 1939 | } |
| 1940 | } |
| 1941 | /* step 2: allocate memory for the results of |
| 1942 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 1943 | if (callcount) { |
| 1944 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 1945 | if (!callresults) { |
| 1946 | PyErr_NoMemory(); |
| 1947 | return NULL; |
| 1948 | } |
| 1949 | callresult = callresults; |
| 1950 | } |
| 1951 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 1952 | if (numbersize) { |
| 1953 | numberresults = PyObject_Malloc(numbersize); |
| 1954 | if (!numberresults) { |
| 1955 | PyErr_NoMemory(); |
| 1956 | goto fail; |
| 1957 | } |
| 1958 | numberresult = numberresults; |
| 1959 | } |
| 1960 | |
| 1961 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 1962 | for (f = format; *f; f++) { |
| 1963 | if (*f == '%') { |
| 1964 | const char* p; |
| 1965 | int longflag; |
| 1966 | int longlongflag; |
| 1967 | int size_tflag; |
| 1968 | int numprinted; |
| 1969 | |
| 1970 | p = f; |
| 1971 | zeropad = (f[1] == '0'); |
| 1972 | f = parse_format_flags(f, &width, &precision, |
| 1973 | &longflag, &longlongflag, &size_tflag); |
| 1974 | switch (*f) { |
| 1975 | case 'c': |
| 1976 | { |
| 1977 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1978 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1979 | n++; |
| 1980 | break; |
| 1981 | } |
| 1982 | case '%': |
| 1983 | n++; |
| 1984 | break; |
| 1985 | case 'i': |
| 1986 | case 'd': |
| 1987 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1988 | width, precision, *f); |
| 1989 | if (longflag) |
| 1990 | numprinted = sprintf(numberresult, fmt, |
| 1991 | va_arg(count, long)); |
| 1992 | #ifdef HAVE_LONG_LONG |
| 1993 | else if (longlongflag) |
| 1994 | numprinted = sprintf(numberresult, fmt, |
| 1995 | va_arg(count, PY_LONG_LONG)); |
| 1996 | #endif |
| 1997 | else if (size_tflag) |
| 1998 | numprinted = sprintf(numberresult, fmt, |
| 1999 | va_arg(count, Py_ssize_t)); |
| 2000 | else |
| 2001 | numprinted = sprintf(numberresult, fmt, |
| 2002 | va_arg(count, int)); |
| 2003 | n += numprinted; |
| 2004 | /* advance by +1 to skip over the '\0' */ |
| 2005 | numberresult += (numprinted + 1); |
| 2006 | assert(*(numberresult - 1) == '\0'); |
| 2007 | assert(*(numberresult - 2) != '\0'); |
| 2008 | assert(numprinted >= 0); |
| 2009 | assert(numberresult <= numberresults + numbersize); |
| 2010 | break; |
| 2011 | case 'u': |
| 2012 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 2013 | width, precision, 'u'); |
| 2014 | if (longflag) |
| 2015 | numprinted = sprintf(numberresult, fmt, |
| 2016 | va_arg(count, unsigned long)); |
| 2017 | #ifdef HAVE_LONG_LONG |
| 2018 | else if (longlongflag) |
| 2019 | numprinted = sprintf(numberresult, fmt, |
| 2020 | va_arg(count, unsigned PY_LONG_LONG)); |
| 2021 | #endif |
| 2022 | else if (size_tflag) |
| 2023 | numprinted = sprintf(numberresult, fmt, |
| 2024 | va_arg(count, size_t)); |
| 2025 | else |
| 2026 | numprinted = sprintf(numberresult, fmt, |
| 2027 | va_arg(count, unsigned int)); |
| 2028 | n += numprinted; |
| 2029 | numberresult += (numprinted + 1); |
| 2030 | assert(*(numberresult - 1) == '\0'); |
| 2031 | assert(*(numberresult - 2) != '\0'); |
| 2032 | assert(numprinted >= 0); |
| 2033 | assert(numberresult <= numberresults + numbersize); |
| 2034 | break; |
| 2035 | case 'x': |
| 2036 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 2037 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 2038 | n += numprinted; |
| 2039 | numberresult += (numprinted + 1); |
| 2040 | assert(*(numberresult - 1) == '\0'); |
| 2041 | assert(*(numberresult - 2) != '\0'); |
| 2042 | assert(numprinted >= 0); |
| 2043 | assert(numberresult <= numberresults + numbersize); |
| 2044 | break; |
| 2045 | case 'p': |
| 2046 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2047 | /* %p is ill-defined: ensure leading 0x. */ |
| 2048 | if (numberresult[1] == 'X') |
| 2049 | numberresult[1] = 'x'; |
| 2050 | else if (numberresult[1] != 'x') { |
| 2051 | memmove(numberresult + 2, numberresult, |
| 2052 | strlen(numberresult) + 1); |
| 2053 | numberresult[0] = '0'; |
| 2054 | numberresult[1] = 'x'; |
| 2055 | numprinted += 2; |
| 2056 | } |
| 2057 | n += numprinted; |
| 2058 | numberresult += (numprinted + 1); |
| 2059 | assert(*(numberresult - 1) == '\0'); |
| 2060 | assert(*(numberresult - 2) != '\0'); |
| 2061 | assert(numprinted >= 0); |
| 2062 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2063 | break; |
| 2064 | case 's': |
| 2065 | { |
| 2066 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2067 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2068 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2069 | if (!str) |
| 2070 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2071 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2072 | unicode objects, there is no need to call ready on them */ |
| 2073 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2074 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2075 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2076 | /* Remember the str and switch to the next slot */ |
| 2077 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2078 | break; |
| 2079 | } |
| 2080 | case 'U': |
| 2081 | { |
| 2082 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2083 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2084 | if (PyUnicode_READY(obj) == -1) |
| 2085 | goto fail; |
| 2086 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2087 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2088 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2089 | break; |
| 2090 | } |
| 2091 | case 'V': |
| 2092 | { |
| 2093 | PyObject *obj = va_arg(count, PyObject *); |
| 2094 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2095 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2096 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2097 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2098 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2099 | if (PyUnicode_READY(obj) == -1) |
| 2100 | goto fail; |
| 2101 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2102 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2103 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2104 | *callresult++ = NULL; |
| 2105 | } |
| 2106 | else { |
| 2107 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2108 | if (!str_obj) |
| 2109 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2110 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2111 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2112 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2113 | *callresult++ = str_obj; |
| 2114 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2115 | break; |
| 2116 | } |
| 2117 | case 'S': |
| 2118 | { |
| 2119 | PyObject *obj = va_arg(count, PyObject *); |
| 2120 | PyObject *str; |
| 2121 | assert(obj); |
| 2122 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2123 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2124 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2125 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2126 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2127 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2128 | /* Remember the str and switch to the next slot */ |
| 2129 | *callresult++ = str; |
| 2130 | break; |
| 2131 | } |
| 2132 | case 'R': |
| 2133 | { |
| 2134 | PyObject *obj = va_arg(count, PyObject *); |
| 2135 | PyObject *repr; |
| 2136 | assert(obj); |
| 2137 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2138 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2139 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2140 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2141 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2142 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2143 | /* Remember the repr and switch to the next slot */ |
| 2144 | *callresult++ = repr; |
| 2145 | break; |
| 2146 | } |
| 2147 | case 'A': |
| 2148 | { |
| 2149 | PyObject *obj = va_arg(count, PyObject *); |
| 2150 | PyObject *ascii; |
| 2151 | assert(obj); |
| 2152 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2153 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2154 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2155 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2156 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2157 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2158 | /* Remember the repr and switch to the next slot */ |
| 2159 | *callresult++ = ascii; |
| 2160 | break; |
| 2161 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2162 | default: |
| 2163 | /* if we stumble upon an unknown |
| 2164 | formatting code, copy the rest of |
| 2165 | the format string to the output |
| 2166 | string. (we cannot just skip the |
| 2167 | code, since there's no way to know |
| 2168 | what's in the argument list) */ |
| 2169 | n += strlen(p); |
| 2170 | goto expand; |
| 2171 | } |
| 2172 | } else |
| 2173 | n++; |
| 2174 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2175 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2176 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2177 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2178 | we don't have to resize the string. |
| 2179 | There can be no errors beyond this point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2180 | string = (PyUnicodeObject *)PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2181 | if (!string) |
| 2182 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2183 | kind = PyUnicode_KIND(string); |
| 2184 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2185 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2186 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2187 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2188 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2189 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2190 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2191 | |
| 2192 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2193 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2194 | /* checking for == because the last argument could be a empty |
| 2195 | string, which causes i to point to end, the assert at the end of |
| 2196 | the loop */ |
| 2197 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2198 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2199 | switch (*f) { |
| 2200 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2201 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2202 | const int ordinal = va_arg(vargs, int); |
| 2203 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2204 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2205 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2206 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2207 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2208 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2209 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2210 | case 'p': |
| 2211 | /* unused, since we already have the result */ |
| 2212 | if (*f == 'p') |
| 2213 | (void) va_arg(vargs, void *); |
| 2214 | else |
| 2215 | (void) va_arg(vargs, int); |
| 2216 | /* extract the result from numberresults and append. */ |
| 2217 | for (; *numberresult; ++i, ++numberresult) |
| 2218 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2219 | /* skip over the separating '\0' */ |
| 2220 | assert(*numberresult == '\0'); |
| 2221 | numberresult++; |
| 2222 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2223 | break; |
| 2224 | case 's': |
| 2225 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2226 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2227 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2228 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2229 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2230 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2231 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2232 | *callresult, 0, |
| 2233 | size) < 0) |
| 2234 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2235 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2236 | /* We're done with the unicode()/repr() => forget it */ |
| 2237 | Py_DECREF(*callresult); |
| 2238 | /* switch to next unicode()/repr() result */ |
| 2239 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2240 | break; |
| 2241 | } |
| 2242 | case 'U': |
| 2243 | { |
| 2244 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2245 | Py_ssize_t size; |
| 2246 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2247 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2248 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2249 | obj, 0, |
| 2250 | size) < 0) |
| 2251 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2252 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2253 | break; |
| 2254 | } |
| 2255 | case 'V': |
| 2256 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2257 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2258 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2259 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2260 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2261 | size = PyUnicode_GET_LENGTH(obj); |
| 2262 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2263 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2264 | obj, 0, |
| 2265 | size) < 0) |
| 2266 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2267 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2268 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2269 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2270 | assert(PyUnicode_KIND(*callresult) <= |
| 2271 | PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2272 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2273 | *callresult, |
| 2274 | 0, size) < 0) |
| 2275 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2276 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2277 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2278 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2279 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2280 | break; |
| 2281 | } |
| 2282 | case 'S': |
| 2283 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2284 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2285 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2286 | /* unused, since we already have the result */ |
| 2287 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2288 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2289 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2290 | *callresult, 0, |
| 2291 | PyUnicode_GET_LENGTH(*callresult)) < 0) |
| 2292 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2293 | i += PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2294 | /* We're done with the unicode()/repr() => forget it */ |
| 2295 | Py_DECREF(*callresult); |
| 2296 | /* switch to next unicode()/repr() result */ |
| 2297 | ++callresult; |
| 2298 | break; |
| 2299 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2300 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2301 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2302 | break; |
| 2303 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2304 | for (; *p; ++p, ++i) |
| 2305 | PyUnicode_WRITE(kind, data, i, *p); |
| 2306 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2307 | goto end; |
| 2308 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2309 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2310 | else { |
| 2311 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2312 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2313 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2314 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2315 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2316 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2317 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2318 | if (callresults) |
| 2319 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2320 | if (numberresults) |
| 2321 | PyObject_Free(numberresults); |
| 2322 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2323 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2324 | if (callresults) { |
| 2325 | PyObject **callresult2 = callresults; |
| 2326 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2327 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2328 | ++callresult2; |
| 2329 | } |
| 2330 | PyObject_Free(callresults); |
| 2331 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2332 | if (numberresults) |
| 2333 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2334 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2337 | PyObject * |
| 2338 | PyUnicode_FromFormat(const char *format, ...) |
| 2339 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2340 | PyObject* ret; |
| 2341 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2342 | |
| 2343 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2344 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2345 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2346 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2347 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2348 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2349 | va_end(vargs); |
| 2350 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2353 | #ifdef HAVE_WCHAR_H |
| 2354 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2355 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2356 | convert a Unicode object to a wide character string. |
| 2357 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2358 | - 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] | 2359 | character) required to convert the unicode object. Ignore size argument. |
| 2360 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2361 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2362 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2363 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2364 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2365 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 2366 | wchar_t *w, |
| 2367 | Py_ssize_t size) |
| 2368 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2369 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2370 | const wchar_t *wstr; |
| 2371 | |
| 2372 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 2373 | if (wstr == NULL) |
| 2374 | return -1; |
| 2375 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2376 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2377 | if (size > res) |
| 2378 | size = res + 1; |
| 2379 | else |
| 2380 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2381 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2382 | return res; |
| 2383 | } |
| 2384 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2385 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2389 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2390 | wchar_t *w, |
| 2391 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2392 | { |
| 2393 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2394 | PyErr_BadInternalCall(); |
| 2395 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2396 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2397 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2400 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2401 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2402 | Py_ssize_t *size) |
| 2403 | { |
| 2404 | wchar_t* buffer; |
| 2405 | Py_ssize_t buflen; |
| 2406 | |
| 2407 | if (unicode == NULL) { |
| 2408 | PyErr_BadInternalCall(); |
| 2409 | return NULL; |
| 2410 | } |
| 2411 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2412 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2413 | if (buflen == -1) |
| 2414 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2415 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2416 | PyErr_NoMemory(); |
| 2417 | return NULL; |
| 2418 | } |
| 2419 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2420 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2421 | if (buffer == NULL) { |
| 2422 | PyErr_NoMemory(); |
| 2423 | return NULL; |
| 2424 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2425 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2426 | if (buflen == -1) |
| 2427 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2428 | if (size != NULL) |
| 2429 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2430 | return buffer; |
| 2431 | } |
| 2432 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2433 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2434 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2435 | PyObject * |
| 2436 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2437 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2438 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2439 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2440 | PyErr_SetString(PyExc_ValueError, |
| 2441 | "chr() arg not in range(0x110000)"); |
| 2442 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2443 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2444 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2445 | if (ordinal < 256) |
| 2446 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2447 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2448 | v = PyUnicode_New(1, ordinal); |
| 2449 | if (v == NULL) |
| 2450 | return NULL; |
| 2451 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
| 2452 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2455 | PyObject * |
| 2456 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2457 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2458 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2459 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2460 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2461 | if (PyUnicode_READY(obj)) |
| 2462 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2463 | Py_INCREF(obj); |
| 2464 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2465 | } |
| 2466 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2467 | /* For a Unicode subtype that's not a Unicode object, |
| 2468 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2469 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2470 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2471 | PyErr_Format(PyExc_TypeError, |
| 2472 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2473 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2474 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2477 | PyObject * |
| 2478 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2479 | const char *encoding, |
| 2480 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2481 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2482 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2483 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2484 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2485 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2486 | PyErr_BadInternalCall(); |
| 2487 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2488 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2489 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2490 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2491 | if (PyBytes_Check(obj)) { |
| 2492 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2493 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2494 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2495 | } |
| 2496 | else { |
| 2497 | v = PyUnicode_Decode( |
| 2498 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2499 | encoding, errors); |
| 2500 | } |
| 2501 | return v; |
| 2502 | } |
| 2503 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2504 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2505 | PyErr_SetString(PyExc_TypeError, |
| 2506 | "decoding str is not supported"); |
| 2507 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2508 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2509 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2510 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2511 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2512 | PyErr_Format(PyExc_TypeError, |
| 2513 | "coercing to str: need bytes, bytearray " |
| 2514 | "or buffer-like object, %.80s found", |
| 2515 | Py_TYPE(obj)->tp_name); |
| 2516 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2517 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2518 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2519 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2520 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2521 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2522 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2523 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2524 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2525 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2526 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2527 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2530 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2531 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2532 | 1 on success. */ |
| 2533 | static int |
| 2534 | normalize_encoding(const char *encoding, |
| 2535 | char *lower, |
| 2536 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2537 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2538 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2539 | char *l; |
| 2540 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2541 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2542 | e = encoding; |
| 2543 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2544 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2545 | while (*e) { |
| 2546 | if (l == l_end) |
| 2547 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2548 | if (Py_ISUPPER(*e)) { |
| 2549 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2550 | } |
| 2551 | else if (*e == '_') { |
| 2552 | *l++ = '-'; |
| 2553 | e++; |
| 2554 | } |
| 2555 | else { |
| 2556 | *l++ = *e++; |
| 2557 | } |
| 2558 | } |
| 2559 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2560 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2563 | PyObject * |
| 2564 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2565 | Py_ssize_t size, |
| 2566 | const char *encoding, |
| 2567 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2568 | { |
| 2569 | PyObject *buffer = NULL, *unicode; |
| 2570 | Py_buffer info; |
| 2571 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2572 | |
| 2573 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2574 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2575 | |
| 2576 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2577 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2578 | if ((strcmp(lower, "utf-8") == 0) || |
| 2579 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2580 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2581 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2582 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2583 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2584 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2585 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2586 | else if (strcmp(lower, "mbcs") == 0) |
| 2587 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2588 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2589 | else if (strcmp(lower, "ascii") == 0) |
| 2590 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2591 | else if (strcmp(lower, "utf-16") == 0) |
| 2592 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2593 | else if (strcmp(lower, "utf-32") == 0) |
| 2594 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2595 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2596 | |
| 2597 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2598 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2599 | 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] | 2600 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2601 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2602 | if (buffer == NULL) |
| 2603 | goto onError; |
| 2604 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2605 | if (unicode == NULL) |
| 2606 | goto onError; |
| 2607 | if (!PyUnicode_Check(unicode)) { |
| 2608 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2609 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2610 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2611 | Py_DECREF(unicode); |
| 2612 | goto onError; |
| 2613 | } |
| 2614 | Py_DECREF(buffer); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2615 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2616 | Py_DECREF(unicode); |
| 2617 | return NULL; |
| 2618 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2619 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2620 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2621 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2622 | Py_XDECREF(buffer); |
| 2623 | return NULL; |
| 2624 | } |
| 2625 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2626 | PyObject * |
| 2627 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2628 | const char *encoding, |
| 2629 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2630 | { |
| 2631 | PyObject *v; |
| 2632 | |
| 2633 | if (!PyUnicode_Check(unicode)) { |
| 2634 | PyErr_BadArgument(); |
| 2635 | goto onError; |
| 2636 | } |
| 2637 | |
| 2638 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2639 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2640 | |
| 2641 | /* Decode via the codec registry */ |
| 2642 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2643 | if (v == NULL) |
| 2644 | goto onError; |
| 2645 | return v; |
| 2646 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2647 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2648 | return NULL; |
| 2649 | } |
| 2650 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2651 | PyObject * |
| 2652 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2653 | const char *encoding, |
| 2654 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2655 | { |
| 2656 | PyObject *v; |
| 2657 | |
| 2658 | if (!PyUnicode_Check(unicode)) { |
| 2659 | PyErr_BadArgument(); |
| 2660 | goto onError; |
| 2661 | } |
| 2662 | |
| 2663 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2664 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2665 | |
| 2666 | /* Decode via the codec registry */ |
| 2667 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2668 | if (v == NULL) |
| 2669 | goto onError; |
| 2670 | if (!PyUnicode_Check(v)) { |
| 2671 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2672 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2673 | Py_TYPE(v)->tp_name); |
| 2674 | Py_DECREF(v); |
| 2675 | goto onError; |
| 2676 | } |
| 2677 | return v; |
| 2678 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2679 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2680 | return NULL; |
| 2681 | } |
| 2682 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2683 | PyObject * |
| 2684 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2685 | Py_ssize_t size, |
| 2686 | const char *encoding, |
| 2687 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2688 | { |
| 2689 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2690 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2691 | unicode = PyUnicode_FromUnicode(s, size); |
| 2692 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2693 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2694 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2695 | Py_DECREF(unicode); |
| 2696 | return v; |
| 2697 | } |
| 2698 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2699 | PyObject * |
| 2700 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2701 | const char *encoding, |
| 2702 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2703 | { |
| 2704 | PyObject *v; |
| 2705 | |
| 2706 | if (!PyUnicode_Check(unicode)) { |
| 2707 | PyErr_BadArgument(); |
| 2708 | goto onError; |
| 2709 | } |
| 2710 | |
| 2711 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2712 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2713 | |
| 2714 | /* Encode via the codec registry */ |
| 2715 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2716 | if (v == NULL) |
| 2717 | goto onError; |
| 2718 | return v; |
| 2719 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2720 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2721 | return NULL; |
| 2722 | } |
| 2723 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2724 | PyObject * |
| 2725 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2726 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2727 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2728 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2729 | PyUnicode_GET_SIZE(unicode), |
| 2730 | NULL); |
| 2731 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2732 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2733 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2734 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2735 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2736 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2737 | the Python codec requires to encode at least its own filename. Use the C |
| 2738 | version of the locale codec until the codec registry is initialized and |
| 2739 | the Python codec is loaded. |
| 2740 | |
| 2741 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2742 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2743 | subinterpreters. */ |
| 2744 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2745 | return PyUnicode_AsEncodedString(unicode, |
| 2746 | Py_FileSystemDefaultEncoding, |
| 2747 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2748 | } |
| 2749 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2750 | /* locale encoding with surrogateescape */ |
| 2751 | wchar_t *wchar; |
| 2752 | char *bytes; |
| 2753 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2754 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2755 | |
| 2756 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2757 | if (wchar == NULL) |
| 2758 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2759 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2760 | if (bytes == NULL) { |
| 2761 | if (error_pos != (size_t)-1) { |
| 2762 | char *errmsg = strerror(errno); |
| 2763 | PyObject *exc = NULL; |
| 2764 | if (errmsg == NULL) |
| 2765 | errmsg = "Py_wchar2char() failed"; |
| 2766 | raise_encode_exception(&exc, |
| 2767 | "filesystemencoding", |
| 2768 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2769 | error_pos, error_pos+1, |
| 2770 | errmsg); |
| 2771 | Py_XDECREF(exc); |
| 2772 | } |
| 2773 | else |
| 2774 | PyErr_NoMemory(); |
| 2775 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2776 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2777 | } |
| 2778 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2779 | |
| 2780 | bytes_obj = PyBytes_FromString(bytes); |
| 2781 | PyMem_Free(bytes); |
| 2782 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2783 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2784 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2787 | PyObject * |
| 2788 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2789 | const char *encoding, |
| 2790 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2791 | { |
| 2792 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2793 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2794 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2795 | if (!PyUnicode_Check(unicode)) { |
| 2796 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2797 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2798 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2799 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2800 | if (encoding == NULL) { |
| 2801 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2802 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2803 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2804 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2805 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2806 | |
| 2807 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2808 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2809 | if ((strcmp(lower, "utf-8") == 0) || |
| 2810 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2811 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2812 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2813 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2814 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2815 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2816 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2817 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2818 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2819 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2820 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2821 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2822 | else if (strcmp(lower, "mbcs") == 0) |
| 2823 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2824 | PyUnicode_GET_SIZE(unicode), |
| 2825 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2826 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2827 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2828 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2829 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2830 | |
| 2831 | /* Encode via the codec registry */ |
| 2832 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2833 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2834 | return NULL; |
| 2835 | |
| 2836 | /* The normal path */ |
| 2837 | if (PyBytes_Check(v)) |
| 2838 | return v; |
| 2839 | |
| 2840 | /* 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] | 2841 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2842 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2843 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2844 | |
| 2845 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 2846 | "encoder %s returned bytearray instead of bytes", |
| 2847 | encoding); |
| 2848 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2849 | Py_DECREF(v); |
| 2850 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2851 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2852 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2853 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 2854 | Py_DECREF(v); |
| 2855 | return b; |
| 2856 | } |
| 2857 | |
| 2858 | PyErr_Format(PyExc_TypeError, |
| 2859 | "encoder did not return a bytes object (type=%.400s)", |
| 2860 | Py_TYPE(v)->tp_name); |
| 2861 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2862 | return NULL; |
| 2863 | } |
| 2864 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2865 | PyObject * |
| 2866 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2867 | const char *encoding, |
| 2868 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2869 | { |
| 2870 | PyObject *v; |
| 2871 | |
| 2872 | if (!PyUnicode_Check(unicode)) { |
| 2873 | PyErr_BadArgument(); |
| 2874 | goto onError; |
| 2875 | } |
| 2876 | |
| 2877 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2878 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2879 | |
| 2880 | /* Encode via the codec registry */ |
| 2881 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2882 | if (v == NULL) |
| 2883 | goto onError; |
| 2884 | if (!PyUnicode_Check(v)) { |
| 2885 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2886 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2887 | Py_TYPE(v)->tp_name); |
| 2888 | Py_DECREF(v); |
| 2889 | goto onError; |
| 2890 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2891 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2892 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2893 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2894 | return NULL; |
| 2895 | } |
| 2896 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2897 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2898 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2899 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2900 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 2901 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2902 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2903 | PyObject* |
| 2904 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 2905 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2906 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2907 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 2908 | #elif defined(__APPLE__) |
| 2909 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 2910 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2911 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2912 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2913 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2914 | the Python codec requires to encode at least its own filename. Use the C |
| 2915 | version of the locale codec until the codec registry is initialized and |
| 2916 | the Python codec is loaded. |
| 2917 | |
| 2918 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2919 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2920 | subinterpreters. */ |
| 2921 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2922 | return PyUnicode_Decode(s, size, |
| 2923 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 2924 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2925 | } |
| 2926 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2927 | /* locale encoding with surrogateescape */ |
| 2928 | wchar_t *wchar; |
| 2929 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2930 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2931 | |
| 2932 | if (s[size] != '\0' || size != strlen(s)) { |
| 2933 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2934 | return NULL; |
| 2935 | } |
| 2936 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2937 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2938 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 2939 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2940 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2941 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2942 | PyMem_Free(wchar); |
| 2943 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2944 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2945 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2948 | |
| 2949 | int |
| 2950 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 2951 | { |
| 2952 | PyObject *output = NULL; |
| 2953 | Py_ssize_t size; |
| 2954 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2955 | if (arg == NULL) { |
| 2956 | Py_DECREF(*(PyObject**)addr); |
| 2957 | return 1; |
| 2958 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2959 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2960 | output = arg; |
| 2961 | Py_INCREF(output); |
| 2962 | } |
| 2963 | else { |
| 2964 | arg = PyUnicode_FromObject(arg); |
| 2965 | if (!arg) |
| 2966 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2967 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2968 | Py_DECREF(arg); |
| 2969 | if (!output) |
| 2970 | return 0; |
| 2971 | if (!PyBytes_Check(output)) { |
| 2972 | Py_DECREF(output); |
| 2973 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 2974 | return 0; |
| 2975 | } |
| 2976 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 2977 | size = PyBytes_GET_SIZE(output); |
| 2978 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2979 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 2980 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2981 | Py_DECREF(output); |
| 2982 | return 0; |
| 2983 | } |
| 2984 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2985 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2989 | int |
| 2990 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 2991 | { |
| 2992 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2993 | if (arg == NULL) { |
| 2994 | Py_DECREF(*(PyObject**)addr); |
| 2995 | return 1; |
| 2996 | } |
| 2997 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2998 | if (PyUnicode_READY(arg)) |
| 2999 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3000 | output = arg; |
| 3001 | Py_INCREF(output); |
| 3002 | } |
| 3003 | else { |
| 3004 | arg = PyBytes_FromObject(arg); |
| 3005 | if (!arg) |
| 3006 | return 0; |
| 3007 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 3008 | PyBytes_GET_SIZE(arg)); |
| 3009 | Py_DECREF(arg); |
| 3010 | if (!output) |
| 3011 | return 0; |
| 3012 | if (!PyUnicode_Check(output)) { |
| 3013 | Py_DECREF(output); |
| 3014 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 3015 | return 0; |
| 3016 | } |
| 3017 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3018 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 3019 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 3020 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 3021 | Py_DECREF(output); |
| 3022 | return 0; |
| 3023 | } |
| 3024 | *(PyObject**)addr = output; |
| 3025 | return Py_CLEANUP_SUPPORTED; |
| 3026 | } |
| 3027 | |
| 3028 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3029 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3030 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3031 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 3032 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3033 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 3034 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 3035 | if (!PyUnicode_Check(unicode)) { |
| 3036 | PyErr_BadArgument(); |
| 3037 | return NULL; |
| 3038 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3039 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3040 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3041 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3042 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3043 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3044 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3045 | if (bytes == NULL) |
| 3046 | return NULL; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3047 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3048 | if (_PyUnicode_UTF8(u) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3049 | Py_DECREF(bytes); |
| 3050 | return NULL; |
| 3051 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3052 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); |
| 3053 | 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] | 3054 | Py_DECREF(bytes); |
| 3055 | } |
| 3056 | |
| 3057 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3058 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3059 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
| 3062 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3063 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3064 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3065 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3066 | } |
| 3067 | |
| 3068 | #ifdef Py_DEBUG |
| 3069 | int unicode_as_unicode_calls = 0; |
| 3070 | #endif |
| 3071 | |
| 3072 | |
| 3073 | Py_UNICODE * |
| 3074 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3075 | { |
| 3076 | PyUnicodeObject *u; |
| 3077 | const unsigned char *one_byte; |
| 3078 | #if SIZEOF_WCHAR_T == 4 |
| 3079 | const Py_UCS2 *two_bytes; |
| 3080 | #else |
| 3081 | const Py_UCS4 *four_bytes; |
| 3082 | const Py_UCS4 *ucs4_end; |
| 3083 | Py_ssize_t num_surrogates; |
| 3084 | #endif |
| 3085 | wchar_t *w; |
| 3086 | wchar_t *wchar_end; |
| 3087 | |
| 3088 | if (!PyUnicode_Check(unicode)) { |
| 3089 | PyErr_BadArgument(); |
| 3090 | return NULL; |
| 3091 | } |
| 3092 | u = (PyUnicodeObject*)unicode; |
| 3093 | if (_PyUnicode_WSTR(u) == NULL) { |
| 3094 | /* Non-ASCII compact unicode object */ |
| 3095 | assert(_PyUnicode_KIND(u) != 0); |
| 3096 | assert(PyUnicode_IS_READY(u)); |
| 3097 | |
| 3098 | #ifdef Py_DEBUG |
| 3099 | ++unicode_as_unicode_calls; |
| 3100 | #endif |
| 3101 | |
| 3102 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 3103 | #if SIZEOF_WCHAR_T == 2 |
| 3104 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3105 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 3106 | num_surrogates = 0; |
| 3107 | |
| 3108 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3109 | if (*four_bytes > 0xFFFF) |
| 3110 | ++num_surrogates; |
| 3111 | } |
| 3112 | |
| 3113 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 3114 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 3115 | if (!_PyUnicode_WSTR(u)) { |
| 3116 | PyErr_NoMemory(); |
| 3117 | return NULL; |
| 3118 | } |
| 3119 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 3120 | |
| 3121 | w = _PyUnicode_WSTR(u); |
| 3122 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 3123 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3124 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3125 | if (*four_bytes > 0xFFFF) { |
| 3126 | /* encode surrogate pair in this case */ |
| 3127 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3128 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3129 | } |
| 3130 | else |
| 3131 | *w = *four_bytes; |
| 3132 | |
| 3133 | if (w > wchar_end) { |
| 3134 | assert(0 && "Miscalculated string end"); |
| 3135 | } |
| 3136 | } |
| 3137 | *w = 0; |
| 3138 | #else |
| 3139 | /* sizeof(wchar_t) == 4 */ |
| 3140 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3141 | "should share memory already."); |
| 3142 | return NULL; |
| 3143 | #endif |
| 3144 | } |
| 3145 | else { |
| 3146 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3147 | (_PyUnicode_LENGTH(u) + 1)); |
| 3148 | if (!_PyUnicode_WSTR(u)) { |
| 3149 | PyErr_NoMemory(); |
| 3150 | return NULL; |
| 3151 | } |
| 3152 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 3153 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 3154 | w = _PyUnicode_WSTR(u); |
| 3155 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 3156 | |
| 3157 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 3158 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 3159 | for (; w < wchar_end; ++one_byte, ++w) |
| 3160 | *w = *one_byte; |
| 3161 | /* null-terminate the wstr */ |
| 3162 | *w = 0; |
| 3163 | } |
| 3164 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 3165 | #if SIZEOF_WCHAR_T == 4 |
| 3166 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 3167 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3168 | *w = *two_bytes; |
| 3169 | /* null-terminate the wstr */ |
| 3170 | *w = 0; |
| 3171 | #else |
| 3172 | /* sizeof(wchar_t) == 2 */ |
| 3173 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 3174 | _PyUnicode_WSTR(u) = NULL; |
| 3175 | Py_FatalError("Impossible unicode object state, wstr " |
| 3176 | "and str should share memory already."); |
| 3177 | return NULL; |
| 3178 | #endif |
| 3179 | } |
| 3180 | else { |
| 3181 | assert(0 && "This should never happen."); |
| 3182 | } |
| 3183 | } |
| 3184 | } |
| 3185 | if (size != NULL) |
| 3186 | *size = PyUnicode_WSTR_LENGTH(u); |
| 3187 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3190 | Py_UNICODE * |
| 3191 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3192 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3193 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3196 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3197 | Py_ssize_t |
| 3198 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3199 | { |
| 3200 | if (!PyUnicode_Check(unicode)) { |
| 3201 | PyErr_BadArgument(); |
| 3202 | goto onError; |
| 3203 | } |
| 3204 | return PyUnicode_GET_SIZE(unicode); |
| 3205 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3206 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3207 | return -1; |
| 3208 | } |
| 3209 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3210 | Py_ssize_t |
| 3211 | PyUnicode_GetLength(PyObject *unicode) |
| 3212 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3213 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3214 | PyErr_BadArgument(); |
| 3215 | return -1; |
| 3216 | } |
| 3217 | |
| 3218 | return PyUnicode_GET_LENGTH(unicode); |
| 3219 | } |
| 3220 | |
| 3221 | Py_UCS4 |
| 3222 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3223 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3224 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3225 | PyErr_BadArgument(); |
| 3226 | return (Py_UCS4)-1; |
| 3227 | } |
| 3228 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3229 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3230 | return (Py_UCS4)-1; |
| 3231 | } |
| 3232 | return PyUnicode_READ_CHAR(unicode, index); |
| 3233 | } |
| 3234 | |
| 3235 | int |
| 3236 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3237 | { |
| 3238 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3239 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3240 | return -1; |
| 3241 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3242 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3243 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3244 | return -1; |
| 3245 | } |
| 3246 | if (_PyUnicode_Dirty(unicode)) |
| 3247 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3248 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3249 | index, ch); |
| 3250 | return 0; |
| 3251 | } |
| 3252 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3253 | const char * |
| 3254 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3255 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3256 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3259 | /* create or adjust a UnicodeDecodeError */ |
| 3260 | static void |
| 3261 | make_decode_exception(PyObject **exceptionObject, |
| 3262 | const char *encoding, |
| 3263 | const char *input, Py_ssize_t length, |
| 3264 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3265 | const char *reason) |
| 3266 | { |
| 3267 | if (*exceptionObject == NULL) { |
| 3268 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3269 | encoding, input, length, startpos, endpos, reason); |
| 3270 | } |
| 3271 | else { |
| 3272 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3273 | goto onError; |
| 3274 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3275 | goto onError; |
| 3276 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3277 | goto onError; |
| 3278 | } |
| 3279 | return; |
| 3280 | |
| 3281 | onError: |
| 3282 | Py_DECREF(*exceptionObject); |
| 3283 | *exceptionObject = NULL; |
| 3284 | } |
| 3285 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3286 | /* error handling callback helper: |
| 3287 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3288 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3289 | and adjust various state variables. |
| 3290 | return 0 on success, -1 on error |
| 3291 | */ |
| 3292 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3293 | static int |
| 3294 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3295 | const char *encoding, const char *reason, |
| 3296 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3297 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3298 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3299 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3300 | 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] | 3301 | |
| 3302 | PyObject *restuple = NULL; |
| 3303 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3304 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3305 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3306 | Py_ssize_t requiredsize; |
| 3307 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3308 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3309 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3310 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3311 | int res = -1; |
| 3312 | |
| 3313 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3314 | *errorHandler = PyCodec_LookupError(errors); |
| 3315 | if (*errorHandler == NULL) |
| 3316 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3319 | make_decode_exception(exceptionObject, |
| 3320 | encoding, |
| 3321 | *input, *inend - *input, |
| 3322 | *startinpos, *endinpos, |
| 3323 | reason); |
| 3324 | if (*exceptionObject == NULL) |
| 3325 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3326 | |
| 3327 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3328 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3329 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3330 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3331 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3332 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3333 | } |
| 3334 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3335 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3336 | |
| 3337 | /* Copy back the bytes variables, which might have been modified by the |
| 3338 | callback */ |
| 3339 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3340 | if (!inputobj) |
| 3341 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3342 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3343 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3344 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3345 | *input = PyBytes_AS_STRING(inputobj); |
| 3346 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3347 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3348 | /* we can DECREF safely, as the exception has another reference, |
| 3349 | so the object won't go away. */ |
| 3350 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3351 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3352 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3353 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3354 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3355 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3356 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3357 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3358 | |
| 3359 | /* need more space? (at least enough for what we |
| 3360 | have+the replacement+the rest of the string (starting |
| 3361 | at the new input position), so we won't have to check space |
| 3362 | when there are no errors in the rest of the string) */ |
| 3363 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3364 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3365 | requiredsize = *outpos + repsize + insize-newpos; |
| 3366 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3367 | if (requiredsize<2*outsize) |
| 3368 | requiredsize = 2*outsize; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3369 | if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3370 | goto onError; |
| 3371 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3372 | } |
| 3373 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3374 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3375 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3376 | *outptr += repsize; |
| 3377 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3378 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3379 | /* we made it! */ |
| 3380 | res = 0; |
| 3381 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3382 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3383 | Py_XDECREF(restuple); |
| 3384 | return res; |
| 3385 | } |
| 3386 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3387 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3388 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3389 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3390 | |
| 3391 | /* Three simple macros defining base-64. */ |
| 3392 | |
| 3393 | /* Is c a base-64 character? */ |
| 3394 | |
| 3395 | #define IS_BASE64(c) \ |
| 3396 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3397 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3398 | ((c) >= '0' && (c) <= '9') || \ |
| 3399 | (c) == '+' || (c) == '/') |
| 3400 | |
| 3401 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3402 | |
| 3403 | #define FROM_BASE64(c) \ |
| 3404 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3405 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3406 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3407 | (c) == '+' ? 62 : 63) |
| 3408 | |
| 3409 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3410 | |
| 3411 | #define TO_BASE64(n) \ |
| 3412 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3413 | |
| 3414 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3415 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3416 | * byte not decoding to itself is the + which begins a base64 |
| 3417 | * string. */ |
| 3418 | |
| 3419 | #define DECODE_DIRECT(c) \ |
| 3420 | ((c) <= 127 && (c) != '+') |
| 3421 | |
| 3422 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3423 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3424 | * the above). See RFC2152. This array identifies these different |
| 3425 | * sets: |
| 3426 | * 0 : "Set D" |
| 3427 | * alphanumeric and '(),-./:? |
| 3428 | * 1 : "Set O" |
| 3429 | * !"#$%&*;<=>@[]^_`{|} |
| 3430 | * 2 : "whitespace" |
| 3431 | * ht nl cr sp |
| 3432 | * 3 : special (must be base64 encoded) |
| 3433 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3434 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3435 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3436 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3437 | char utf7_category[128] = { |
| 3438 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3439 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3440 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3441 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3442 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3443 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3444 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3445 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 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 [ \ ] ^ _ */ |
| 3449 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3450 | /* ` a b c d e f g h i j k l m n o */ |
| 3451 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3452 | /* p q r s t u v w x y z { | } ~ del */ |
| 3453 | 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] | 3454 | }; |
| 3455 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3456 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3457 | * answer depends on whether we are encoding set O as itself, and also |
| 3458 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3459 | * clear that the answers to these questions vary between |
| 3460 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3461 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3462 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3463 | ((c) < 128 && (c) > 0 && \ |
| 3464 | ((utf7_category[(c)] == 0) || \ |
| 3465 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3466 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3467 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3468 | PyObject * |
| 3469 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3470 | Py_ssize_t size, |
| 3471 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3472 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3473 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3474 | } |
| 3475 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3476 | /* The decoder. The only state we preserve is our read position, |
| 3477 | * i.e. how many characters we have consumed. So if we end in the |
| 3478 | * middle of a shift sequence we have to back off the read position |
| 3479 | * and the output to the beginning of the sequence, otherwise we lose |
| 3480 | * all the shift state (seen bits, number of bits seen, high |
| 3481 | * surrogate). */ |
| 3482 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3483 | PyObject * |
| 3484 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3485 | Py_ssize_t size, |
| 3486 | const char *errors, |
| 3487 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3488 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3489 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3490 | Py_ssize_t startinpos; |
| 3491 | Py_ssize_t endinpos; |
| 3492 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3493 | const char *e; |
| 3494 | PyUnicodeObject *unicode; |
| 3495 | Py_UNICODE *p; |
| 3496 | const char *errmsg = ""; |
| 3497 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3498 | Py_UNICODE *shiftOutStart; |
| 3499 | unsigned int base64bits = 0; |
| 3500 | unsigned long base64buffer = 0; |
| 3501 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3502 | PyObject *errorHandler = NULL; |
| 3503 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3504 | |
| 3505 | unicode = _PyUnicode_New(size); |
| 3506 | if (!unicode) |
| 3507 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3508 | if (size == 0) { |
| 3509 | if (consumed) |
| 3510 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3511 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3512 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3513 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3514 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3515 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3516 | e = s + size; |
| 3517 | |
| 3518 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3519 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3520 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3521 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3522 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3523 | if (inShift) { /* in a base-64 section */ |
| 3524 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3525 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3526 | base64bits += 6; |
| 3527 | s++; |
| 3528 | if (base64bits >= 16) { |
| 3529 | /* we have enough bits for a UTF-16 value */ |
| 3530 | Py_UNICODE outCh = (Py_UNICODE) |
| 3531 | (base64buffer >> (base64bits-16)); |
| 3532 | base64bits -= 16; |
| 3533 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3534 | if (surrogate) { |
| 3535 | /* expecting a second surrogate */ |
| 3536 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3537 | #ifdef Py_UNICODE_WIDE |
| 3538 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3539 | | (outCh & 0x3FF)) + 0x10000; |
| 3540 | #else |
| 3541 | *p++ = surrogate; |
| 3542 | *p++ = outCh; |
| 3543 | #endif |
| 3544 | surrogate = 0; |
| 3545 | } |
| 3546 | else { |
| 3547 | surrogate = 0; |
| 3548 | errmsg = "second surrogate missing"; |
| 3549 | goto utf7Error; |
| 3550 | } |
| 3551 | } |
| 3552 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3553 | /* first surrogate */ |
| 3554 | surrogate = outCh; |
| 3555 | } |
| 3556 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3557 | errmsg = "unexpected second surrogate"; |
| 3558 | goto utf7Error; |
| 3559 | } |
| 3560 | else { |
| 3561 | *p++ = outCh; |
| 3562 | } |
| 3563 | } |
| 3564 | } |
| 3565 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3566 | inShift = 0; |
| 3567 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3568 | if (surrogate) { |
| 3569 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3570 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3571 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3572 | if (base64bits > 0) { /* left-over bits */ |
| 3573 | if (base64bits >= 6) { |
| 3574 | /* We've seen at least one base-64 character */ |
| 3575 | errmsg = "partial character in shift sequence"; |
| 3576 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3577 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3578 | else { |
| 3579 | /* Some bits remain; they should be zero */ |
| 3580 | if (base64buffer != 0) { |
| 3581 | errmsg = "non-zero padding bits in shift sequence"; |
| 3582 | goto utf7Error; |
| 3583 | } |
| 3584 | } |
| 3585 | } |
| 3586 | if (ch != '-') { |
| 3587 | /* '-' is absorbed; other terminating |
| 3588 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3589 | *p++ = ch; |
| 3590 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3591 | } |
| 3592 | } |
| 3593 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3594 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3595 | s++; /* consume '+' */ |
| 3596 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3597 | s++; |
| 3598 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3599 | } |
| 3600 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3601 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3602 | shiftOutStart = p; |
| 3603 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3604 | } |
| 3605 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3606 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3607 | *p++ = ch; |
| 3608 | s++; |
| 3609 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3610 | else { |
| 3611 | startinpos = s-starts; |
| 3612 | s++; |
| 3613 | errmsg = "unexpected special character"; |
| 3614 | goto utf7Error; |
| 3615 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3616 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3617 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3618 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3619 | endinpos = s-starts; |
| 3620 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3621 | errors, &errorHandler, |
| 3622 | "utf7", errmsg, |
| 3623 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3624 | &unicode, &outpos, &p)) |
| 3625 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3628 | /* end of string */ |
| 3629 | |
| 3630 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3631 | /* if we're in an inconsistent state, that's an error */ |
| 3632 | if (surrogate || |
| 3633 | (base64bits >= 6) || |
| 3634 | (base64bits > 0 && base64buffer != 0)) { |
| 3635 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3636 | endinpos = size; |
| 3637 | if (unicode_decode_call_errorhandler( |
| 3638 | errors, &errorHandler, |
| 3639 | "utf7", "unterminated shift sequence", |
| 3640 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3641 | &unicode, &outpos, &p)) |
| 3642 | goto onError; |
| 3643 | if (s < e) |
| 3644 | goto restart; |
| 3645 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3646 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3647 | |
| 3648 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3649 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3650 | if (inShift) { |
| 3651 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3652 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3653 | } |
| 3654 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3655 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3656 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3657 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3658 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3659 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3660 | goto onError; |
| 3661 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3662 | Py_XDECREF(errorHandler); |
| 3663 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3664 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3665 | Py_DECREF(unicode); |
| 3666 | return NULL; |
| 3667 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3668 | return (PyObject *)unicode; |
| 3669 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3670 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3671 | Py_XDECREF(errorHandler); |
| 3672 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3673 | Py_DECREF(unicode); |
| 3674 | return NULL; |
| 3675 | } |
| 3676 | |
| 3677 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3678 | PyObject * |
| 3679 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3680 | Py_ssize_t size, |
| 3681 | int base64SetO, |
| 3682 | int base64WhiteSpace, |
| 3683 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3684 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3685 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3686 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3687 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3688 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3689 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3690 | unsigned int base64bits = 0; |
| 3691 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3692 | char * out; |
| 3693 | char * start; |
| 3694 | |
| 3695 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3696 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3697 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3698 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3699 | return PyErr_NoMemory(); |
| 3700 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3701 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3702 | if (v == NULL) |
| 3703 | return NULL; |
| 3704 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3705 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3706 | for (;i < size; ++i) { |
| 3707 | Py_UNICODE ch = s[i]; |
| 3708 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3709 | if (inShift) { |
| 3710 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3711 | /* shifting out */ |
| 3712 | if (base64bits) { /* output remaining bits */ |
| 3713 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3714 | base64buffer = 0; |
| 3715 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3716 | } |
| 3717 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3718 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3719 | so no '-' is required, except if the character is itself a '-' */ |
| 3720 | if (IS_BASE64(ch) || ch == '-') { |
| 3721 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3722 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3723 | *out++ = (char) ch; |
| 3724 | } |
| 3725 | else { |
| 3726 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3727 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3728 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3729 | else { /* not in a shift sequence */ |
| 3730 | if (ch == '+') { |
| 3731 | *out++ = '+'; |
| 3732 | *out++ = '-'; |
| 3733 | } |
| 3734 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3735 | *out++ = (char) ch; |
| 3736 | } |
| 3737 | else { |
| 3738 | *out++ = '+'; |
| 3739 | inShift = 1; |
| 3740 | goto encode_char; |
| 3741 | } |
| 3742 | } |
| 3743 | continue; |
| 3744 | encode_char: |
| 3745 | #ifdef Py_UNICODE_WIDE |
| 3746 | if (ch >= 0x10000) { |
| 3747 | /* code first surrogate */ |
| 3748 | base64bits += 16; |
| 3749 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3750 | while (base64bits >= 6) { |
| 3751 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3752 | base64bits -= 6; |
| 3753 | } |
| 3754 | /* prepare second surrogate */ |
| 3755 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3756 | } |
| 3757 | #endif |
| 3758 | base64bits += 16; |
| 3759 | base64buffer = (base64buffer << 16) | ch; |
| 3760 | while (base64bits >= 6) { |
| 3761 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3762 | base64bits -= 6; |
| 3763 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3764 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3765 | if (base64bits) |
| 3766 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3767 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3768 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3769 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3770 | return NULL; |
| 3771 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3774 | #undef IS_BASE64 |
| 3775 | #undef FROM_BASE64 |
| 3776 | #undef TO_BASE64 |
| 3777 | #undef DECODE_DIRECT |
| 3778 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3779 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3780 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 3781 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3782 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3783 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3784 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 3785 | illegal prefix. See RFC 3629 for details */ |
| 3786 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 3787 | 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] | 3788 | 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] | 3789 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3790 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3791 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3792 | 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] | 3793 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 3794 | 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] | 3795 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3796 | 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] | 3797 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 3798 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 3799 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 3800 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 3801 | 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] | 3802 | }; |
| 3803 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3804 | PyObject * |
| 3805 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3806 | Py_ssize_t size, |
| 3807 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3808 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3809 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 3810 | } |
| 3811 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3812 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 3813 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 3814 | |
| 3815 | /* Mask to quickly check whether a C 'long' contains a |
| 3816 | non-ASCII, UTF8-encoded char. */ |
| 3817 | #if (SIZEOF_LONG == 8) |
| 3818 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 3819 | #elif (SIZEOF_LONG == 4) |
| 3820 | # define ASCII_CHAR_MASK 0x80808080L |
| 3821 | #else |
| 3822 | # error C 'long' size should be either 4 or 8! |
| 3823 | #endif |
| 3824 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3825 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 3826 | the size of the decoded unicode string and if any major errors were |
| 3827 | encountered. |
| 3828 | |
| 3829 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 3830 | if the string contains surrogates, and if all continuation bytes are |
| 3831 | within the correct ranges, these checks are performed in |
| 3832 | PyUnicode_DecodeUTF8Stateful. |
| 3833 | |
| 3834 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 3835 | will be bogus and you should not rely on useful information in them. |
| 3836 | */ |
| 3837 | static Py_UCS4 |
| 3838 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 3839 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 3840 | int *has_errors) |
| 3841 | { |
| 3842 | Py_ssize_t n; |
| 3843 | Py_ssize_t char_count = 0; |
| 3844 | Py_UCS4 max_char = 127, new_max; |
| 3845 | Py_UCS4 upper_bound; |
| 3846 | const unsigned char *p = (const unsigned char *)s; |
| 3847 | const unsigned char *end = p + string_size; |
| 3848 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 3849 | int err = 0; |
| 3850 | |
| 3851 | for (; p < end && !err; ++p, ++char_count) { |
| 3852 | /* Only check value if it's not a ASCII char... */ |
| 3853 | if (*p < 0x80) { |
| 3854 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 3855 | an explanation. */ |
| 3856 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 3857 | /* Help register allocation */ |
| 3858 | register const unsigned char *_p = p; |
| 3859 | while (_p < aligned_end) { |
| 3860 | unsigned long value = *(unsigned long *) _p; |
| 3861 | if (value & ASCII_CHAR_MASK) |
| 3862 | break; |
| 3863 | _p += SIZEOF_LONG; |
| 3864 | char_count += SIZEOF_LONG; |
| 3865 | } |
| 3866 | p = _p; |
| 3867 | if (p == end) |
| 3868 | break; |
| 3869 | } |
| 3870 | } |
| 3871 | if (*p >= 0x80) { |
| 3872 | n = utf8_code_length[*p]; |
| 3873 | new_max = max_char; |
| 3874 | switch (n) { |
| 3875 | /* invalid start byte */ |
| 3876 | case 0: |
| 3877 | err = 1; |
| 3878 | break; |
| 3879 | case 2: |
| 3880 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 3881 | Approximate the upper bound of the code point, |
| 3882 | if this flips over 255 we can be sure it will be more |
| 3883 | than 255 and the string will need 2 bytes per code coint, |
| 3884 | if it stays under or equal to 255, we can be sure 1 byte |
| 3885 | is enough. |
| 3886 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 3887 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 3888 | if (max_char < upper_bound) |
| 3889 | new_max = upper_bound; |
| 3890 | /* Ensure we track at least that we left ASCII space. */ |
| 3891 | if (new_max < 128) |
| 3892 | new_max = 128; |
| 3893 | break; |
| 3894 | case 3: |
| 3895 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 3896 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 3897 | if (max_char < 65535) |
| 3898 | new_max = 65535; |
| 3899 | break; |
| 3900 | case 4: |
| 3901 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 3902 | new_max = 65537; |
| 3903 | break; |
| 3904 | /* Internal error, this should be caught by the first if */ |
| 3905 | case 1: |
| 3906 | default: |
| 3907 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 3908 | err = 1; |
| 3909 | } |
| 3910 | /* Instead of number of overall bytes for this code point, |
| 3911 | n containts the number of following bytes: */ |
| 3912 | --n; |
| 3913 | /* Check if the follow up chars are all valid continuation bytes */ |
| 3914 | if (n >= 1) { |
| 3915 | const unsigned char *cont; |
| 3916 | if ((p + n) >= end) { |
| 3917 | if (consumed == 0) |
| 3918 | /* incomplete data, non-incremental decoding */ |
| 3919 | err = 1; |
| 3920 | break; |
| 3921 | } |
| 3922 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 3923 | if ((*cont & 0xc0) != 0x80) { |
| 3924 | err = 1; |
| 3925 | break; |
| 3926 | } |
| 3927 | } |
| 3928 | p += n; |
| 3929 | } |
| 3930 | else |
| 3931 | err = 1; |
| 3932 | max_char = new_max; |
| 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | if (unicode_size) |
| 3937 | *unicode_size = char_count; |
| 3938 | if (has_errors) |
| 3939 | *has_errors = err; |
| 3940 | return max_char; |
| 3941 | } |
| 3942 | |
| 3943 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 3944 | of the legacy unicode representation */ |
| 3945 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 3946 | do { \ |
| 3947 | const int k_ = (kind); \ |
| 3948 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 3949 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 3950 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 3951 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 3952 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 3953 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 3954 | else \ |
| 3955 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 3956 | } while (0) |
| 3957 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3958 | PyObject * |
| 3959 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3960 | Py_ssize_t size, |
| 3961 | const char *errors, |
| 3962 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3963 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3964 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3965 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3966 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3967 | Py_ssize_t startinpos; |
| 3968 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3969 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3970 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3971 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3972 | PyObject *errorHandler = NULL; |
| 3973 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3974 | Py_UCS4 maxchar = 0; |
| 3975 | Py_ssize_t unicode_size; |
| 3976 | Py_ssize_t i; |
| 3977 | int kind; |
| 3978 | void *data; |
| 3979 | int has_errors; |
| 3980 | Py_UNICODE *error_outptr; |
| 3981 | #if SIZEOF_WCHAR_T == 2 |
| 3982 | Py_ssize_t wchar_offset = 0; |
| 3983 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3984 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3985 | if (size == 0) { |
| 3986 | if (consumed) |
| 3987 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3988 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3989 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3990 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 3991 | consumed, &has_errors); |
| 3992 | if (has_errors) { |
| 3993 | unicode = _PyUnicode_New(size); |
| 3994 | if (!unicode) |
| 3995 | return NULL; |
| 3996 | kind = PyUnicode_WCHAR_KIND; |
| 3997 | data = PyUnicode_AS_UNICODE(unicode); |
| 3998 | assert(data != NULL); |
| 3999 | } |
| 4000 | else { |
| 4001 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 4002 | if (!unicode) |
| 4003 | return NULL; |
| 4004 | /* When the string is ASCII only, just use memcpy and return. |
| 4005 | unicode_size may be != size if there is an incomplete UTF-8 |
| 4006 | sequence at the end of the ASCII block. */ |
| 4007 | if (maxchar < 128 && size == unicode_size) { |
| 4008 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 4009 | return (PyObject *)unicode; |
| 4010 | } |
| 4011 | kind = PyUnicode_KIND(unicode); |
| 4012 | data = PyUnicode_DATA(unicode); |
| 4013 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4014 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4015 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4016 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4017 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4018 | |
| 4019 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4020 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4021 | |
| 4022 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4023 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 4024 | input will consist of an overwhelming majority of ASCII |
| 4025 | characters, we try to optimize for this case by checking |
| 4026 | as many characters as a C 'long' can contain. |
| 4027 | First, check if we can do an aligned read, as most CPUs have |
| 4028 | a penalty for unaligned reads. |
| 4029 | */ |
| 4030 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 4031 | /* Help register allocation */ |
| 4032 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4033 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4034 | while (_s < aligned_end) { |
| 4035 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 4036 | and do a fast unrolled copy if it only contains ASCII |
| 4037 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4038 | unsigned long value = *(unsigned long *) _s; |
| 4039 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4040 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4041 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4042 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4043 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4044 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4045 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4046 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4047 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4048 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4049 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4050 | #endif |
| 4051 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4052 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4053 | } |
| 4054 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4055 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4056 | if (s == e) |
| 4057 | break; |
| 4058 | ch = (unsigned char)*s; |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4063 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4064 | s++; |
| 4065 | continue; |
| 4066 | } |
| 4067 | |
| 4068 | n = utf8_code_length[ch]; |
| 4069 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4070 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4071 | if (consumed) |
| 4072 | break; |
| 4073 | else { |
| 4074 | errmsg = "unexpected end of data"; |
| 4075 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4076 | endinpos = startinpos+1; |
| 4077 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4078 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4079 | goto utf8Error; |
| 4080 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4081 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4082 | |
| 4083 | switch (n) { |
| 4084 | |
| 4085 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4086 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4087 | startinpos = s-starts; |
| 4088 | endinpos = startinpos+1; |
| 4089 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4090 | |
| 4091 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4092 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4093 | startinpos = s-starts; |
| 4094 | endinpos = startinpos+1; |
| 4095 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4096 | |
| 4097 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4098 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4099 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4100 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4101 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4102 | goto utf8Error; |
| 4103 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4104 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4105 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4106 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4107 | break; |
| 4108 | |
| 4109 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4110 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4111 | will result in surrogates in range d800-dfff. Surrogates are |
| 4112 | not valid UTF-8 so they are rejected. |
| 4113 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4114 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4115 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4116 | (s[2] & 0xc0) != 0x80 || |
| 4117 | ((unsigned char)s[0] == 0xE0 && |
| 4118 | (unsigned char)s[1] < 0xA0) || |
| 4119 | ((unsigned char)s[0] == 0xED && |
| 4120 | (unsigned char)s[1] > 0x9F)) { |
| 4121 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4122 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4123 | endinpos = startinpos + 1; |
| 4124 | |
| 4125 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4126 | continuation byte is s[2], so increment endinpos by 1, |
| 4127 | if not, s[1] is invalid and endinpos doesn't need to |
| 4128 | be incremented. */ |
| 4129 | if ((s[1] & 0xC0) == 0x80) |
| 4130 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4131 | goto utf8Error; |
| 4132 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4133 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4134 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4135 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4136 | break; |
| 4137 | |
| 4138 | case 4: |
| 4139 | if ((s[1] & 0xc0) != 0x80 || |
| 4140 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4141 | (s[3] & 0xc0) != 0x80 || |
| 4142 | ((unsigned char)s[0] == 0xF0 && |
| 4143 | (unsigned char)s[1] < 0x90) || |
| 4144 | ((unsigned char)s[0] == 0xF4 && |
| 4145 | (unsigned char)s[1] > 0x8F)) { |
| 4146 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4147 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4148 | endinpos = startinpos + 1; |
| 4149 | if ((s[1] & 0xC0) == 0x80) { |
| 4150 | endinpos++; |
| 4151 | if ((s[2] & 0xC0) == 0x80) |
| 4152 | endinpos++; |
| 4153 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4154 | goto utf8Error; |
| 4155 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4156 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4157 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4158 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4159 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4160 | /* If the string is flexible or we have native UCS-4, write |
| 4161 | directly.. */ |
| 4162 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4163 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4164 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4165 | else { |
| 4166 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4167 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4168 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4169 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4170 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4171 | /* high surrogate = top 10 bits added to D800 */ |
| 4172 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4173 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4174 | |
| 4175 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4176 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4177 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4178 | } |
| 4179 | #if SIZEOF_WCHAR_T == 2 |
| 4180 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4181 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4182 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4183 | } |
| 4184 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4185 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4186 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4187 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4188 | /* If this is not yet a resizable string, make it one.. */ |
| 4189 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4190 | const Py_UNICODE *u; |
| 4191 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 4192 | if (!new_unicode) |
| 4193 | goto onError; |
| 4194 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 4195 | if (!u) |
| 4196 | goto onError; |
| 4197 | #if SIZEOF_WCHAR_T == 2 |
| 4198 | i += wchar_offset; |
| 4199 | #endif |
| 4200 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4201 | Py_DECREF(unicode); |
| 4202 | unicode = new_unicode; |
| 4203 | kind = 0; |
| 4204 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4205 | assert(data != NULL); |
| 4206 | } |
| 4207 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4208 | if (unicode_decode_call_errorhandler( |
| 4209 | errors, &errorHandler, |
| 4210 | "utf8", errmsg, |
| 4211 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4212 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4213 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4214 | /* Update data because unicode_decode_call_errorhandler might have |
| 4215 | re-created or resized the unicode object. */ |
| 4216 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4217 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4218 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4219 | /* Ensure the unicode_size calculation above was correct: */ |
| 4220 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4221 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4222 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4223 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4224 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4225 | /* Adjust length and ready string when it contained errors and |
| 4226 | is of the old resizable kind. */ |
| 4227 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4228 | if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4229 | goto onError; |
| 4230 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4231 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4232 | Py_XDECREF(errorHandler); |
| 4233 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4234 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4235 | Py_DECREF(unicode); |
| 4236 | return NULL; |
| 4237 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4238 | return (PyObject *)unicode; |
| 4239 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4240 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4241 | Py_XDECREF(errorHandler); |
| 4242 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4243 | Py_DECREF(unicode); |
| 4244 | return NULL; |
| 4245 | } |
| 4246 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4247 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4248 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4249 | #ifdef __APPLE__ |
| 4250 | |
| 4251 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4252 | used to decode the command line arguments on Mac OS X. */ |
| 4253 | |
| 4254 | wchar_t* |
| 4255 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4256 | { |
| 4257 | int n; |
| 4258 | const char *e; |
| 4259 | wchar_t *unicode, *p; |
| 4260 | |
| 4261 | /* Note: size will always be longer than the resulting Unicode |
| 4262 | character count */ |
| 4263 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4264 | PyErr_NoMemory(); |
| 4265 | return NULL; |
| 4266 | } |
| 4267 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4268 | if (!unicode) |
| 4269 | return NULL; |
| 4270 | |
| 4271 | /* Unpack UTF-8 encoded data */ |
| 4272 | p = unicode; |
| 4273 | e = s + size; |
| 4274 | while (s < e) { |
| 4275 | Py_UCS4 ch = (unsigned char)*s; |
| 4276 | |
| 4277 | if (ch < 0x80) { |
| 4278 | *p++ = (wchar_t)ch; |
| 4279 | s++; |
| 4280 | continue; |
| 4281 | } |
| 4282 | |
| 4283 | n = utf8_code_length[ch]; |
| 4284 | if (s + n > e) { |
| 4285 | goto surrogateescape; |
| 4286 | } |
| 4287 | |
| 4288 | switch (n) { |
| 4289 | case 0: |
| 4290 | case 1: |
| 4291 | goto surrogateescape; |
| 4292 | |
| 4293 | case 2: |
| 4294 | if ((s[1] & 0xc0) != 0x80) |
| 4295 | goto surrogateescape; |
| 4296 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4297 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4298 | *p++ = (wchar_t)ch; |
| 4299 | break; |
| 4300 | |
| 4301 | case 3: |
| 4302 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4303 | will result in surrogates in range d800-dfff. Surrogates are |
| 4304 | not valid UTF-8 so they are rejected. |
| 4305 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4306 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4307 | if ((s[1] & 0xc0) != 0x80 || |
| 4308 | (s[2] & 0xc0) != 0x80 || |
| 4309 | ((unsigned char)s[0] == 0xE0 && |
| 4310 | (unsigned char)s[1] < 0xA0) || |
| 4311 | ((unsigned char)s[0] == 0xED && |
| 4312 | (unsigned char)s[1] > 0x9F)) { |
| 4313 | |
| 4314 | goto surrogateescape; |
| 4315 | } |
| 4316 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4317 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4318 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4319 | break; |
| 4320 | |
| 4321 | case 4: |
| 4322 | if ((s[1] & 0xc0) != 0x80 || |
| 4323 | (s[2] & 0xc0) != 0x80 || |
| 4324 | (s[3] & 0xc0) != 0x80 || |
| 4325 | ((unsigned char)s[0] == 0xF0 && |
| 4326 | (unsigned char)s[1] < 0x90) || |
| 4327 | ((unsigned char)s[0] == 0xF4 && |
| 4328 | (unsigned char)s[1] > 0x8F)) { |
| 4329 | goto surrogateescape; |
| 4330 | } |
| 4331 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4332 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4333 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4334 | |
| 4335 | #if SIZEOF_WCHAR_T == 4 |
| 4336 | *p++ = (wchar_t)ch; |
| 4337 | #else |
| 4338 | /* compute and append the two surrogates: */ |
| 4339 | |
| 4340 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4341 | ch -= 0x10000; |
| 4342 | |
| 4343 | /* high surrogate = top 10 bits added to D800 */ |
| 4344 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4345 | |
| 4346 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4347 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4348 | #endif |
| 4349 | break; |
| 4350 | } |
| 4351 | s += n; |
| 4352 | continue; |
| 4353 | |
| 4354 | surrogateescape: |
| 4355 | *p++ = 0xDC00 + ch; |
| 4356 | s++; |
| 4357 | } |
| 4358 | *p = L'\0'; |
| 4359 | return unicode; |
| 4360 | } |
| 4361 | |
| 4362 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4363 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4364 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4365 | |
| 4366 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4367 | and allocate exactly as much space needed at the end. Else allocate the |
| 4368 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4369 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4370 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4371 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4372 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4373 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4374 | #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] | 4375 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4376 | Py_ssize_t i; /* index into s of next input byte */ |
| 4377 | PyObject *result; /* result string object */ |
| 4378 | char *p; /* next free byte in output buffer */ |
| 4379 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4380 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4381 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4382 | PyObject *errorHandler = NULL; |
| 4383 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4384 | int kind; |
| 4385 | void *data; |
| 4386 | Py_ssize_t size; |
| 4387 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4388 | #if SIZEOF_WCHAR_T == 2 |
| 4389 | Py_ssize_t wchar_offset = 0; |
| 4390 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4391 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4392 | if (!PyUnicode_Check(unicode)) { |
| 4393 | PyErr_BadArgument(); |
| 4394 | return NULL; |
| 4395 | } |
| 4396 | |
| 4397 | if (PyUnicode_READY(unicode) == -1) |
| 4398 | return NULL; |
| 4399 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4400 | if (PyUnicode_UTF8(unicode)) |
| 4401 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4402 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4403 | |
| 4404 | kind = PyUnicode_KIND(unicode); |
| 4405 | data = PyUnicode_DATA(unicode); |
| 4406 | size = PyUnicode_GET_LENGTH(unicode); |
| 4407 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4408 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4409 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4410 | if (size <= MAX_SHORT_UNICHARS) { |
| 4411 | /* Write into the stack buffer; nallocated can't overflow. |
| 4412 | * At the end, we'll allocate exactly as much heap space as it |
| 4413 | * turns out we need. |
| 4414 | */ |
| 4415 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4416 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4417 | p = stackbuf; |
| 4418 | } |
| 4419 | else { |
| 4420 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4421 | nallocated = size * 4; |
| 4422 | if (nallocated / 4 != size) /* overflow! */ |
| 4423 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4424 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4425 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4426 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4427 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4428 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4429 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4430 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4431 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4432 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4433 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4434 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4435 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4437 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4438 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4439 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4440 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4441 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4442 | Py_ssize_t newpos; |
| 4443 | PyObject *rep; |
| 4444 | Py_ssize_t repsize, k, startpos; |
| 4445 | startpos = i-1; |
| 4446 | #if SIZEOF_WCHAR_T == 2 |
| 4447 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4448 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4449 | rep = unicode_encode_call_errorhandler( |
| 4450 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4451 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4452 | &exc, startpos, startpos+1, &newpos); |
| 4453 | if (!rep) |
| 4454 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4455 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4456 | if (PyBytes_Check(rep)) |
| 4457 | repsize = PyBytes_GET_SIZE(rep); |
| 4458 | else |
| 4459 | repsize = PyUnicode_GET_SIZE(rep); |
| 4460 | |
| 4461 | if (repsize > 4) { |
| 4462 | Py_ssize_t offset; |
| 4463 | |
| 4464 | if (result == NULL) |
| 4465 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4466 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4467 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4468 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4469 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4470 | /* integer overflow */ |
| 4471 | PyErr_NoMemory(); |
| 4472 | goto error; |
| 4473 | } |
| 4474 | nallocated += repsize - 4; |
| 4475 | if (result != NULL) { |
| 4476 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4477 | goto error; |
| 4478 | } else { |
| 4479 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4480 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4481 | goto error; |
| 4482 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4483 | } |
| 4484 | p = PyBytes_AS_STRING(result) + offset; |
| 4485 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4486 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4487 | if (PyBytes_Check(rep)) { |
| 4488 | char *prep = PyBytes_AS_STRING(rep); |
| 4489 | for(k = repsize; k > 0; k--) |
| 4490 | *p++ = *prep++; |
| 4491 | } else /* rep is unicode */ { |
| 4492 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4493 | Py_UNICODE c; |
| 4494 | |
| 4495 | for(k=0; k<repsize; k++) { |
| 4496 | c = prep[k]; |
| 4497 | if (0x80 <= c) { |
| 4498 | raise_encode_exception(&exc, "utf-8", |
| 4499 | PyUnicode_AS_UNICODE(unicode), |
| 4500 | size, i-1, i, |
| 4501 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4502 | goto error; |
| 4503 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4504 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4505 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4506 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4507 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4508 | } else if (ch < 0x10000) { |
| 4509 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4510 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4511 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4512 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4513 | /* Encode UCS4 Unicode ordinals */ |
| 4514 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4515 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4516 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4517 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4518 | #if SIZEOF_WCHAR_T == 2 |
| 4519 | wchar_offset++; |
| 4520 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4521 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4522 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4523 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4524 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4525 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4526 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4527 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4528 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4529 | } |
| 4530 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4531 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4532 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4533 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4534 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4535 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4536 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4537 | Py_XDECREF(errorHandler); |
| 4538 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4539 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4540 | error: |
| 4541 | Py_XDECREF(errorHandler); |
| 4542 | Py_XDECREF(exc); |
| 4543 | Py_XDECREF(result); |
| 4544 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4545 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4546 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4549 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4550 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4551 | Py_ssize_t size, |
| 4552 | const char *errors) |
| 4553 | { |
| 4554 | PyObject *v, *unicode; |
| 4555 | |
| 4556 | unicode = PyUnicode_FromUnicode(s, size); |
| 4557 | if (unicode == NULL) |
| 4558 | return NULL; |
| 4559 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4560 | Py_DECREF(unicode); |
| 4561 | return v; |
| 4562 | } |
| 4563 | |
| 4564 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4565 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4566 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4567 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4570 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4571 | |
| 4572 | PyObject * |
| 4573 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4574 | Py_ssize_t size, |
| 4575 | const char *errors, |
| 4576 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4577 | { |
| 4578 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4579 | } |
| 4580 | |
| 4581 | PyObject * |
| 4582 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4583 | Py_ssize_t size, |
| 4584 | const char *errors, |
| 4585 | int *byteorder, |
| 4586 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4587 | { |
| 4588 | const char *starts = s; |
| 4589 | Py_ssize_t startinpos; |
| 4590 | Py_ssize_t endinpos; |
| 4591 | Py_ssize_t outpos; |
| 4592 | PyUnicodeObject *unicode; |
| 4593 | Py_UNICODE *p; |
| 4594 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4595 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4596 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4597 | #else |
| 4598 | const int pairs = 0; |
| 4599 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4600 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4601 | int bo = 0; /* assume native ordering by default */ |
| 4602 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4603 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4604 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4605 | int iorder[] = {0, 1, 2, 3}; |
| 4606 | #else |
| 4607 | int iorder[] = {3, 2, 1, 0}; |
| 4608 | #endif |
| 4609 | PyObject *errorHandler = NULL; |
| 4610 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4611 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4612 | q = (unsigned char *)s; |
| 4613 | e = q + size; |
| 4614 | |
| 4615 | if (byteorder) |
| 4616 | bo = *byteorder; |
| 4617 | |
| 4618 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4619 | byte order setting accordingly. In native mode, the leading BOM |
| 4620 | mark is skipped, in all other modes, it is copied to the output |
| 4621 | stream as-is (giving a ZWNBSP character). */ |
| 4622 | if (bo == 0) { |
| 4623 | if (size >= 4) { |
| 4624 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4625 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4626 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4627 | if (bom == 0x0000FEFF) { |
| 4628 | q += 4; |
| 4629 | bo = -1; |
| 4630 | } |
| 4631 | else if (bom == 0xFFFE0000) { |
| 4632 | q += 4; |
| 4633 | bo = 1; |
| 4634 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4635 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4636 | if (bom == 0x0000FEFF) { |
| 4637 | q += 4; |
| 4638 | bo = 1; |
| 4639 | } |
| 4640 | else if (bom == 0xFFFE0000) { |
| 4641 | q += 4; |
| 4642 | bo = -1; |
| 4643 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4644 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4645 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | if (bo == -1) { |
| 4649 | /* force LE */ |
| 4650 | iorder[0] = 0; |
| 4651 | iorder[1] = 1; |
| 4652 | iorder[2] = 2; |
| 4653 | iorder[3] = 3; |
| 4654 | } |
| 4655 | else if (bo == 1) { |
| 4656 | /* force BE */ |
| 4657 | iorder[0] = 3; |
| 4658 | iorder[1] = 2; |
| 4659 | iorder[2] = 1; |
| 4660 | iorder[3] = 0; |
| 4661 | } |
| 4662 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4663 | /* On narrow builds we split characters outside the BMP into two |
| 4664 | codepoints => count how much extra space we need. */ |
| 4665 | #ifndef Py_UNICODE_WIDE |
| 4666 | for (qq = q; qq < e; qq += 4) |
| 4667 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4668 | pairs++; |
| 4669 | #endif |
| 4670 | |
| 4671 | /* This might be one to much, because of a BOM */ |
| 4672 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4673 | if (!unicode) |
| 4674 | return NULL; |
| 4675 | if (size == 0) |
| 4676 | return (PyObject *)unicode; |
| 4677 | |
| 4678 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4679 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4680 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4681 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4682 | Py_UCS4 ch; |
| 4683 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4684 | if (e-q<4) { |
| 4685 | if (consumed) |
| 4686 | break; |
| 4687 | errmsg = "truncated data"; |
| 4688 | startinpos = ((const char *)q)-starts; |
| 4689 | endinpos = ((const char *)e)-starts; |
| 4690 | goto utf32Error; |
| 4691 | /* The remaining input chars are ignored if the callback |
| 4692 | chooses to skip the input */ |
| 4693 | } |
| 4694 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4695 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4696 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4697 | if (ch >= 0x110000) |
| 4698 | { |
| 4699 | errmsg = "codepoint not in range(0x110000)"; |
| 4700 | startinpos = ((const char *)q)-starts; |
| 4701 | endinpos = startinpos+4; |
| 4702 | goto utf32Error; |
| 4703 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4704 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4705 | if (ch >= 0x10000) |
| 4706 | { |
| 4707 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4708 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4709 | } |
| 4710 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4711 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4712 | *p++ = ch; |
| 4713 | q += 4; |
| 4714 | continue; |
| 4715 | utf32Error: |
| 4716 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4717 | if (unicode_decode_call_errorhandler( |
| 4718 | errors, &errorHandler, |
| 4719 | "utf32", errmsg, |
| 4720 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4721 | &unicode, &outpos, &p)) |
| 4722 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | if (byteorder) |
| 4726 | *byteorder = bo; |
| 4727 | |
| 4728 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4729 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4730 | |
| 4731 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 4732 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4733 | goto onError; |
| 4734 | |
| 4735 | Py_XDECREF(errorHandler); |
| 4736 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4737 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4738 | Py_DECREF(unicode); |
| 4739 | return NULL; |
| 4740 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4741 | return (PyObject *)unicode; |
| 4742 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4743 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4744 | Py_DECREF(unicode); |
| 4745 | Py_XDECREF(errorHandler); |
| 4746 | Py_XDECREF(exc); |
| 4747 | return NULL; |
| 4748 | } |
| 4749 | |
| 4750 | PyObject * |
| 4751 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4752 | Py_ssize_t size, |
| 4753 | const char *errors, |
| 4754 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4755 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4756 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4757 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4758 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4759 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4760 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4761 | #else |
| 4762 | const int pairs = 0; |
| 4763 | #endif |
| 4764 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4765 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4766 | int iorder[] = {0, 1, 2, 3}; |
| 4767 | #else |
| 4768 | int iorder[] = {3, 2, 1, 0}; |
| 4769 | #endif |
| 4770 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4771 | #define STORECHAR(CH) \ |
| 4772 | do { \ |
| 4773 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4774 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4775 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4776 | p[iorder[0]] = (CH) & 0xff; \ |
| 4777 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4778 | } while(0) |
| 4779 | |
| 4780 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 4781 | so we need less space. */ |
| 4782 | #ifndef Py_UNICODE_WIDE |
| 4783 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4784 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 4785 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 4786 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4787 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4788 | nsize = (size - pairs + (byteorder == 0)); |
| 4789 | bytesize = nsize * 4; |
| 4790 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4791 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4792 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4793 | if (v == NULL) |
| 4794 | return NULL; |
| 4795 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4796 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4797 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4798 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4799 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4800 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4801 | |
| 4802 | if (byteorder == -1) { |
| 4803 | /* force LE */ |
| 4804 | iorder[0] = 0; |
| 4805 | iorder[1] = 1; |
| 4806 | iorder[2] = 2; |
| 4807 | iorder[3] = 3; |
| 4808 | } |
| 4809 | else if (byteorder == 1) { |
| 4810 | /* force BE */ |
| 4811 | iorder[0] = 3; |
| 4812 | iorder[1] = 2; |
| 4813 | iorder[2] = 1; |
| 4814 | iorder[3] = 0; |
| 4815 | } |
| 4816 | |
| 4817 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4818 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4819 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4820 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 4821 | Py_UCS4 ch2 = *s; |
| 4822 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 4823 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 4824 | s++; |
| 4825 | size--; |
| 4826 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4827 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4828 | #endif |
| 4829 | STORECHAR(ch); |
| 4830 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4831 | |
| 4832 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4833 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4834 | #undef STORECHAR |
| 4835 | } |
| 4836 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4837 | PyObject * |
| 4838 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4839 | { |
| 4840 | if (!PyUnicode_Check(unicode)) { |
| 4841 | PyErr_BadArgument(); |
| 4842 | return NULL; |
| 4843 | } |
| 4844 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4845 | PyUnicode_GET_SIZE(unicode), |
| 4846 | NULL, |
| 4847 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4850 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 4851 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4852 | PyObject * |
| 4853 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4854 | Py_ssize_t size, |
| 4855 | const char *errors, |
| 4856 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4857 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4858 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 4859 | } |
| 4860 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4861 | /* Two masks for fast checking of whether a C 'long' may contain |
| 4862 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 4863 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 4864 | rare in most input. |
| 4865 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 4866 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4867 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4868 | #if (SIZEOF_LONG == 8) |
| 4869 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 4870 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 4871 | #elif (SIZEOF_LONG == 4) |
| 4872 | # define FAST_CHAR_MASK 0x80008000L |
| 4873 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 4874 | #else |
| 4875 | # error C 'long' size should be either 4 or 8! |
| 4876 | #endif |
| 4877 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4878 | PyObject * |
| 4879 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4880 | Py_ssize_t size, |
| 4881 | const char *errors, |
| 4882 | int *byteorder, |
| 4883 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4884 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4885 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4886 | Py_ssize_t startinpos; |
| 4887 | Py_ssize_t endinpos; |
| 4888 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4889 | PyUnicodeObject *unicode; |
| 4890 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4891 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4892 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4893 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4894 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4895 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 4896 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4897 | int ihi = 1, ilo = 0; |
| 4898 | #else |
| 4899 | int ihi = 0, ilo = 1; |
| 4900 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4901 | PyObject *errorHandler = NULL; |
| 4902 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4903 | |
| 4904 | /* Note: size will always be longer than the resulting Unicode |
| 4905 | character count */ |
| 4906 | unicode = _PyUnicode_New(size); |
| 4907 | if (!unicode) |
| 4908 | return NULL; |
| 4909 | if (size == 0) |
| 4910 | return (PyObject *)unicode; |
| 4911 | |
| 4912 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4913 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4914 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4915 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4916 | |
| 4917 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4918 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4919 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4920 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4921 | byte order setting accordingly. In native mode, the leading BOM |
| 4922 | mark is skipped, in all other modes, it is copied to the output |
| 4923 | stream as-is (giving a ZWNBSP character). */ |
| 4924 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4925 | if (size >= 2) { |
| 4926 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4927 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4928 | if (bom == 0xFEFF) { |
| 4929 | q += 2; |
| 4930 | bo = -1; |
| 4931 | } |
| 4932 | else if (bom == 0xFFFE) { |
| 4933 | q += 2; |
| 4934 | bo = 1; |
| 4935 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4936 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4937 | if (bom == 0xFEFF) { |
| 4938 | q += 2; |
| 4939 | bo = 1; |
| 4940 | } |
| 4941 | else if (bom == 0xFFFE) { |
| 4942 | q += 2; |
| 4943 | bo = -1; |
| 4944 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4945 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4946 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4947 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4948 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4949 | if (bo == -1) { |
| 4950 | /* force LE */ |
| 4951 | ihi = 1; |
| 4952 | ilo = 0; |
| 4953 | } |
| 4954 | else if (bo == 1) { |
| 4955 | /* force BE */ |
| 4956 | ihi = 0; |
| 4957 | ilo = 1; |
| 4958 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4959 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4960 | native_ordering = ilo < ihi; |
| 4961 | #else |
| 4962 | native_ordering = ilo > ihi; |
| 4963 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4964 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4965 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4966 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4967 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4968 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 4969 | reads are more expensive, better to defer to another iteration. */ |
| 4970 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 4971 | /* Fast path for runs of non-surrogate chars. */ |
| 4972 | register const unsigned char *_q = q; |
| 4973 | Py_UNICODE *_p = p; |
| 4974 | if (native_ordering) { |
| 4975 | /* Native ordering is simple: as long as the input cannot |
| 4976 | possibly contain a surrogate char, do an unrolled copy |
| 4977 | of several 16-bit code points to the target object. |
| 4978 | The non-surrogate check is done on several input bytes |
| 4979 | at a time (as many as a C 'long' can contain). */ |
| 4980 | while (_q < aligned_end) { |
| 4981 | unsigned long data = * (unsigned long *) _q; |
| 4982 | if (data & FAST_CHAR_MASK) |
| 4983 | break; |
| 4984 | _p[0] = ((unsigned short *) _q)[0]; |
| 4985 | _p[1] = ((unsigned short *) _q)[1]; |
| 4986 | #if (SIZEOF_LONG == 8) |
| 4987 | _p[2] = ((unsigned short *) _q)[2]; |
| 4988 | _p[3] = ((unsigned short *) _q)[3]; |
| 4989 | #endif |
| 4990 | _q += SIZEOF_LONG; |
| 4991 | _p += SIZEOF_LONG / 2; |
| 4992 | } |
| 4993 | } |
| 4994 | else { |
| 4995 | /* Byteswapped ordering is similar, but we must decompose |
| 4996 | the copy bytewise, and take care of zero'ing out the |
| 4997 | upper bytes if the target object is in 32-bit units |
| 4998 | (that is, in UCS-4 builds). */ |
| 4999 | while (_q < aligned_end) { |
| 5000 | unsigned long data = * (unsigned long *) _q; |
| 5001 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 5002 | break; |
| 5003 | /* Zero upper bytes in UCS-4 builds */ |
| 5004 | #if (Py_UNICODE_SIZE > 2) |
| 5005 | _p[0] = 0; |
| 5006 | _p[1] = 0; |
| 5007 | #if (SIZEOF_LONG == 8) |
| 5008 | _p[2] = 0; |
| 5009 | _p[3] = 0; |
| 5010 | #endif |
| 5011 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5012 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 5013 | fill the two last bytes of each 4-byte unit. */ |
| 5014 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 5015 | # define OFF 2 |
| 5016 | #else |
| 5017 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5018 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 5019 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 5020 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 5021 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 5022 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 5023 | #if (SIZEOF_LONG == 8) |
| 5024 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 5025 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 5026 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 5027 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 5028 | #endif |
| 5029 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5030 | _q += SIZEOF_LONG; |
| 5031 | _p += SIZEOF_LONG / 2; |
| 5032 | } |
| 5033 | } |
| 5034 | p = _p; |
| 5035 | q = _q; |
| 5036 | if (q >= e) |
| 5037 | break; |
| 5038 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5039 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5040 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5041 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5042 | |
| 5043 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5044 | *p++ = ch; |
| 5045 | continue; |
| 5046 | } |
| 5047 | |
| 5048 | /* UTF-16 code pair: */ |
| 5049 | if (q > e) { |
| 5050 | errmsg = "unexpected end of data"; |
| 5051 | startinpos = (((const char *)q) - 2) - starts; |
| 5052 | endinpos = ((const char *)e) + 1 - starts; |
| 5053 | goto utf16Error; |
| 5054 | } |
| 5055 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5056 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5057 | q += 2; |
| 5058 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5059 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5060 | *p++ = ch; |
| 5061 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5062 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5063 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5064 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5065 | continue; |
| 5066 | } |
| 5067 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5068 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5069 | startinpos = (((const char *)q)-4)-starts; |
| 5070 | endinpos = startinpos+2; |
| 5071 | goto utf16Error; |
| 5072 | } |
| 5073 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5074 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5075 | errmsg = "illegal encoding"; |
| 5076 | startinpos = (((const char *)q)-2)-starts; |
| 5077 | endinpos = startinpos+2; |
| 5078 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5079 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5080 | utf16Error: |
| 5081 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5082 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5083 | errors, |
| 5084 | &errorHandler, |
| 5085 | "utf16", errmsg, |
| 5086 | &starts, |
| 5087 | (const char **)&e, |
| 5088 | &startinpos, |
| 5089 | &endinpos, |
| 5090 | &exc, |
| 5091 | (const char **)&q, |
| 5092 | &unicode, |
| 5093 | &outpos, |
| 5094 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5095 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5096 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5097 | /* remaining byte at the end? (size should be even) */ |
| 5098 | if (e == q) { |
| 5099 | if (!consumed) { |
| 5100 | errmsg = "truncated data"; |
| 5101 | startinpos = ((const char *)q) - starts; |
| 5102 | endinpos = ((const char *)e) + 1 - starts; |
| 5103 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5104 | if (unicode_decode_call_errorhandler( |
| 5105 | errors, |
| 5106 | &errorHandler, |
| 5107 | "utf16", errmsg, |
| 5108 | &starts, |
| 5109 | (const char **)&e, |
| 5110 | &startinpos, |
| 5111 | &endinpos, |
| 5112 | &exc, |
| 5113 | (const char **)&q, |
| 5114 | &unicode, |
| 5115 | &outpos, |
| 5116 | &p)) |
| 5117 | goto onError; |
| 5118 | /* The remaining input chars are ignored if the callback |
| 5119 | chooses to skip the input */ |
| 5120 | } |
| 5121 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5122 | |
| 5123 | if (byteorder) |
| 5124 | *byteorder = bo; |
| 5125 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5126 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5127 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5128 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5129 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5130 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5131 | goto onError; |
| 5132 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5133 | Py_XDECREF(errorHandler); |
| 5134 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5135 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5136 | Py_DECREF(unicode); |
| 5137 | return NULL; |
| 5138 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5139 | return (PyObject *)unicode; |
| 5140 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5141 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5142 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5143 | Py_XDECREF(errorHandler); |
| 5144 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5145 | return NULL; |
| 5146 | } |
| 5147 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5148 | #undef FAST_CHAR_MASK |
| 5149 | #undef SWAPPED_FAST_CHAR_MASK |
| 5150 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5151 | PyObject * |
| 5152 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5153 | Py_ssize_t size, |
| 5154 | const char *errors, |
| 5155 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5156 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5157 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5158 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5159 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5160 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5161 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5162 | #else |
| 5163 | const int pairs = 0; |
| 5164 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5165 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5166 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5167 | int ihi = 1, ilo = 0; |
| 5168 | #else |
| 5169 | int ihi = 0, ilo = 1; |
| 5170 | #endif |
| 5171 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5172 | #define STORECHAR(CH) \ |
| 5173 | do { \ |
| 5174 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5175 | p[ilo] = (CH) & 0xff; \ |
| 5176 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5177 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5178 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5179 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5180 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5181 | if (s[i] >= 0x10000) |
| 5182 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5183 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5184 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5185 | if (size > PY_SSIZE_T_MAX || |
| 5186 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5187 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5188 | nsize = size + pairs + (byteorder == 0); |
| 5189 | bytesize = nsize * 2; |
| 5190 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5191 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5192 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5193 | if (v == NULL) |
| 5194 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5195 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5196 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5197 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5198 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5199 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5200 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5201 | |
| 5202 | if (byteorder == -1) { |
| 5203 | /* force LE */ |
| 5204 | ihi = 1; |
| 5205 | ilo = 0; |
| 5206 | } |
| 5207 | else if (byteorder == 1) { |
| 5208 | /* force BE */ |
| 5209 | ihi = 0; |
| 5210 | ilo = 1; |
| 5211 | } |
| 5212 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5213 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5214 | Py_UNICODE ch = *s++; |
| 5215 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5216 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5217 | if (ch >= 0x10000) { |
| 5218 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5219 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5220 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5221 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5222 | STORECHAR(ch); |
| 5223 | if (ch2) |
| 5224 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5225 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5226 | |
| 5227 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5228 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5229 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5230 | } |
| 5231 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5232 | PyObject * |
| 5233 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5234 | { |
| 5235 | if (!PyUnicode_Check(unicode)) { |
| 5236 | PyErr_BadArgument(); |
| 5237 | return NULL; |
| 5238 | } |
| 5239 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5240 | PyUnicode_GET_SIZE(unicode), |
| 5241 | NULL, |
| 5242 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5246 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5247 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5248 | if all the escapes in the string make it still a valid ASCII string. |
| 5249 | Returns -1 if any escapes were found which cause the string to |
| 5250 | pop out of ASCII range. Otherwise returns the length of the |
| 5251 | required buffer to hold the string. |
| 5252 | */ |
| 5253 | Py_ssize_t |
| 5254 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5255 | { |
| 5256 | const unsigned char *p = (const unsigned char *)s; |
| 5257 | const unsigned char *end = p + size; |
| 5258 | Py_ssize_t length = 0; |
| 5259 | |
| 5260 | if (size < 0) |
| 5261 | return -1; |
| 5262 | |
| 5263 | for (; p < end; ++p) { |
| 5264 | if (*p > 127) { |
| 5265 | /* Non-ASCII */ |
| 5266 | return -1; |
| 5267 | } |
| 5268 | else if (*p != '\\') { |
| 5269 | /* Normal character */ |
| 5270 | ++length; |
| 5271 | } |
| 5272 | else { |
| 5273 | /* Backslash-escape, check next char */ |
| 5274 | ++p; |
| 5275 | /* Escape sequence reaches till end of string or |
| 5276 | non-ASCII follow-up. */ |
| 5277 | if (p >= end || *p > 127) |
| 5278 | return -1; |
| 5279 | switch (*p) { |
| 5280 | case '\n': |
| 5281 | /* backslash + \n result in zero characters */ |
| 5282 | break; |
| 5283 | case '\\': case '\'': case '\"': |
| 5284 | case 'b': case 'f': case 't': |
| 5285 | case 'n': case 'r': case 'v': case 'a': |
| 5286 | ++length; |
| 5287 | break; |
| 5288 | case '0': case '1': case '2': case '3': |
| 5289 | case '4': case '5': case '6': case '7': |
| 5290 | case 'x': case 'u': case 'U': case 'N': |
| 5291 | /* these do not guarantee ASCII characters */ |
| 5292 | return -1; |
| 5293 | default: |
| 5294 | /* count the backslash + the other character */ |
| 5295 | length += 2; |
| 5296 | } |
| 5297 | } |
| 5298 | } |
| 5299 | return length; |
| 5300 | } |
| 5301 | |
| 5302 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5303 | or treat string as ASCII. */ |
| 5304 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5305 | do { \ |
| 5306 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5307 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5308 | else \ |
| 5309 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5310 | } while (0) |
| 5311 | |
| 5312 | #define WRITE_WSTR(buf, index, value) \ |
| 5313 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5314 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5315 | |
| 5316 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5317 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5318 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5319 | PyObject * |
| 5320 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5321 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5322 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5323 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5324 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5325 | Py_ssize_t startinpos; |
| 5326 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5327 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5328 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5329 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5330 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5331 | char* message; |
| 5332 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5333 | PyObject *errorHandler = NULL; |
| 5334 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5335 | Py_ssize_t ascii_length; |
| 5336 | Py_ssize_t i; |
| 5337 | int kind; |
| 5338 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5339 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5340 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5341 | |
| 5342 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5343 | either the string is pure ASCII with named escapes like \n, etc. |
| 5344 | and we determined it's exact size (common case) |
| 5345 | or it contains \x, \u, ... escape sequences. then we create a |
| 5346 | legacy wchar string and resize it at the end of this function. */ |
| 5347 | if (ascii_length >= 0) { |
| 5348 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 5349 | if (!v) |
| 5350 | goto onError; |
| 5351 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5352 | kind = PyUnicode_1BYTE_KIND; |
| 5353 | data = PyUnicode_DATA(v); |
| 5354 | } |
| 5355 | else { |
| 5356 | /* Escaped strings will always be longer than the resulting |
| 5357 | Unicode string, so we start with size here and then reduce the |
| 5358 | length after conversion to the true value. |
| 5359 | (but if the error callback returns a long replacement string |
| 5360 | we'll have to allocate more space) */ |
| 5361 | v = _PyUnicode_New(size); |
| 5362 | if (!v) |
| 5363 | goto onError; |
| 5364 | kind = PyUnicode_WCHAR_KIND; |
| 5365 | data = PyUnicode_AS_UNICODE(v); |
| 5366 | } |
| 5367 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5368 | if (size == 0) |
| 5369 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5370 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5371 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5372 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5373 | while (s < end) { |
| 5374 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5375 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5376 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5377 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5378 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5379 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5380 | } |
| 5381 | else { |
| 5382 | /* The only case in which i == ascii_length is a backslash |
| 5383 | followed by a newline. */ |
| 5384 | assert(i <= ascii_length); |
| 5385 | } |
| 5386 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5388 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5389 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5390 | continue; |
| 5391 | } |
| 5392 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5393 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5394 | /* \ - Escapes */ |
| 5395 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5396 | c = *s++; |
| 5397 | if (s > end) |
| 5398 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5399 | |
| 5400 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5401 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5402 | } |
| 5403 | else { |
| 5404 | /* The only case in which i == ascii_length is a backslash |
| 5405 | followed by a newline. */ |
| 5406 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5407 | } |
| 5408 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5409 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5410 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5411 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5412 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5413 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5414 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5415 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5416 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5417 | /* FF */ |
| 5418 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5419 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5420 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5421 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5422 | /* VT */ |
| 5423 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5424 | /* BEL, not classic C */ |
| 5425 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5426 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5427 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5428 | case '0': case '1': case '2': case '3': |
| 5429 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5430 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5431 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5432 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5433 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5434 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5435 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5436 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5437 | break; |
| 5438 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5439 | /* hex escapes */ |
| 5440 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5442 | digits = 2; |
| 5443 | message = "truncated \\xXX escape"; |
| 5444 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5445 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5446 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5448 | digits = 4; |
| 5449 | message = "truncated \\uXXXX escape"; |
| 5450 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5451 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5452 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5453 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5454 | digits = 8; |
| 5455 | message = "truncated \\UXXXXXXXX escape"; |
| 5456 | hexescape: |
| 5457 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5458 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5459 | if (s+digits>end) { |
| 5460 | endinpos = size; |
| 5461 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5462 | errors, &errorHandler, |
| 5463 | "unicodeescape", "end of string in escape sequence", |
| 5464 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5465 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5466 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5467 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5468 | goto nextByte; |
| 5469 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5470 | for (j = 0; j < digits; ++j) { |
| 5471 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5472 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5473 | endinpos = (s+j+1)-starts; |
| 5474 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5475 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5476 | errors, &errorHandler, |
| 5477 | "unicodeescape", message, |
| 5478 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5479 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5480 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5481 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5482 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5483 | } |
| 5484 | chr = (chr<<4) & ~0xF; |
| 5485 | if (c >= '0' && c <= '9') |
| 5486 | chr += c - '0'; |
| 5487 | else if (c >= 'a' && c <= 'f') |
| 5488 | chr += 10 + c - 'a'; |
| 5489 | else |
| 5490 | chr += 10 + c - 'A'; |
| 5491 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5492 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5493 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5494 | /* _decoding_error will have already written into the |
| 5495 | target buffer. */ |
| 5496 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5497 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5498 | /* when we get here, chr is a 32-bit unicode character */ |
| 5499 | if (chr <= 0xffff) |
| 5500 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5501 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5502 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5503 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5504 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5505 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5506 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5507 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5508 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5509 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5510 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5511 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5512 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5513 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5514 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5515 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5516 | errors, &errorHandler, |
| 5517 | "unicodeescape", "illegal Unicode character", |
| 5518 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5519 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5520 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5521 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5522 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5523 | break; |
| 5524 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5525 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5526 | case 'N': |
| 5527 | message = "malformed \\N character escape"; |
| 5528 | if (ucnhash_CAPI == NULL) { |
| 5529 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5530 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5531 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5532 | if (ucnhash_CAPI == NULL) |
| 5533 | goto ucnhashError; |
| 5534 | } |
| 5535 | if (*s == '{') { |
| 5536 | const char *start = s+1; |
| 5537 | /* look for the closing brace */ |
| 5538 | while (*s != '}' && s < end) |
| 5539 | s++; |
| 5540 | if (s > start && s < end && *s == '}') { |
| 5541 | /* found a name. look it up in the unicode database */ |
| 5542 | message = "unknown Unicode character name"; |
| 5543 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5544 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5545 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5546 | goto store; |
| 5547 | } |
| 5548 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5549 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5550 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5551 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5552 | errors, &errorHandler, |
| 5553 | "unicodeescape", message, |
| 5554 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5555 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5556 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5557 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5558 | break; |
| 5559 | |
| 5560 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5561 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5562 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5563 | message = "\\ at end of string"; |
| 5564 | s--; |
| 5565 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5566 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5567 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5568 | errors, &errorHandler, |
| 5569 | "unicodeescape", message, |
| 5570 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5571 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5572 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5573 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5574 | } |
| 5575 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5576 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5577 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5578 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5579 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5580 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5581 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5582 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5583 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5584 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5585 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5586 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5587 | if (kind == PyUnicode_WCHAR_KIND) |
| 5588 | { |
| 5589 | if (PyUnicode_Resize((PyObject**)&v, i) < 0) |
| 5590 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5591 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5592 | Py_XDECREF(errorHandler); |
| 5593 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5594 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5595 | Py_DECREF(v); |
| 5596 | return NULL; |
| 5597 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5598 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5599 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5600 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5601 | PyErr_SetString( |
| 5602 | PyExc_UnicodeError, |
| 5603 | "\\N escapes not supported (can't load unicodedata module)" |
| 5604 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5605 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5606 | Py_XDECREF(errorHandler); |
| 5607 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5608 | return NULL; |
| 5609 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5610 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5611 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5612 | Py_XDECREF(errorHandler); |
| 5613 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5614 | return NULL; |
| 5615 | } |
| 5616 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5617 | #undef WRITE_ASCII_OR_WSTR |
| 5618 | #undef WRITE_WSTR |
| 5619 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5620 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5621 | |
| 5622 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5623 | appropriate. |
| 5624 | |
| 5625 | */ |
| 5626 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5627 | static const char *hexdigits = "0123456789abcdef"; |
| 5628 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5629 | PyObject * |
| 5630 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5631 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5632 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5633 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5634 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5635 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5636 | #ifdef Py_UNICODE_WIDE |
| 5637 | const Py_ssize_t expandsize = 10; |
| 5638 | #else |
| 5639 | const Py_ssize_t expandsize = 6; |
| 5640 | #endif |
| 5641 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5642 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5643 | better to choose a different scheme. Perhaps scan the |
| 5644 | first N-chars of the string and allocate based on that size. |
| 5645 | */ |
| 5646 | /* Initial allocation is based on the longest-possible unichr |
| 5647 | escape. |
| 5648 | |
| 5649 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5650 | unichr, so in this case it's the longest unichr escape. In |
| 5651 | narrow (UTF-16) builds this is five chars per source unichr |
| 5652 | since there are two unichrs in the surrogate pair, so in narrow |
| 5653 | (UTF-16) builds it's not the longest unichr escape. |
| 5654 | |
| 5655 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5656 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5657 | escape. |
| 5658 | */ |
| 5659 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5660 | if (size == 0) |
| 5661 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5662 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5663 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5664 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5665 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5666 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5667 | 2 |
| 5668 | + expandsize*size |
| 5669 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | if (repr == NULL) |
| 5671 | return NULL; |
| 5672 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5673 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5674 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | while (size-- > 0) { |
| 5676 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5677 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5678 | /* Escape backslashes */ |
| 5679 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5680 | *p++ = '\\'; |
| 5681 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5682 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5683 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5684 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5685 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5686 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5687 | else if (ch >= 0x10000) { |
| 5688 | *p++ = '\\'; |
| 5689 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5690 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5691 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5692 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5693 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5694 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5695 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5696 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5697 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5698 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5699 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5700 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5701 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5702 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5703 | Py_UNICODE ch2; |
| 5704 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5705 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5706 | ch2 = *s++; |
| 5707 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5708 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5709 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5710 | *p++ = '\\'; |
| 5711 | *p++ = 'U'; |
| 5712 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5713 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5714 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5715 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5716 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5717 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5718 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5719 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5720 | continue; |
| 5721 | } |
| 5722 | /* Fall through: isolated surrogates are copied as-is */ |
| 5723 | s--; |
| 5724 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5725 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5726 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5727 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5729 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5730 | *p++ = '\\'; |
| 5731 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5732 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5733 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5734 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5735 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5736 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5737 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5738 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5739 | else if (ch == '\t') { |
| 5740 | *p++ = '\\'; |
| 5741 | *p++ = 't'; |
| 5742 | } |
| 5743 | else if (ch == '\n') { |
| 5744 | *p++ = '\\'; |
| 5745 | *p++ = 'n'; |
| 5746 | } |
| 5747 | else if (ch == '\r') { |
| 5748 | *p++ = '\\'; |
| 5749 | *p++ = 'r'; |
| 5750 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5751 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5752 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5753 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5754 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5755 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5756 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5757 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5758 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5759 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5760 | /* Copy everything else as-is */ |
| 5761 | else |
| 5762 | *p++ = (char) ch; |
| 5763 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5765 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5766 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5767 | return NULL; |
| 5768 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5769 | } |
| 5770 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5771 | PyObject * |
| 5772 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5773 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5774 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5775 | if (!PyUnicode_Check(unicode)) { |
| 5776 | PyErr_BadArgument(); |
| 5777 | return NULL; |
| 5778 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5779 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5780 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5781 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5782 | } |
| 5783 | |
| 5784 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5785 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5786 | PyObject * |
| 5787 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5788 | Py_ssize_t size, |
| 5789 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5790 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5791 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5792 | Py_ssize_t startinpos; |
| 5793 | Py_ssize_t endinpos; |
| 5794 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5795 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5796 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5797 | const char *end; |
| 5798 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5799 | PyObject *errorHandler = NULL; |
| 5800 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5801 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5802 | /* Escaped strings will always be longer than the resulting |
| 5803 | 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] | 5804 | length after conversion to the true value. (But decoding error |
| 5805 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5806 | v = _PyUnicode_New(size); |
| 5807 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5808 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5809 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5810 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5811 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5812 | end = s + size; |
| 5813 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5814 | unsigned char c; |
| 5815 | Py_UCS4 x; |
| 5816 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5817 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5818 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5819 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5820 | if (*s != '\\') { |
| 5821 | *p++ = (unsigned char)*s++; |
| 5822 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5823 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5824 | startinpos = s-starts; |
| 5825 | |
| 5826 | /* \u-escapes are only interpreted iff the number of leading |
| 5827 | backslashes if odd */ |
| 5828 | bs = s; |
| 5829 | for (;s < end;) { |
| 5830 | if (*s != '\\') |
| 5831 | break; |
| 5832 | *p++ = (unsigned char)*s++; |
| 5833 | } |
| 5834 | if (((s - bs) & 1) == 0 || |
| 5835 | s >= end || |
| 5836 | (*s != 'u' && *s != 'U')) { |
| 5837 | continue; |
| 5838 | } |
| 5839 | p--; |
| 5840 | count = *s=='u' ? 4 : 8; |
| 5841 | s++; |
| 5842 | |
| 5843 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 5844 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5845 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5846 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5847 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5848 | endinpos = s-starts; |
| 5849 | if (unicode_decode_call_errorhandler( |
| 5850 | errors, &errorHandler, |
| 5851 | "rawunicodeescape", "truncated \\uXXXX", |
| 5852 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5853 | &v, &outpos, &p)) |
| 5854 | goto onError; |
| 5855 | goto nextByte; |
| 5856 | } |
| 5857 | x = (x<<4) & ~0xF; |
| 5858 | if (c >= '0' && c <= '9') |
| 5859 | x += c - '0'; |
| 5860 | else if (c >= 'a' && c <= 'f') |
| 5861 | x += 10 + c - 'a'; |
| 5862 | else |
| 5863 | x += 10 + c - 'A'; |
| 5864 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5865 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5866 | /* UCS-2 character */ |
| 5867 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5868 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5869 | /* UCS-4 character. Either store directly, or as |
| 5870 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5871 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5872 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5873 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5874 | x -= 0x10000L; |
| 5875 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 5876 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5877 | #endif |
| 5878 | } else { |
| 5879 | endinpos = s-starts; |
| 5880 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5881 | if (unicode_decode_call_errorhandler( |
| 5882 | errors, &errorHandler, |
| 5883 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5884 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5885 | &v, &outpos, &p)) |
| 5886 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5887 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5888 | nextByte: |
| 5889 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5891 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5892 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5893 | Py_XDECREF(errorHandler); |
| 5894 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5895 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5896 | Py_DECREF(v); |
| 5897 | return NULL; |
| 5898 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5899 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5900 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5901 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5902 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5903 | Py_XDECREF(errorHandler); |
| 5904 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5905 | return NULL; |
| 5906 | } |
| 5907 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5908 | PyObject * |
| 5909 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5910 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5911 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5912 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5913 | char *p; |
| 5914 | char *q; |
| 5915 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5916 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5917 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5918 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5919 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5920 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5921 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5922 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5923 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5924 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5925 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | if (repr == NULL) |
| 5927 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5928 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5929 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5930 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5931 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5932 | while (size-- > 0) { |
| 5933 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5934 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5935 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5936 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5937 | *p++ = '\\'; |
| 5938 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5939 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 5940 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 5941 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 5942 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 5943 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5944 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5945 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5946 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5947 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5948 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5949 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5950 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5951 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 5952 | Py_UNICODE ch2; |
| 5953 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5954 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5955 | ch2 = *s++; |
| 5956 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5957 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5958 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5959 | *p++ = '\\'; |
| 5960 | *p++ = 'U'; |
| 5961 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 5962 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 5963 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 5964 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 5965 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 5966 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 5967 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 5968 | *p++ = hexdigits[ucs & 0xf]; |
| 5969 | continue; |
| 5970 | } |
| 5971 | /* Fall through: isolated surrogates are copied as-is */ |
| 5972 | s--; |
| 5973 | size++; |
| 5974 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5975 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5976 | /* Map 16-bit characters to '\uxxxx' */ |
| 5977 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 | *p++ = '\\'; |
| 5979 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5980 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5981 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5982 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5983 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5985 | /* Copy everything else as-is */ |
| 5986 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | *p++ = (char) ch; |
| 5988 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5989 | size = p - q; |
| 5990 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5991 | assert(size > 0); |
| 5992 | if (_PyBytes_Resize(&repr, size) < 0) |
| 5993 | return NULL; |
| 5994 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5995 | } |
| 5996 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5997 | PyObject * |
| 5998 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5999 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6000 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6001 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6002 | PyErr_BadArgument(); |
| 6003 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6004 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 6005 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 6006 | PyUnicode_GET_SIZE(unicode)); |
| 6007 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 6008 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | } |
| 6010 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6011 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 6012 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6013 | PyObject * |
| 6014 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6015 | Py_ssize_t size, |
| 6016 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6017 | { |
| 6018 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6019 | Py_ssize_t startinpos; |
| 6020 | Py_ssize_t endinpos; |
| 6021 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6022 | PyUnicodeObject *v; |
| 6023 | Py_UNICODE *p; |
| 6024 | const char *end; |
| 6025 | const char *reason; |
| 6026 | PyObject *errorHandler = NULL; |
| 6027 | PyObject *exc = NULL; |
| 6028 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 6029 | #ifdef Py_UNICODE_WIDE |
| 6030 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 6031 | #endif |
| 6032 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6033 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6034 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 6035 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6036 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6037 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 6038 | as string was created with the old API. */ |
| 6039 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6040 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6041 | p = PyUnicode_AS_UNICODE(v); |
| 6042 | end = s + size; |
| 6043 | |
| 6044 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6045 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6046 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6047 | some malformed UCS-4 data. */ |
| 6048 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6049 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6050 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6051 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6052 | end-s < Py_UNICODE_SIZE |
| 6053 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6054 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6055 | startinpos = s - starts; |
| 6056 | if (end-s < Py_UNICODE_SIZE) { |
| 6057 | endinpos = end-starts; |
| 6058 | reason = "truncated input"; |
| 6059 | } |
| 6060 | else { |
| 6061 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6062 | reason = "illegal code point (> 0x10FFFF)"; |
| 6063 | } |
| 6064 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6065 | if (unicode_decode_call_errorhandler( |
| 6066 | errors, &errorHandler, |
| 6067 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6068 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6069 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6070 | goto onError; |
| 6071 | } |
| 6072 | } |
| 6073 | else { |
| 6074 | p++; |
| 6075 | s += Py_UNICODE_SIZE; |
| 6076 | } |
| 6077 | } |
| 6078 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6079 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6080 | goto onError; |
| 6081 | Py_XDECREF(errorHandler); |
| 6082 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6083 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6084 | Py_DECREF(v); |
| 6085 | return NULL; |
| 6086 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6087 | return (PyObject *)v; |
| 6088 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6089 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6090 | Py_XDECREF(v); |
| 6091 | Py_XDECREF(errorHandler); |
| 6092 | Py_XDECREF(exc); |
| 6093 | return NULL; |
| 6094 | } |
| 6095 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6097 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6098 | PyObject * |
| 6099 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6100 | Py_ssize_t size, |
| 6101 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6102 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6103 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6104 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6105 | } |
| 6106 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6107 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6108 | static void |
| 6109 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6110 | const char *encoding, |
| 6111 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6112 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6113 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6115 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6116 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6117 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6118 | } |
| 6119 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6120 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6121 | goto onError; |
| 6122 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6123 | goto onError; |
| 6124 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6125 | goto onError; |
| 6126 | return; |
| 6127 | onError: |
| 6128 | Py_DECREF(*exceptionObject); |
| 6129 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6130 | } |
| 6131 | } |
| 6132 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6133 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6134 | static void |
| 6135 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6136 | const char *encoding, |
| 6137 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6138 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6139 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6140 | { |
| 6141 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6142 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6143 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6144 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6145 | } |
| 6146 | |
| 6147 | /* error handling callback helper: |
| 6148 | build arguments, call the callback and check the arguments, |
| 6149 | put the result into newpos and return the replacement string, which |
| 6150 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6151 | static PyObject * |
| 6152 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6153 | PyObject **errorHandler, |
| 6154 | const char *encoding, const char *reason, |
| 6155 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 6156 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6157 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6158 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6159 | 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] | 6160 | |
| 6161 | PyObject *restuple; |
| 6162 | PyObject *resunicode; |
| 6163 | |
| 6164 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6165 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6166 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6168 | } |
| 6169 | |
| 6170 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6171 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6172 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6173 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6174 | |
| 6175 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6176 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6177 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6178 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6179 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6180 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6181 | Py_DECREF(restuple); |
| 6182 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6183 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6184 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6185 | &resunicode, newpos)) { |
| 6186 | Py_DECREF(restuple); |
| 6187 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6188 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6189 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6190 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6191 | Py_DECREF(restuple); |
| 6192 | return NULL; |
| 6193 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6194 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6195 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6196 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6197 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6198 | Py_DECREF(restuple); |
| 6199 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6200 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6201 | Py_INCREF(resunicode); |
| 6202 | Py_DECREF(restuple); |
| 6203 | return resunicode; |
| 6204 | } |
| 6205 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6206 | static PyObject * |
| 6207 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6208 | Py_ssize_t size, |
| 6209 | const char *errors, |
| 6210 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6211 | { |
| 6212 | /* output object */ |
| 6213 | PyObject *res; |
| 6214 | /* pointers to the beginning and end+1 of input */ |
| 6215 | const Py_UNICODE *startp = p; |
| 6216 | const Py_UNICODE *endp = p + size; |
| 6217 | /* pointer to the beginning of the unencodable characters */ |
| 6218 | /* const Py_UNICODE *badp = NULL; */ |
| 6219 | /* pointer into the output */ |
| 6220 | char *str; |
| 6221 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6222 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6223 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6224 | 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] | 6225 | PyObject *errorHandler = NULL; |
| 6226 | PyObject *exc = NULL; |
| 6227 | /* the following variable is used for caching string comparisons |
| 6228 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6229 | int known_errorHandler = -1; |
| 6230 | |
| 6231 | /* allocate enough for a simple encoding without |
| 6232 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6233 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6234 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6235 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6236 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6237 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6238 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6239 | ressize = size; |
| 6240 | |
| 6241 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6242 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6243 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6244 | /* can we encode this? */ |
| 6245 | if (c<limit) { |
| 6246 | /* no overflow check, because we know that the space is enough */ |
| 6247 | *str++ = (char)c; |
| 6248 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6249 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6250 | else { |
| 6251 | Py_ssize_t unicodepos = p-startp; |
| 6252 | Py_ssize_t requiredsize; |
| 6253 | PyObject *repunicode; |
| 6254 | Py_ssize_t repsize; |
| 6255 | Py_ssize_t newpos; |
| 6256 | Py_ssize_t respos; |
| 6257 | Py_UNICODE *uni2; |
| 6258 | /* startpos for collecting unencodable chars */ |
| 6259 | const Py_UNICODE *collstart = p; |
| 6260 | const Py_UNICODE *collend = p; |
| 6261 | /* find all unecodable characters */ |
| 6262 | while ((collend < endp) && ((*collend)>=limit)) |
| 6263 | ++collend; |
| 6264 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6265 | if (known_errorHandler==-1) { |
| 6266 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6267 | known_errorHandler = 1; |
| 6268 | else if (!strcmp(errors, "replace")) |
| 6269 | known_errorHandler = 2; |
| 6270 | else if (!strcmp(errors, "ignore")) |
| 6271 | known_errorHandler = 3; |
| 6272 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6273 | known_errorHandler = 4; |
| 6274 | else |
| 6275 | known_errorHandler = 0; |
| 6276 | } |
| 6277 | switch (known_errorHandler) { |
| 6278 | case 1: /* strict */ |
| 6279 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 6280 | goto onError; |
| 6281 | case 2: /* replace */ |
| 6282 | while (collstart++<collend) |
| 6283 | *str++ = '?'; /* fall through */ |
| 6284 | case 3: /* ignore */ |
| 6285 | p = collend; |
| 6286 | break; |
| 6287 | case 4: /* xmlcharrefreplace */ |
| 6288 | respos = str - PyBytes_AS_STRING(res); |
| 6289 | /* determine replacement size (temporarily (mis)uses p) */ |
| 6290 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 6291 | if (*p<10) |
| 6292 | repsize += 2+1+1; |
| 6293 | else if (*p<100) |
| 6294 | repsize += 2+2+1; |
| 6295 | else if (*p<1000) |
| 6296 | repsize += 2+3+1; |
| 6297 | else if (*p<10000) |
| 6298 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6299 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6300 | else |
| 6301 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6302 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6303 | else if (*p<100000) |
| 6304 | repsize += 2+5+1; |
| 6305 | else if (*p<1000000) |
| 6306 | repsize += 2+6+1; |
| 6307 | else |
| 6308 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6309 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6310 | } |
| 6311 | requiredsize = respos+repsize+(endp-collend); |
| 6312 | if (requiredsize > ressize) { |
| 6313 | if (requiredsize<2*ressize) |
| 6314 | requiredsize = 2*ressize; |
| 6315 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6316 | goto onError; |
| 6317 | str = PyBytes_AS_STRING(res) + respos; |
| 6318 | ressize = requiredsize; |
| 6319 | } |
| 6320 | /* generate replacement (temporarily (mis)uses p) */ |
| 6321 | for (p = collstart; p < collend; ++p) { |
| 6322 | str += sprintf(str, "&#%d;", (int)*p); |
| 6323 | } |
| 6324 | p = collend; |
| 6325 | break; |
| 6326 | default: |
| 6327 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6328 | encoding, reason, startp, size, &exc, |
| 6329 | collstart-startp, collend-startp, &newpos); |
| 6330 | if (repunicode == NULL) |
| 6331 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6332 | if (PyBytes_Check(repunicode)) { |
| 6333 | /* Directly copy bytes result to output. */ |
| 6334 | repsize = PyBytes_Size(repunicode); |
| 6335 | if (repsize > 1) { |
| 6336 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6337 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6338 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6339 | Py_DECREF(repunicode); |
| 6340 | goto onError; |
| 6341 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6342 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6343 | ressize += repsize-1; |
| 6344 | } |
| 6345 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6346 | str += repsize; |
| 6347 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6348 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6349 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6350 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6351 | /* need more space? (at least enough for what we |
| 6352 | have+the replacement+the rest of the string, so |
| 6353 | we won't have to check space for encodable characters) */ |
| 6354 | respos = str - PyBytes_AS_STRING(res); |
| 6355 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6356 | requiredsize = respos+repsize+(endp-collend); |
| 6357 | if (requiredsize > ressize) { |
| 6358 | if (requiredsize<2*ressize) |
| 6359 | requiredsize = 2*ressize; |
| 6360 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6361 | Py_DECREF(repunicode); |
| 6362 | goto onError; |
| 6363 | } |
| 6364 | str = PyBytes_AS_STRING(res) + respos; |
| 6365 | ressize = requiredsize; |
| 6366 | } |
| 6367 | /* check if there is anything unencodable in the replacement |
| 6368 | and copy it to the output */ |
| 6369 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6370 | c = *uni2; |
| 6371 | if (c >= limit) { |
| 6372 | raise_encode_exception(&exc, encoding, startp, size, |
| 6373 | unicodepos, unicodepos+1, reason); |
| 6374 | Py_DECREF(repunicode); |
| 6375 | goto onError; |
| 6376 | } |
| 6377 | *str = (char)c; |
| 6378 | } |
| 6379 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6380 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6381 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6382 | } |
| 6383 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6384 | /* Resize if we allocated to much */ |
| 6385 | size = str - PyBytes_AS_STRING(res); |
| 6386 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6387 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6388 | if (_PyBytes_Resize(&res, size) < 0) |
| 6389 | goto onError; |
| 6390 | } |
| 6391 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6392 | Py_XDECREF(errorHandler); |
| 6393 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6394 | return res; |
| 6395 | |
| 6396 | onError: |
| 6397 | Py_XDECREF(res); |
| 6398 | Py_XDECREF(errorHandler); |
| 6399 | Py_XDECREF(exc); |
| 6400 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6403 | PyObject * |
| 6404 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6405 | Py_ssize_t size, |
| 6406 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6407 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6408 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6409 | } |
| 6410 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6411 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6412 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6413 | { |
| 6414 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6415 | PyErr_BadArgument(); |
| 6416 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6417 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6418 | if (PyUnicode_READY(unicode) == -1) |
| 6419 | return NULL; |
| 6420 | /* Fast path: if it is a one-byte string, construct |
| 6421 | bytes object directly. */ |
| 6422 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6423 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6424 | PyUnicode_GET_LENGTH(unicode)); |
| 6425 | /* Non-Latin-1 characters present. Defer to above function to |
| 6426 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6427 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6428 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6429 | errors); |
| 6430 | } |
| 6431 | |
| 6432 | PyObject* |
| 6433 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6434 | { |
| 6435 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6436 | } |
| 6437 | |
| 6438 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6439 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6440 | PyObject * |
| 6441 | PyUnicode_DecodeASCII(const char *s, |
| 6442 | Py_ssize_t size, |
| 6443 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6445 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6446 | PyUnicodeObject *v; |
| 6447 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6448 | Py_ssize_t startinpos; |
| 6449 | Py_ssize_t endinpos; |
| 6450 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6451 | const char *e; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6452 | unsigned char* d; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6453 | PyObject *errorHandler = NULL; |
| 6454 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6455 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6457 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6458 | if (size == 1 && *(unsigned char*)s < 128) |
| 6459 | return PyUnicode_FromOrdinal(*(unsigned char*)s); |
| 6460 | |
| 6461 | /* Fast path. Assume the input actually *is* ASCII, and allocate |
| 6462 | a single-block Unicode object with that assumption. If there is |
| 6463 | an error, drop the object and start over. */ |
| 6464 | v = (PyUnicodeObject*)PyUnicode_New(size, 127); |
| 6465 | if (v == NULL) |
| 6466 | goto onError; |
| 6467 | d = PyUnicode_1BYTE_DATA(v); |
| 6468 | for (i = 0; i < size; i++) { |
| 6469 | unsigned char ch = ((unsigned char*)s)[i]; |
| 6470 | if (ch < 128) |
| 6471 | d[i] = ch; |
| 6472 | else |
| 6473 | break; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6474 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6475 | if (i == size) |
| 6476 | return (PyObject*)v; |
| 6477 | Py_DECREF(v); /* start over */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6478 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | v = _PyUnicode_New(size); |
| 6480 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6481 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6482 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6483 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6484 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6485 | e = s + size; |
| 6486 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6487 | register unsigned char c = (unsigned char)*s; |
| 6488 | if (c < 128) { |
| 6489 | *p++ = c; |
| 6490 | ++s; |
| 6491 | } |
| 6492 | else { |
| 6493 | startinpos = s-starts; |
| 6494 | endinpos = startinpos + 1; |
| 6495 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 6496 | if (unicode_decode_call_errorhandler( |
| 6497 | errors, &errorHandler, |
| 6498 | "ascii", "ordinal not in range(128)", |
| 6499 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6500 | &v, &outpos, &p)) |
| 6501 | goto onError; |
| 6502 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6503 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6504 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6505 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6506 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6507 | Py_XDECREF(errorHandler); |
| 6508 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6509 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6510 | Py_DECREF(v); |
| 6511 | return NULL; |
| 6512 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6513 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6514 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6515 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6516 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6517 | Py_XDECREF(errorHandler); |
| 6518 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6519 | return NULL; |
| 6520 | } |
| 6521 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6522 | PyObject * |
| 6523 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6524 | Py_ssize_t size, |
| 6525 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6526 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6527 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6528 | } |
| 6529 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6530 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6531 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6532 | { |
| 6533 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6534 | PyErr_BadArgument(); |
| 6535 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6536 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6537 | if (PyUnicode_READY(unicode) == -1) |
| 6538 | return NULL; |
| 6539 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6540 | directly. Else defer to above function to raise the exception. */ |
| 6541 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6542 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6543 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6544 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6545 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6546 | errors); |
| 6547 | } |
| 6548 | |
| 6549 | PyObject * |
| 6550 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6551 | { |
| 6552 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6553 | } |
| 6554 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6555 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6556 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6557 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6558 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6559 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6560 | #define NEED_RETRY |
| 6561 | #endif |
| 6562 | |
| 6563 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6564 | a) it assumes an incomplete character consists of a single byte, and |
| 6565 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6566 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6567 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6568 | static int |
| 6569 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6570 | { |
| 6571 | const char *curr = s + offset; |
| 6572 | |
| 6573 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6574 | const char *prev = CharPrev(s, curr); |
| 6575 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6576 | } |
| 6577 | return 0; |
| 6578 | } |
| 6579 | |
| 6580 | /* |
| 6581 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6582 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6583 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6584 | static int |
| 6585 | decode_mbcs(PyUnicodeObject **v, |
| 6586 | const char *s, /* MBCS string */ |
| 6587 | int size, /* sizeof MBCS string */ |
| 6588 | int final, |
| 6589 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6590 | { |
| 6591 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6592 | Py_ssize_t n; |
| 6593 | DWORD usize; |
| 6594 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6595 | |
| 6596 | assert(size >= 0); |
| 6597 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6598 | /* check and handle 'errors' arg */ |
| 6599 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6600 | flags = MB_ERR_INVALID_CHARS; |
| 6601 | else if (strcmp(errors, "ignore")==0) |
| 6602 | flags = 0; |
| 6603 | else { |
| 6604 | PyErr_Format(PyExc_ValueError, |
| 6605 | "mbcs encoding does not support errors='%s'", |
| 6606 | errors); |
| 6607 | return -1; |
| 6608 | } |
| 6609 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6610 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6611 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6612 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6613 | |
| 6614 | /* First get the size of the result */ |
| 6615 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6616 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6617 | if (usize==0) |
| 6618 | goto mbcs_decode_error; |
| 6619 | } else |
| 6620 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6621 | |
| 6622 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6623 | /* Create unicode object */ |
| 6624 | *v = _PyUnicode_New(usize); |
| 6625 | if (*v == NULL) |
| 6626 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6627 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6628 | } |
| 6629 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6630 | /* Extend unicode object */ |
| 6631 | n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 6632 | if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6633 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6634 | } |
| 6635 | |
| 6636 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6637 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6638 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6639 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6640 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6641 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6642 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6643 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6644 | |
| 6645 | mbcs_decode_error: |
| 6646 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6647 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6648 | windows error |
| 6649 | */ |
| 6650 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6651 | /* Ideally, we should get reason from FormatMessage - this |
| 6652 | is the Windows 2000 English version of the message |
| 6653 | */ |
| 6654 | PyObject *exc = NULL; |
| 6655 | const char *reason = "No mapping for the Unicode character exists " |
| 6656 | "in the target multi-byte code page."; |
| 6657 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6658 | if (exc != NULL) { |
| 6659 | PyCodec_StrictErrors(exc); |
| 6660 | Py_DECREF(exc); |
| 6661 | } |
| 6662 | } else { |
| 6663 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6664 | } |
| 6665 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6666 | } |
| 6667 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6668 | PyObject * |
| 6669 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6670 | Py_ssize_t size, |
| 6671 | const char *errors, |
| 6672 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6673 | { |
| 6674 | PyUnicodeObject *v = NULL; |
| 6675 | int done; |
| 6676 | |
| 6677 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6678 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6679 | |
| 6680 | #ifdef NEED_RETRY |
| 6681 | retry: |
| 6682 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6683 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6684 | else |
| 6685 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6686 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6687 | |
| 6688 | if (done < 0) { |
| 6689 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6690 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6691 | } |
| 6692 | |
| 6693 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6694 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6695 | |
| 6696 | #ifdef NEED_RETRY |
| 6697 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6698 | s += done; |
| 6699 | size -= done; |
| 6700 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6701 | } |
| 6702 | #endif |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6703 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6704 | Py_DECREF(v); |
| 6705 | return NULL; |
| 6706 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6707 | return (PyObject *)v; |
| 6708 | } |
| 6709 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6710 | PyObject * |
| 6711 | PyUnicode_DecodeMBCS(const char *s, |
| 6712 | Py_ssize_t size, |
| 6713 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6714 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6715 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6716 | } |
| 6717 | |
| 6718 | /* |
| 6719 | * Convert unicode into string object (MBCS). |
| 6720 | * Returns 0 if succeed, -1 otherwise. |
| 6721 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6722 | static int |
| 6723 | encode_mbcs(PyObject **repr, |
| 6724 | const Py_UNICODE *p, /* unicode */ |
| 6725 | int size, /* size of unicode */ |
| 6726 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6727 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6728 | BOOL usedDefaultChar = FALSE; |
| 6729 | BOOL *pusedDefaultChar; |
| 6730 | int mbcssize; |
| 6731 | Py_ssize_t n; |
| 6732 | PyObject *exc = NULL; |
| 6733 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6734 | |
| 6735 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6736 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6737 | /* check and handle 'errors' arg */ |
| 6738 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 6739 | flags = WC_NO_BEST_FIT_CHARS; |
| 6740 | pusedDefaultChar = &usedDefaultChar; |
| 6741 | } else if (strcmp(errors, "replace")==0) { |
| 6742 | flags = 0; |
| 6743 | pusedDefaultChar = NULL; |
| 6744 | } else { |
| 6745 | PyErr_Format(PyExc_ValueError, |
| 6746 | "mbcs encoding does not support errors='%s'", |
| 6747 | errors); |
| 6748 | return -1; |
| 6749 | } |
| 6750 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6751 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6752 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6753 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 6754 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6755 | if (mbcssize == 0) { |
| 6756 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6757 | return -1; |
| 6758 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6759 | /* If we used a default char, then we failed! */ |
| 6760 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6761 | goto mbcs_encode_error; |
| 6762 | } else { |
| 6763 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6764 | } |
| 6765 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6766 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6767 | /* Create string object */ |
| 6768 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 6769 | if (*repr == NULL) |
| 6770 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6771 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6772 | } |
| 6773 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6774 | /* Extend string object */ |
| 6775 | n = PyBytes_Size(*repr); |
| 6776 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 6777 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6778 | } |
| 6779 | |
| 6780 | /* Do the conversion */ |
| 6781 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6782 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6783 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 6784 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6785 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6786 | return -1; |
| 6787 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6788 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6789 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6790 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6791 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6792 | |
| 6793 | mbcs_encode_error: |
| 6794 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 6795 | Py_XDECREF(exc); |
| 6796 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6797 | } |
| 6798 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6799 | PyObject * |
| 6800 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 6801 | Py_ssize_t size, |
| 6802 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6803 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6804 | PyObject *repr = NULL; |
| 6805 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 6806 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6807 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6808 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6809 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6810 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6811 | else |
| 6812 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6813 | ret = encode_mbcs(&repr, p, (int)size, errors); |
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 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6816 | Py_XDECREF(repr); |
| 6817 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6818 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6819 | |
| 6820 | #ifdef NEED_RETRY |
| 6821 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6822 | p += INT_MAX; |
| 6823 | size -= INT_MAX; |
| 6824 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6825 | } |
| 6826 | #endif |
| 6827 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6828 | return repr; |
| 6829 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6830 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6831 | PyObject * |
| 6832 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6833 | { |
| 6834 | if (!PyUnicode_Check(unicode)) { |
| 6835 | PyErr_BadArgument(); |
| 6836 | return NULL; |
| 6837 | } |
| 6838 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6839 | PyUnicode_GET_SIZE(unicode), |
| 6840 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6841 | } |
| 6842 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6843 | #undef NEED_RETRY |
| 6844 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6845 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6846 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6847 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 6848 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6849 | PyObject * |
| 6850 | PyUnicode_DecodeCharmap(const char *s, |
| 6851 | Py_ssize_t size, |
| 6852 | PyObject *mapping, |
| 6853 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6854 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6855 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6856 | Py_ssize_t startinpos; |
| 6857 | Py_ssize_t endinpos; |
| 6858 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6859 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6860 | PyUnicodeObject *v; |
| 6861 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6862 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6863 | PyObject *errorHandler = NULL; |
| 6864 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6865 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6866 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6867 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6868 | /* Default to Latin-1 */ |
| 6869 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6870 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6871 | |
| 6872 | v = _PyUnicode_New(size); |
| 6873 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6874 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6875 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6876 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6877 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6878 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6879 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6880 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 6881 | maplen = PyUnicode_GET_SIZE(mapping); |
| 6882 | while (s < e) { |
| 6883 | unsigned char ch = *s; |
| 6884 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6885 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6886 | if (ch < maplen) |
| 6887 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6888 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6889 | if (x == 0xfffe) { |
| 6890 | /* undefined mapping */ |
| 6891 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6892 | startinpos = s-starts; |
| 6893 | endinpos = startinpos+1; |
| 6894 | if (unicode_decode_call_errorhandler( |
| 6895 | errors, &errorHandler, |
| 6896 | "charmap", "character maps to <undefined>", |
| 6897 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6898 | &v, &outpos, &p)) { |
| 6899 | goto onError; |
| 6900 | } |
| 6901 | continue; |
| 6902 | } |
| 6903 | *p++ = x; |
| 6904 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6905 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6906 | } |
| 6907 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6908 | while (s < e) { |
| 6909 | unsigned char ch = *s; |
| 6910 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6911 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6912 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 6913 | w = PyLong_FromLong((long)ch); |
| 6914 | if (w == NULL) |
| 6915 | goto onError; |
| 6916 | x = PyObject_GetItem(mapping, w); |
| 6917 | Py_DECREF(w); |
| 6918 | if (x == NULL) { |
| 6919 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6920 | /* No mapping found means: mapping is undefined. */ |
| 6921 | PyErr_Clear(); |
| 6922 | x = Py_None; |
| 6923 | Py_INCREF(x); |
| 6924 | } else |
| 6925 | goto onError; |
| 6926 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6927 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6928 | /* Apply mapping */ |
| 6929 | if (PyLong_Check(x)) { |
| 6930 | long value = PyLong_AS_LONG(x); |
| 6931 | if (value < 0 || value > 65535) { |
| 6932 | PyErr_SetString(PyExc_TypeError, |
| 6933 | "character mapping must be in range(65536)"); |
| 6934 | Py_DECREF(x); |
| 6935 | goto onError; |
| 6936 | } |
| 6937 | *p++ = (Py_UNICODE)value; |
| 6938 | } |
| 6939 | else if (x == Py_None) { |
| 6940 | /* undefined mapping */ |
| 6941 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6942 | startinpos = s-starts; |
| 6943 | endinpos = startinpos+1; |
| 6944 | if (unicode_decode_call_errorhandler( |
| 6945 | errors, &errorHandler, |
| 6946 | "charmap", "character maps to <undefined>", |
| 6947 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6948 | &v, &outpos, &p)) { |
| 6949 | Py_DECREF(x); |
| 6950 | goto onError; |
| 6951 | } |
| 6952 | Py_DECREF(x); |
| 6953 | continue; |
| 6954 | } |
| 6955 | else if (PyUnicode_Check(x)) { |
| 6956 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(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 | if (targetsize == 1) |
| 6959 | /* 1-1 mapping */ |
| 6960 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6961 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6962 | else if (targetsize > 1) { |
| 6963 | /* 1-n mapping */ |
| 6964 | if (targetsize > extrachars) { |
| 6965 | /* resize first */ |
| 6966 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 6967 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 6968 | (targetsize << 2); |
| 6969 | extrachars += needed; |
| 6970 | /* XXX overflow detection missing */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6971 | if (PyUnicode_Resize((PyObject**)&v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6972 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 6973 | Py_DECREF(x); |
| 6974 | goto onError; |
| 6975 | } |
| 6976 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 6977 | } |
| 6978 | Py_UNICODE_COPY(p, |
| 6979 | PyUnicode_AS_UNICODE(x), |
| 6980 | targetsize); |
| 6981 | p += targetsize; |
| 6982 | extrachars -= targetsize; |
| 6983 | } |
| 6984 | /* 1-0 mapping: skip the character */ |
| 6985 | } |
| 6986 | else { |
| 6987 | /* wrong return value */ |
| 6988 | PyErr_SetString(PyExc_TypeError, |
| 6989 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6990 | Py_DECREF(x); |
| 6991 | goto onError; |
| 6992 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6993 | Py_DECREF(x); |
| 6994 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6995 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6996 | } |
| 6997 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6998 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6999 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7000 | Py_XDECREF(errorHandler); |
| 7001 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7002 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7003 | Py_DECREF(v); |
| 7004 | return NULL; |
| 7005 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7006 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7007 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7008 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7009 | Py_XDECREF(errorHandler); |
| 7010 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7011 | Py_XDECREF(v); |
| 7012 | return NULL; |
| 7013 | } |
| 7014 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7015 | /* Charmap encoding: the lookup table */ |
| 7016 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7017 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7018 | PyObject_HEAD |
| 7019 | unsigned char level1[32]; |
| 7020 | int count2, count3; |
| 7021 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7022 | }; |
| 7023 | |
| 7024 | static PyObject* |
| 7025 | encoding_map_size(PyObject *obj, PyObject* args) |
| 7026 | { |
| 7027 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7028 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7029 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7030 | } |
| 7031 | |
| 7032 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7033 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7034 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 7035 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7036 | }; |
| 7037 | |
| 7038 | static void |
| 7039 | encoding_map_dealloc(PyObject* o) |
| 7040 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7041 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7042 | } |
| 7043 | |
| 7044 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7045 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7046 | "EncodingMap", /*tp_name*/ |
| 7047 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7048 | 0, /*tp_itemsize*/ |
| 7049 | /* methods */ |
| 7050 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7051 | 0, /*tp_print*/ |
| 7052 | 0, /*tp_getattr*/ |
| 7053 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7054 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7055 | 0, /*tp_repr*/ |
| 7056 | 0, /*tp_as_number*/ |
| 7057 | 0, /*tp_as_sequence*/ |
| 7058 | 0, /*tp_as_mapping*/ |
| 7059 | 0, /*tp_hash*/ |
| 7060 | 0, /*tp_call*/ |
| 7061 | 0, /*tp_str*/ |
| 7062 | 0, /*tp_getattro*/ |
| 7063 | 0, /*tp_setattro*/ |
| 7064 | 0, /*tp_as_buffer*/ |
| 7065 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7066 | 0, /*tp_doc*/ |
| 7067 | 0, /*tp_traverse*/ |
| 7068 | 0, /*tp_clear*/ |
| 7069 | 0, /*tp_richcompare*/ |
| 7070 | 0, /*tp_weaklistoffset*/ |
| 7071 | 0, /*tp_iter*/ |
| 7072 | 0, /*tp_iternext*/ |
| 7073 | encoding_map_methods, /*tp_methods*/ |
| 7074 | 0, /*tp_members*/ |
| 7075 | 0, /*tp_getset*/ |
| 7076 | 0, /*tp_base*/ |
| 7077 | 0, /*tp_dict*/ |
| 7078 | 0, /*tp_descr_get*/ |
| 7079 | 0, /*tp_descr_set*/ |
| 7080 | 0, /*tp_dictoffset*/ |
| 7081 | 0, /*tp_init*/ |
| 7082 | 0, /*tp_alloc*/ |
| 7083 | 0, /*tp_new*/ |
| 7084 | 0, /*tp_free*/ |
| 7085 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7086 | }; |
| 7087 | |
| 7088 | PyObject* |
| 7089 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7090 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7091 | PyObject *result; |
| 7092 | struct encoding_map *mresult; |
| 7093 | int i; |
| 7094 | int need_dict = 0; |
| 7095 | unsigned char level1[32]; |
| 7096 | unsigned char level2[512]; |
| 7097 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7098 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7099 | int kind; |
| 7100 | void *data; |
| 7101 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7102 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7103 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7104 | PyErr_BadArgument(); |
| 7105 | return NULL; |
| 7106 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7107 | kind = PyUnicode_KIND(string); |
| 7108 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7109 | memset(level1, 0xFF, sizeof level1); |
| 7110 | memset(level2, 0xFF, sizeof level2); |
| 7111 | |
| 7112 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7113 | or if there are non-BMP characters, we need to use |
| 7114 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7115 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7116 | need_dict = 1; |
| 7117 | for (i = 1; i < 256; i++) { |
| 7118 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7119 | ch = PyUnicode_READ(kind, data, i); |
| 7120 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7121 | need_dict = 1; |
| 7122 | break; |
| 7123 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7124 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7125 | /* unmapped character */ |
| 7126 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7127 | l1 = ch >> 11; |
| 7128 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7129 | if (level1[l1] == 0xFF) |
| 7130 | level1[l1] = count2++; |
| 7131 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7132 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
| 7135 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7136 | need_dict = 1; |
| 7137 | |
| 7138 | if (need_dict) { |
| 7139 | PyObject *result = PyDict_New(); |
| 7140 | PyObject *key, *value; |
| 7141 | if (!result) |
| 7142 | return NULL; |
| 7143 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7144 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7145 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7146 | if (!key || !value) |
| 7147 | goto failed1; |
| 7148 | if (PyDict_SetItem(result, key, value) == -1) |
| 7149 | goto failed1; |
| 7150 | Py_DECREF(key); |
| 7151 | Py_DECREF(value); |
| 7152 | } |
| 7153 | return result; |
| 7154 | failed1: |
| 7155 | Py_XDECREF(key); |
| 7156 | Py_XDECREF(value); |
| 7157 | Py_DECREF(result); |
| 7158 | return NULL; |
| 7159 | } |
| 7160 | |
| 7161 | /* Create a three-level trie */ |
| 7162 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7163 | 16*count2 + 128*count3 - 1); |
| 7164 | if (!result) |
| 7165 | return PyErr_NoMemory(); |
| 7166 | PyObject_Init(result, &EncodingMapType); |
| 7167 | mresult = (struct encoding_map*)result; |
| 7168 | mresult->count2 = count2; |
| 7169 | mresult->count3 = count3; |
| 7170 | mlevel1 = mresult->level1; |
| 7171 | mlevel2 = mresult->level23; |
| 7172 | mlevel3 = mresult->level23 + 16*count2; |
| 7173 | memcpy(mlevel1, level1, 32); |
| 7174 | memset(mlevel2, 0xFF, 16*count2); |
| 7175 | memset(mlevel3, 0, 128*count3); |
| 7176 | count3 = 0; |
| 7177 | for (i = 1; i < 256; i++) { |
| 7178 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7179 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7180 | /* unmapped character */ |
| 7181 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7182 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7183 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7184 | i2 = 16*mlevel1[o1] + o2; |
| 7185 | if (mlevel2[i2] == 0xFF) |
| 7186 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7187 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7188 | i3 = 128*mlevel2[i2] + o3; |
| 7189 | mlevel3[i3] = i; |
| 7190 | } |
| 7191 | return result; |
| 7192 | } |
| 7193 | |
| 7194 | static int |
| 7195 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7196 | { |
| 7197 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7198 | int l1 = c>>11; |
| 7199 | int l2 = (c>>7) & 0xF; |
| 7200 | int l3 = c & 0x7F; |
| 7201 | int i; |
| 7202 | |
| 7203 | #ifdef Py_UNICODE_WIDE |
| 7204 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7205 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7206 | } |
| 7207 | #endif |
| 7208 | if (c == 0) |
| 7209 | return 0; |
| 7210 | /* level 1*/ |
| 7211 | i = map->level1[l1]; |
| 7212 | if (i == 0xFF) { |
| 7213 | return -1; |
| 7214 | } |
| 7215 | /* level 2*/ |
| 7216 | i = map->level23[16*i+l2]; |
| 7217 | if (i == 0xFF) { |
| 7218 | return -1; |
| 7219 | } |
| 7220 | /* level 3 */ |
| 7221 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7222 | if (i == 0) { |
| 7223 | return -1; |
| 7224 | } |
| 7225 | return i; |
| 7226 | } |
| 7227 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7228 | /* Lookup the character ch in the mapping. If the character |
| 7229 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7230 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7231 | static PyObject * |
| 7232 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7233 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7234 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7235 | PyObject *x; |
| 7236 | |
| 7237 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7238 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7239 | x = PyObject_GetItem(mapping, w); |
| 7240 | Py_DECREF(w); |
| 7241 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7242 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7243 | /* No mapping found means: mapping is undefined. */ |
| 7244 | PyErr_Clear(); |
| 7245 | x = Py_None; |
| 7246 | Py_INCREF(x); |
| 7247 | return x; |
| 7248 | } else |
| 7249 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7250 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7251 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7252 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7253 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7254 | long value = PyLong_AS_LONG(x); |
| 7255 | if (value < 0 || value > 255) { |
| 7256 | PyErr_SetString(PyExc_TypeError, |
| 7257 | "character mapping must be in range(256)"); |
| 7258 | Py_DECREF(x); |
| 7259 | return NULL; |
| 7260 | } |
| 7261 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7262 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7263 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7264 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7265 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7266 | /* wrong return value */ |
| 7267 | PyErr_Format(PyExc_TypeError, |
| 7268 | "character mapping must return integer, bytes or None, not %.400s", |
| 7269 | x->ob_type->tp_name); |
| 7270 | Py_DECREF(x); |
| 7271 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7272 | } |
| 7273 | } |
| 7274 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7275 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7276 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7277 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7278 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7279 | /* exponentially overallocate to minimize reallocations */ |
| 7280 | if (requiredsize < 2*outsize) |
| 7281 | requiredsize = 2*outsize; |
| 7282 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7283 | return -1; |
| 7284 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7285 | } |
| 7286 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7287 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7288 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7289 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7290 | /* 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] | 7291 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7292 | space is available. Return a new reference to the object that |
| 7293 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7294 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7295 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7296 | static charmapencode_result |
| 7297 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 7298 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7299 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7300 | PyObject *rep; |
| 7301 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7302 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7303 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7304 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7305 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7306 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7307 | if (res == -1) |
| 7308 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7309 | if (outsize<requiredsize) |
| 7310 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7311 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7312 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7313 | outstart[(*outpos)++] = (char)res; |
| 7314 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7315 | } |
| 7316 | |
| 7317 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7318 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7319 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7320 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7321 | Py_DECREF(rep); |
| 7322 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7323 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7324 | if (PyLong_Check(rep)) { |
| 7325 | Py_ssize_t requiredsize = *outpos+1; |
| 7326 | if (outsize<requiredsize) |
| 7327 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7328 | Py_DECREF(rep); |
| 7329 | return enc_EXCEPTION; |
| 7330 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7331 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7332 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7333 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7334 | else { |
| 7335 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7336 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7337 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7338 | if (outsize<requiredsize) |
| 7339 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7340 | Py_DECREF(rep); |
| 7341 | return enc_EXCEPTION; |
| 7342 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7343 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7344 | memcpy(outstart + *outpos, repchars, repsize); |
| 7345 | *outpos += repsize; |
| 7346 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7347 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7348 | Py_DECREF(rep); |
| 7349 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7350 | } |
| 7351 | |
| 7352 | /* handle an error in PyUnicode_EncodeCharmap |
| 7353 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7354 | static int |
| 7355 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7356 | 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] | 7357 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7358 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7359 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7360 | { |
| 7361 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7362 | Py_ssize_t repsize; |
| 7363 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7364 | Py_UNICODE *uni2; |
| 7365 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7366 | Py_ssize_t collstartpos = *inpos; |
| 7367 | Py_ssize_t collendpos = *inpos+1; |
| 7368 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7369 | char *encoding = "charmap"; |
| 7370 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7371 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7372 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7373 | /* find all unencodable characters */ |
| 7374 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7375 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7376 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7377 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 7378 | if (res != -1) |
| 7379 | break; |
| 7380 | ++collendpos; |
| 7381 | continue; |
| 7382 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7383 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7384 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 7385 | if (rep==NULL) |
| 7386 | return -1; |
| 7387 | else if (rep!=Py_None) { |
| 7388 | Py_DECREF(rep); |
| 7389 | break; |
| 7390 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7391 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7392 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7393 | } |
| 7394 | /* cache callback name lookup |
| 7395 | * (if not done yet, i.e. it's the first error) */ |
| 7396 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7397 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7398 | *known_errorHandler = 1; |
| 7399 | else if (!strcmp(errors, "replace")) |
| 7400 | *known_errorHandler = 2; |
| 7401 | else if (!strcmp(errors, "ignore")) |
| 7402 | *known_errorHandler = 3; |
| 7403 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7404 | *known_errorHandler = 4; |
| 7405 | else |
| 7406 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7407 | } |
| 7408 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7409 | case 1: /* strict */ |
| 7410 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7411 | return -1; |
| 7412 | case 2: /* replace */ |
| 7413 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7414 | x = charmapencode_output('?', mapping, res, respos); |
| 7415 | if (x==enc_EXCEPTION) { |
| 7416 | return -1; |
| 7417 | } |
| 7418 | else if (x==enc_FAILED) { |
| 7419 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7420 | return -1; |
| 7421 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7422 | } |
| 7423 | /* fall through */ |
| 7424 | case 3: /* ignore */ |
| 7425 | *inpos = collendpos; |
| 7426 | break; |
| 7427 | case 4: /* xmlcharrefreplace */ |
| 7428 | /* generate replacement (temporarily (mis)uses p) */ |
| 7429 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7430 | char buffer[2+29+1+1]; |
| 7431 | char *cp; |
| 7432 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7433 | for (cp = buffer; *cp; ++cp) { |
| 7434 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7435 | if (x==enc_EXCEPTION) |
| 7436 | return -1; |
| 7437 | else if (x==enc_FAILED) { |
| 7438 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7439 | return -1; |
| 7440 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7441 | } |
| 7442 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7443 | *inpos = collendpos; |
| 7444 | break; |
| 7445 | default: |
| 7446 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7447 | encoding, reason, p, size, exceptionObject, |
| 7448 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7449 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7450 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7451 | if (PyBytes_Check(repunicode)) { |
| 7452 | /* Directly copy bytes result to output. */ |
| 7453 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7454 | Py_ssize_t requiredsize; |
| 7455 | repsize = PyBytes_Size(repunicode); |
| 7456 | requiredsize = *respos + repsize; |
| 7457 | if (requiredsize > outsize) |
| 7458 | /* Make room for all additional bytes. */ |
| 7459 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7460 | Py_DECREF(repunicode); |
| 7461 | return -1; |
| 7462 | } |
| 7463 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7464 | PyBytes_AsString(repunicode), repsize); |
| 7465 | *respos += repsize; |
| 7466 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7467 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7468 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7469 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7470 | /* generate replacement */ |
| 7471 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7472 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7473 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7474 | if (x==enc_EXCEPTION) { |
| 7475 | return -1; |
| 7476 | } |
| 7477 | else if (x==enc_FAILED) { |
| 7478 | Py_DECREF(repunicode); |
| 7479 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7480 | return -1; |
| 7481 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7482 | } |
| 7483 | *inpos = newpos; |
| 7484 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7485 | } |
| 7486 | return 0; |
| 7487 | } |
| 7488 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7489 | PyObject * |
| 7490 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7491 | Py_ssize_t size, |
| 7492 | PyObject *mapping, |
| 7493 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7494 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7495 | /* output object */ |
| 7496 | PyObject *res = NULL; |
| 7497 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7498 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7499 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7500 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7501 | PyObject *errorHandler = NULL; |
| 7502 | PyObject *exc = NULL; |
| 7503 | /* the following variable is used for caching string comparisons |
| 7504 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7505 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7506 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7507 | |
| 7508 | /* Default to Latin-1 */ |
| 7509 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7510 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7511 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7512 | /* allocate enough for a simple encoding without |
| 7513 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7514 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7515 | if (res == NULL) |
| 7516 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7517 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7518 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7519 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7520 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7521 | /* try to encode it */ |
| 7522 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7523 | if (x==enc_EXCEPTION) /* error */ |
| 7524 | goto onError; |
| 7525 | if (x==enc_FAILED) { /* unencodable character */ |
| 7526 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7527 | &exc, |
| 7528 | &known_errorHandler, &errorHandler, errors, |
| 7529 | &res, &respos)) { |
| 7530 | goto onError; |
| 7531 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7532 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7533 | else |
| 7534 | /* done with this character => adjust input position */ |
| 7535 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7536 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7537 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7538 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7539 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7540 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7541 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7542 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7543 | Py_XDECREF(exc); |
| 7544 | Py_XDECREF(errorHandler); |
| 7545 | return res; |
| 7546 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7547 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7548 | Py_XDECREF(res); |
| 7549 | Py_XDECREF(exc); |
| 7550 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7551 | return NULL; |
| 7552 | } |
| 7553 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7554 | PyObject * |
| 7555 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7556 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7557 | { |
| 7558 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7559 | PyErr_BadArgument(); |
| 7560 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7561 | } |
| 7562 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7563 | PyUnicode_GET_SIZE(unicode), |
| 7564 | mapping, |
| 7565 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7566 | } |
| 7567 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7568 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7569 | static void |
| 7570 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7571 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7572 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7573 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7574 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7575 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7576 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7577 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7578 | } |
| 7579 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7580 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7581 | goto onError; |
| 7582 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7583 | goto onError; |
| 7584 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7585 | goto onError; |
| 7586 | return; |
| 7587 | onError: |
| 7588 | Py_DECREF(*exceptionObject); |
| 7589 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7590 | } |
| 7591 | } |
| 7592 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7593 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7594 | static void |
| 7595 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7596 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7597 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7598 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7599 | { |
| 7600 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7601 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7602 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7603 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7604 | } |
| 7605 | |
| 7606 | /* error handling callback helper: |
| 7607 | build arguments, call the callback and check the arguments, |
| 7608 | put the result into newpos and return the replacement string, which |
| 7609 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7610 | static PyObject * |
| 7611 | unicode_translate_call_errorhandler(const char *errors, |
| 7612 | PyObject **errorHandler, |
| 7613 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7614 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7615 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7616 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7617 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7618 | 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] | 7619 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7620 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7621 | PyObject *restuple; |
| 7622 | PyObject *resunicode; |
| 7623 | |
| 7624 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7625 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7626 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7627 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7628 | } |
| 7629 | |
| 7630 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7631 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7632 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7633 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7634 | |
| 7635 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7636 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7637 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7638 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7639 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7640 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7641 | Py_DECREF(restuple); |
| 7642 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7643 | } |
| 7644 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7645 | &resunicode, &i_newpos)) { |
| 7646 | Py_DECREF(restuple); |
| 7647 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7648 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7649 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7650 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7651 | else |
| 7652 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7653 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7654 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7655 | Py_DECREF(restuple); |
| 7656 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7657 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7658 | Py_INCREF(resunicode); |
| 7659 | Py_DECREF(restuple); |
| 7660 | return resunicode; |
| 7661 | } |
| 7662 | |
| 7663 | /* Lookup the character ch in the mapping and put the result in result, |
| 7664 | which must be decrefed by the caller. |
| 7665 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7666 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7667 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7668 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7669 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7670 | PyObject *x; |
| 7671 | |
| 7672 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7673 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7674 | x = PyObject_GetItem(mapping, w); |
| 7675 | Py_DECREF(w); |
| 7676 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7677 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7678 | /* No mapping found means: use 1:1 mapping. */ |
| 7679 | PyErr_Clear(); |
| 7680 | *result = NULL; |
| 7681 | return 0; |
| 7682 | } else |
| 7683 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7684 | } |
| 7685 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7686 | *result = x; |
| 7687 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7688 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7689 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7690 | long value = PyLong_AS_LONG(x); |
| 7691 | long max = PyUnicode_GetMax(); |
| 7692 | if (value < 0 || value > max) { |
| 7693 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7694 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7695 | Py_DECREF(x); |
| 7696 | return -1; |
| 7697 | } |
| 7698 | *result = x; |
| 7699 | return 0; |
| 7700 | } |
| 7701 | else if (PyUnicode_Check(x)) { |
| 7702 | *result = x; |
| 7703 | return 0; |
| 7704 | } |
| 7705 | else { |
| 7706 | /* wrong return value */ |
| 7707 | PyErr_SetString(PyExc_TypeError, |
| 7708 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7709 | Py_DECREF(x); |
| 7710 | return -1; |
| 7711 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7712 | } |
| 7713 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7714 | if not reallocate and adjust various state variables. |
| 7715 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7716 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7717 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7718 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7719 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7720 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7721 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7722 | /* exponentially overallocate to minimize reallocations */ |
| 7723 | if (requiredsize < 2 * oldsize) |
| 7724 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7725 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7726 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7727 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7728 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7729 | } |
| 7730 | return 0; |
| 7731 | } |
| 7732 | /* lookup the character, put the result in the output string and adjust |
| 7733 | various state variables. Return a new reference to the object that |
| 7734 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 7735 | undefined (in which case no character was written). |
| 7736 | The called must decref result. |
| 7737 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7738 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7739 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 7740 | PyObject *mapping, Py_UCS4 **output, |
| 7741 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7742 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7743 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7744 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 7745 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7746 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7747 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7748 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7749 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7750 | } |
| 7751 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7752 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7753 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7754 | /* 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] | 7755 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7756 | } |
| 7757 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7758 | Py_ssize_t repsize; |
| 7759 | if (PyUnicode_READY(*res) == -1) |
| 7760 | return -1; |
| 7761 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7762 | if (repsize==1) { |
| 7763 | /* 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] | 7764 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7765 | } |
| 7766 | else if (repsize!=0) { |
| 7767 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7768 | Py_ssize_t requiredsize = *opos + |
| 7769 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7770 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7771 | Py_ssize_t i; |
| 7772 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7773 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7774 | for(i = 0; i < repsize; i++) |
| 7775 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7776 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7777 | } |
| 7778 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7779 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7780 | return 0; |
| 7781 | } |
| 7782 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7783 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7784 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 7785 | PyObject *mapping, |
| 7786 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7787 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7788 | /* input object */ |
| 7789 | char *idata; |
| 7790 | Py_ssize_t size, i; |
| 7791 | int kind; |
| 7792 | /* output buffer */ |
| 7793 | Py_UCS4 *output = NULL; |
| 7794 | Py_ssize_t osize; |
| 7795 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7796 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7797 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7798 | char *reason = "character maps to <undefined>"; |
| 7799 | PyObject *errorHandler = NULL; |
| 7800 | PyObject *exc = NULL; |
| 7801 | /* the following variable is used for caching string comparisons |
| 7802 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7803 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7804 | int known_errorHandler = -1; |
| 7805 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7806 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7807 | PyErr_BadArgument(); |
| 7808 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7809 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7810 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7811 | if (PyUnicode_READY(input) == -1) |
| 7812 | return NULL; |
| 7813 | idata = (char*)PyUnicode_DATA(input); |
| 7814 | kind = PyUnicode_KIND(input); |
| 7815 | size = PyUnicode_GET_LENGTH(input); |
| 7816 | i = 0; |
| 7817 | |
| 7818 | if (size == 0) { |
| 7819 | Py_INCREF(input); |
| 7820 | return input; |
| 7821 | } |
| 7822 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7823 | /* allocate enough for a simple 1:1 translation without |
| 7824 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7825 | osize = size; |
| 7826 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 7827 | opos = 0; |
| 7828 | if (output == NULL) { |
| 7829 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7830 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7831 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7832 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7833 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7834 | /* try to encode it */ |
| 7835 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7836 | if (charmaptranslate_output(input, i, mapping, |
| 7837 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7838 | Py_XDECREF(x); |
| 7839 | goto onError; |
| 7840 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7841 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7842 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7843 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7844 | else { /* untranslatable character */ |
| 7845 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 7846 | Py_ssize_t repsize; |
| 7847 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7848 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7849 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7850 | Py_ssize_t collstart = i; |
| 7851 | Py_ssize_t collend = i+1; |
| 7852 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7853 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7854 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7855 | while (collend < size) { |
| 7856 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7857 | goto onError; |
| 7858 | Py_XDECREF(x); |
| 7859 | if (x!=Py_None) |
| 7860 | break; |
| 7861 | ++collend; |
| 7862 | } |
| 7863 | /* cache callback name lookup |
| 7864 | * (if not done yet, i.e. it's the first error) */ |
| 7865 | if (known_errorHandler==-1) { |
| 7866 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7867 | known_errorHandler = 1; |
| 7868 | else if (!strcmp(errors, "replace")) |
| 7869 | known_errorHandler = 2; |
| 7870 | else if (!strcmp(errors, "ignore")) |
| 7871 | known_errorHandler = 3; |
| 7872 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7873 | known_errorHandler = 4; |
| 7874 | else |
| 7875 | known_errorHandler = 0; |
| 7876 | } |
| 7877 | switch (known_errorHandler) { |
| 7878 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7879 | raise_translate_exception(&exc, input, collstart, |
| 7880 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7881 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7882 | case 2: /* replace */ |
| 7883 | /* 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] | 7884 | for (coll = collstart; coll<collend; coll++) |
| 7885 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7886 | /* fall through */ |
| 7887 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7888 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7889 | break; |
| 7890 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7891 | /* generate replacement (temporarily (mis)uses i) */ |
| 7892 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7893 | char buffer[2+29+1+1]; |
| 7894 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7895 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 7896 | if (charmaptranslate_makespace(&output, &osize, |
| 7897 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7898 | goto onError; |
| 7899 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7900 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7901 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7902 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7903 | break; |
| 7904 | default: |
| 7905 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7906 | reason, input, &exc, |
| 7907 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7908 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7909 | goto onError; |
| 7910 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7911 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 7912 | if (charmaptranslate_makespace(&output, &osize, |
| 7913 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7914 | Py_DECREF(repunicode); |
| 7915 | goto onError; |
| 7916 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7917 | for (uni2 = 0; repsize-->0; ++uni2) |
| 7918 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 7919 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7920 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7921 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7922 | } |
| 7923 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7924 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 7925 | if (!res) |
| 7926 | goto onError; |
| 7927 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7928 | Py_XDECREF(exc); |
| 7929 | Py_XDECREF(errorHandler); |
| 7930 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7931 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7932 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7933 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7934 | Py_XDECREF(exc); |
| 7935 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7936 | return NULL; |
| 7937 | } |
| 7938 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7939 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 7940 | PyObject * |
| 7941 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 7942 | Py_ssize_t size, |
| 7943 | PyObject *mapping, |
| 7944 | const char *errors) |
| 7945 | { |
| 7946 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7947 | if (!unicode) |
| 7948 | return NULL; |
| 7949 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 7950 | } |
| 7951 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7952 | PyObject * |
| 7953 | PyUnicode_Translate(PyObject *str, |
| 7954 | PyObject *mapping, |
| 7955 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7956 | { |
| 7957 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7958 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7959 | str = PyUnicode_FromObject(str); |
| 7960 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7961 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7962 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7963 | Py_DECREF(str); |
| 7964 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7965 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7966 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7967 | Py_XDECREF(str); |
| 7968 | return NULL; |
| 7969 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7970 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7971 | static Py_UCS4 |
| 7972 | fix_decimal_and_space_to_ascii(PyUnicodeObject *self) |
| 7973 | { |
| 7974 | /* No need to call PyUnicode_READY(self) because this function is only |
| 7975 | called as a callback from fixup() which does it already. */ |
| 7976 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 7977 | const int kind = PyUnicode_KIND(self); |
| 7978 | void *data = PyUnicode_DATA(self); |
| 7979 | Py_UCS4 maxchar = 0, ch, fixed; |
| 7980 | Py_ssize_t i; |
| 7981 | |
| 7982 | for (i = 0; i < len; ++i) { |
| 7983 | ch = PyUnicode_READ(kind, data, i); |
| 7984 | fixed = 0; |
| 7985 | if (ch > 127) { |
| 7986 | if (Py_UNICODE_ISSPACE(ch)) |
| 7987 | fixed = ' '; |
| 7988 | else { |
| 7989 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7990 | if (decimal >= 0) |
| 7991 | fixed = '0' + decimal; |
| 7992 | } |
| 7993 | if (fixed != 0) { |
| 7994 | if (fixed > maxchar) |
| 7995 | maxchar = fixed; |
| 7996 | PyUnicode_WRITE(kind, data, i, fixed); |
| 7997 | } |
| 7998 | else if (ch > maxchar) |
| 7999 | maxchar = ch; |
| 8000 | } |
| 8001 | else if (ch > maxchar) |
| 8002 | maxchar = ch; |
| 8003 | } |
| 8004 | |
| 8005 | return maxchar; |
| 8006 | } |
| 8007 | |
| 8008 | PyObject * |
| 8009 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 8010 | { |
| 8011 | if (!PyUnicode_Check(unicode)) { |
| 8012 | PyErr_BadInternalCall(); |
| 8013 | return NULL; |
| 8014 | } |
| 8015 | if (PyUnicode_READY(unicode) == -1) |
| 8016 | return NULL; |
| 8017 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 8018 | /* If the string is already ASCII, just return the same string */ |
| 8019 | Py_INCREF(unicode); |
| 8020 | return unicode; |
| 8021 | } |
| 8022 | return fixup((PyUnicodeObject *)unicode, fix_decimal_and_space_to_ascii); |
| 8023 | } |
| 8024 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8025 | PyObject * |
| 8026 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 8027 | Py_ssize_t length) |
| 8028 | { |
| 8029 | PyObject *result; |
| 8030 | Py_UNICODE *p; /* write pointer into result */ |
| 8031 | Py_ssize_t i; |
| 8032 | /* Copy to a new string */ |
| 8033 | result = (PyObject *)_PyUnicode_New(length); |
| 8034 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 8035 | if (result == NULL) |
| 8036 | return result; |
| 8037 | p = PyUnicode_AS_UNICODE(result); |
| 8038 | /* Iterate over code points */ |
| 8039 | for (i = 0; i < length; i++) { |
| 8040 | Py_UNICODE ch =s[i]; |
| 8041 | if (ch > 127) { |
| 8042 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8043 | if (decimal >= 0) |
| 8044 | p[i] = '0' + decimal; |
| 8045 | } |
| 8046 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8047 | if (PyUnicode_READY((PyUnicodeObject*)result) == -1) { |
| 8048 | Py_DECREF(result); |
| 8049 | return NULL; |
| 8050 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8051 | return result; |
| 8052 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8053 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8054 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8055 | int |
| 8056 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8057 | Py_ssize_t length, |
| 8058 | char *output, |
| 8059 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8060 | { |
| 8061 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8062 | PyObject *errorHandler = NULL; |
| 8063 | PyObject *exc = NULL; |
| 8064 | const char *encoding = "decimal"; |
| 8065 | const char *reason = "invalid decimal Unicode string"; |
| 8066 | /* the following variable is used for caching string comparisons |
| 8067 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8068 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8069 | |
| 8070 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8071 | PyErr_BadArgument(); |
| 8072 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8073 | } |
| 8074 | |
| 8075 | p = s; |
| 8076 | end = s + length; |
| 8077 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8078 | register Py_UNICODE ch = *p; |
| 8079 | int decimal; |
| 8080 | PyObject *repunicode; |
| 8081 | Py_ssize_t repsize; |
| 8082 | Py_ssize_t newpos; |
| 8083 | Py_UNICODE *uni2; |
| 8084 | Py_UNICODE *collstart; |
| 8085 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8086 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8087 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8088 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8089 | ++p; |
| 8090 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8091 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8092 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8093 | if (decimal >= 0) { |
| 8094 | *output++ = '0' + decimal; |
| 8095 | ++p; |
| 8096 | continue; |
| 8097 | } |
| 8098 | if (0 < ch && ch < 256) { |
| 8099 | *output++ = (char)ch; |
| 8100 | ++p; |
| 8101 | continue; |
| 8102 | } |
| 8103 | /* All other characters are considered unencodable */ |
| 8104 | collstart = p; |
| 8105 | collend = p+1; |
| 8106 | while (collend < end) { |
| 8107 | if ((0 < *collend && *collend < 256) || |
| 8108 | !Py_UNICODE_ISSPACE(*collend) || |
| 8109 | Py_UNICODE_TODECIMAL(*collend)) |
| 8110 | break; |
| 8111 | } |
| 8112 | /* cache callback name lookup |
| 8113 | * (if not done yet, i.e. it's the first error) */ |
| 8114 | if (known_errorHandler==-1) { |
| 8115 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8116 | known_errorHandler = 1; |
| 8117 | else if (!strcmp(errors, "replace")) |
| 8118 | known_errorHandler = 2; |
| 8119 | else if (!strcmp(errors, "ignore")) |
| 8120 | known_errorHandler = 3; |
| 8121 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8122 | known_errorHandler = 4; |
| 8123 | else |
| 8124 | known_errorHandler = 0; |
| 8125 | } |
| 8126 | switch (known_errorHandler) { |
| 8127 | case 1: /* strict */ |
| 8128 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8129 | goto onError; |
| 8130 | case 2: /* replace */ |
| 8131 | for (p = collstart; p < collend; ++p) |
| 8132 | *output++ = '?'; |
| 8133 | /* fall through */ |
| 8134 | case 3: /* ignore */ |
| 8135 | p = collend; |
| 8136 | break; |
| 8137 | case 4: /* xmlcharrefreplace */ |
| 8138 | /* generate replacement (temporarily (mis)uses p) */ |
| 8139 | for (p = collstart; p < collend; ++p) |
| 8140 | output += sprintf(output, "&#%d;", (int)*p); |
| 8141 | p = collend; |
| 8142 | break; |
| 8143 | default: |
| 8144 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 8145 | encoding, reason, s, length, &exc, |
| 8146 | collstart-s, collend-s, &newpos); |
| 8147 | if (repunicode == NULL) |
| 8148 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8149 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8150 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8151 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8152 | Py_DECREF(repunicode); |
| 8153 | goto onError; |
| 8154 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8155 | /* generate replacement */ |
| 8156 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8157 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8158 | Py_UNICODE ch = *uni2; |
| 8159 | if (Py_UNICODE_ISSPACE(ch)) |
| 8160 | *output++ = ' '; |
| 8161 | else { |
| 8162 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8163 | if (decimal >= 0) |
| 8164 | *output++ = '0' + decimal; |
| 8165 | else if (0 < ch && ch < 256) |
| 8166 | *output++ = (char)ch; |
| 8167 | else { |
| 8168 | Py_DECREF(repunicode); |
| 8169 | raise_encode_exception(&exc, encoding, |
| 8170 | s, length, collstart-s, collend-s, reason); |
| 8171 | goto onError; |
| 8172 | } |
| 8173 | } |
| 8174 | } |
| 8175 | p = s + newpos; |
| 8176 | Py_DECREF(repunicode); |
| 8177 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8178 | } |
| 8179 | /* 0-terminate the output string */ |
| 8180 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8181 | Py_XDECREF(exc); |
| 8182 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8183 | return 0; |
| 8184 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8185 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8186 | Py_XDECREF(exc); |
| 8187 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8188 | return -1; |
| 8189 | } |
| 8190 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8191 | /* --- Helpers ------------------------------------------------------------ */ |
| 8192 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8193 | #include "stringlib/ucs1lib.h" |
| 8194 | #include "stringlib/fastsearch.h" |
| 8195 | #include "stringlib/partition.h" |
| 8196 | #include "stringlib/split.h" |
| 8197 | #include "stringlib/count.h" |
| 8198 | #include "stringlib/find.h" |
| 8199 | #include "stringlib/localeutil.h" |
| 8200 | #include "stringlib/undef.h" |
| 8201 | |
| 8202 | #include "stringlib/ucs2lib.h" |
| 8203 | #include "stringlib/fastsearch.h" |
| 8204 | #include "stringlib/partition.h" |
| 8205 | #include "stringlib/split.h" |
| 8206 | #include "stringlib/count.h" |
| 8207 | #include "stringlib/find.h" |
| 8208 | #include "stringlib/localeutil.h" |
| 8209 | #include "stringlib/undef.h" |
| 8210 | |
| 8211 | #include "stringlib/ucs4lib.h" |
| 8212 | #include "stringlib/fastsearch.h" |
| 8213 | #include "stringlib/partition.h" |
| 8214 | #include "stringlib/split.h" |
| 8215 | #include "stringlib/count.h" |
| 8216 | #include "stringlib/find.h" |
| 8217 | #include "stringlib/localeutil.h" |
| 8218 | #include "stringlib/undef.h" |
| 8219 | |
| 8220 | static Py_ssize_t |
| 8221 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, |
| 8222 | const Py_UCS1*, Py_ssize_t, |
| 8223 | Py_ssize_t, Py_ssize_t), |
| 8224 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 8225 | const Py_UCS2*, Py_ssize_t, |
| 8226 | Py_ssize_t, Py_ssize_t), |
| 8227 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 8228 | const Py_UCS4*, Py_ssize_t, |
| 8229 | Py_ssize_t, Py_ssize_t), |
| 8230 | PyObject* s1, PyObject* s2, |
| 8231 | Py_ssize_t start, |
| 8232 | Py_ssize_t end) |
| 8233 | { |
| 8234 | int kind1, kind2, kind; |
| 8235 | void *buf1, *buf2; |
| 8236 | Py_ssize_t len1, len2, result; |
| 8237 | |
| 8238 | kind1 = PyUnicode_KIND(s1); |
| 8239 | kind2 = PyUnicode_KIND(s2); |
| 8240 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8241 | buf1 = PyUnicode_DATA(s1); |
| 8242 | buf2 = PyUnicode_DATA(s2); |
| 8243 | if (kind1 != kind) |
| 8244 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8245 | if (!buf1) |
| 8246 | return -2; |
| 8247 | if (kind2 != kind) |
| 8248 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8249 | if (!buf2) { |
| 8250 | if (kind1 != kind) PyMem_Free(buf1); |
| 8251 | return -2; |
| 8252 | } |
| 8253 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8254 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8255 | |
| 8256 | switch(kind) { |
| 8257 | case PyUnicode_1BYTE_KIND: |
| 8258 | result = ucs1(buf1, len1, buf2, len2, start, end); |
| 8259 | break; |
| 8260 | case PyUnicode_2BYTE_KIND: |
| 8261 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 8262 | break; |
| 8263 | case PyUnicode_4BYTE_KIND: |
| 8264 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 8265 | break; |
| 8266 | default: |
| 8267 | assert(0); result = -2; |
| 8268 | } |
| 8269 | |
| 8270 | if (kind1 != kind) |
| 8271 | PyMem_Free(buf1); |
| 8272 | if (kind2 != kind) |
| 8273 | PyMem_Free(buf2); |
| 8274 | |
| 8275 | return result; |
| 8276 | } |
| 8277 | |
| 8278 | Py_ssize_t |
| 8279 | _PyUnicode_InsertThousandsGrouping(int kind, void *data, |
| 8280 | Py_ssize_t n_buffer, |
| 8281 | void *digits, Py_ssize_t n_digits, |
| 8282 | Py_ssize_t min_width, |
| 8283 | const char *grouping, |
| 8284 | const char *thousands_sep) |
| 8285 | { |
| 8286 | switch(kind) { |
| 8287 | case PyUnicode_1BYTE_KIND: |
| 8288 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 8289 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8290 | min_width, grouping, thousands_sep); |
| 8291 | case PyUnicode_2BYTE_KIND: |
| 8292 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 8293 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 8294 | min_width, grouping, thousands_sep); |
| 8295 | case PyUnicode_4BYTE_KIND: |
| 8296 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 8297 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 8298 | min_width, grouping, thousands_sep); |
| 8299 | } |
| 8300 | assert(0); |
| 8301 | return -1; |
| 8302 | } |
| 8303 | |
| 8304 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8305 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8306 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8307 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8308 | #include "stringlib/count.h" |
| 8309 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8310 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8311 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8312 | #define ADJUST_INDICES(start, end, len) \ |
| 8313 | if (end > len) \ |
| 8314 | end = len; \ |
| 8315 | else if (end < 0) { \ |
| 8316 | end += len; \ |
| 8317 | if (end < 0) \ |
| 8318 | end = 0; \ |
| 8319 | } \ |
| 8320 | if (start < 0) { \ |
| 8321 | start += len; \ |
| 8322 | if (start < 0) \ |
| 8323 | start = 0; \ |
| 8324 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8325 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8326 | Py_ssize_t |
| 8327 | PyUnicode_Count(PyObject *str, |
| 8328 | PyObject *substr, |
| 8329 | Py_ssize_t start, |
| 8330 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8331 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8332 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8333 | PyUnicodeObject* str_obj; |
| 8334 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8335 | int kind1, kind2, kind; |
| 8336 | void *buf1 = NULL, *buf2 = NULL; |
| 8337 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8338 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8339 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8340 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8341 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8342 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8343 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8344 | Py_DECREF(str_obj); |
| 8345 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8346 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8347 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8348 | kind1 = PyUnicode_KIND(str_obj); |
| 8349 | kind2 = PyUnicode_KIND(sub_obj); |
| 8350 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8351 | buf1 = PyUnicode_DATA(str_obj); |
| 8352 | if (kind1 != kind) |
| 8353 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 8354 | if (!buf1) |
| 8355 | goto onError; |
| 8356 | buf2 = PyUnicode_DATA(sub_obj); |
| 8357 | if (kind2 != kind) |
| 8358 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 8359 | if (!buf2) |
| 8360 | goto onError; |
| 8361 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8362 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8363 | |
| 8364 | ADJUST_INDICES(start, end, len1); |
| 8365 | switch(kind) { |
| 8366 | case PyUnicode_1BYTE_KIND: |
| 8367 | result = ucs1lib_count( |
| 8368 | ((Py_UCS1*)buf1) + start, end - start, |
| 8369 | buf2, len2, PY_SSIZE_T_MAX |
| 8370 | ); |
| 8371 | break; |
| 8372 | case PyUnicode_2BYTE_KIND: |
| 8373 | result = ucs2lib_count( |
| 8374 | ((Py_UCS2*)buf1) + start, end - start, |
| 8375 | buf2, len2, PY_SSIZE_T_MAX |
| 8376 | ); |
| 8377 | break; |
| 8378 | case PyUnicode_4BYTE_KIND: |
| 8379 | result = ucs4lib_count( |
| 8380 | ((Py_UCS4*)buf1) + start, end - start, |
| 8381 | buf2, len2, PY_SSIZE_T_MAX |
| 8382 | ); |
| 8383 | break; |
| 8384 | default: |
| 8385 | assert(0); result = 0; |
| 8386 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8387 | |
| 8388 | Py_DECREF(sub_obj); |
| 8389 | Py_DECREF(str_obj); |
| 8390 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8391 | if (kind1 != kind) |
| 8392 | PyMem_Free(buf1); |
| 8393 | if (kind2 != kind) |
| 8394 | PyMem_Free(buf2); |
| 8395 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8396 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8397 | onError: |
| 8398 | Py_DECREF(sub_obj); |
| 8399 | Py_DECREF(str_obj); |
| 8400 | if (kind1 != kind && buf1) |
| 8401 | PyMem_Free(buf1); |
| 8402 | if (kind2 != kind && buf2) |
| 8403 | PyMem_Free(buf2); |
| 8404 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8405 | } |
| 8406 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8407 | Py_ssize_t |
| 8408 | PyUnicode_Find(PyObject *str, |
| 8409 | PyObject *sub, |
| 8410 | Py_ssize_t start, |
| 8411 | Py_ssize_t end, |
| 8412 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8413 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8414 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8416 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8417 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8418 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8419 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8420 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8421 | Py_DECREF(str); |
| 8422 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8423 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8424 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8425 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8426 | result = any_find_slice( |
| 8427 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 8428 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8429 | ); |
| 8430 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8431 | result = any_find_slice( |
| 8432 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 8433 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8434 | ); |
| 8435 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8436 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8437 | Py_DECREF(sub); |
| 8438 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8439 | return result; |
| 8440 | } |
| 8441 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8442 | Py_ssize_t |
| 8443 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8444 | Py_ssize_t start, Py_ssize_t end, |
| 8445 | int direction) |
| 8446 | { |
| 8447 | char *result; |
| 8448 | int kind; |
| 8449 | if (PyUnicode_READY(str) == -1) |
| 8450 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8451 | if (start < 0 || end < 0) { |
| 8452 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8453 | return -2; |
| 8454 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8455 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8456 | end = PyUnicode_GET_LENGTH(str); |
| 8457 | kind = PyUnicode_KIND(str); |
| 8458 | result = findchar(PyUnicode_1BYTE_DATA(str) |
| 8459 | + PyUnicode_KIND_SIZE(kind, start), |
| 8460 | kind, |
| 8461 | end-start, ch, direction); |
| 8462 | if (!result) |
| 8463 | return -1; |
| 8464 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8465 | } |
| 8466 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8467 | static int |
| 8468 | tailmatch(PyUnicodeObject *self, |
| 8469 | PyUnicodeObject *substring, |
| 8470 | Py_ssize_t start, |
| 8471 | Py_ssize_t end, |
| 8472 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8473 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8474 | int kind_self; |
| 8475 | int kind_sub; |
| 8476 | void *data_self; |
| 8477 | void *data_sub; |
| 8478 | Py_ssize_t offset; |
| 8479 | Py_ssize_t i; |
| 8480 | Py_ssize_t end_sub; |
| 8481 | |
| 8482 | if (PyUnicode_READY(self) == -1 || |
| 8483 | PyUnicode_READY(substring) == -1) |
| 8484 | return 0; |
| 8485 | |
| 8486 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8487 | return 1; |
| 8488 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8489 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8490 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8491 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8492 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8493 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8494 | kind_self = PyUnicode_KIND(self); |
| 8495 | data_self = PyUnicode_DATA(self); |
| 8496 | kind_sub = PyUnicode_KIND(substring); |
| 8497 | data_sub = PyUnicode_DATA(substring); |
| 8498 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8499 | |
| 8500 | if (direction > 0) |
| 8501 | offset = end; |
| 8502 | else |
| 8503 | offset = start; |
| 8504 | |
| 8505 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8506 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8507 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8508 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8509 | /* If both are of the same kind, memcmp is sufficient */ |
| 8510 | if (kind_self == kind_sub) { |
| 8511 | return ! memcmp((char *)data_self + |
| 8512 | (offset * PyUnicode_CHARACTER_SIZE(substring)), |
| 8513 | data_sub, |
| 8514 | PyUnicode_GET_LENGTH(substring) * |
| 8515 | PyUnicode_CHARACTER_SIZE(substring)); |
| 8516 | } |
| 8517 | /* otherwise we have to compare each character by first accesing it */ |
| 8518 | else { |
| 8519 | /* We do not need to compare 0 and len(substring)-1 because |
| 8520 | the if statement above ensured already that they are equal |
| 8521 | when we end up here. */ |
| 8522 | // TODO: honor direction and do a forward or backwards search |
| 8523 | for (i = 1; i < end_sub; ++i) { |
| 8524 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8525 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8526 | return 0; |
| 8527 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8528 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8529 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8530 | } |
| 8531 | |
| 8532 | return 0; |
| 8533 | } |
| 8534 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8535 | Py_ssize_t |
| 8536 | PyUnicode_Tailmatch(PyObject *str, |
| 8537 | PyObject *substr, |
| 8538 | Py_ssize_t start, |
| 8539 | Py_ssize_t end, |
| 8540 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8541 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8542 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8543 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8544 | str = PyUnicode_FromObject(str); |
| 8545 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8546 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8547 | substr = PyUnicode_FromObject(substr); |
| 8548 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8549 | Py_DECREF(str); |
| 8550 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8551 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8553 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8554 | (PyUnicodeObject *)substr, |
| 8555 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8556 | Py_DECREF(str); |
| 8557 | Py_DECREF(substr); |
| 8558 | return result; |
| 8559 | } |
| 8560 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8561 | /* Apply fixfct filter to the Unicode object self and return a |
| 8562 | reference to the modified object */ |
| 8563 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8564 | static PyObject * |
| 8565 | fixup(PyUnicodeObject *self, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8566 | Py_UCS4 (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8567 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8568 | PyObject *u; |
| 8569 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8570 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8571 | if (PyUnicode_READY(self) == -1) |
| 8572 | return NULL; |
| 8573 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8574 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8575 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8576 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8577 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8578 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8579 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
| 8580 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8581 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8582 | /* fix functions return the new maximum character in a string, |
| 8583 | if the kind of the resulting unicode object does not change, |
| 8584 | everything is fine. Otherwise we need to change the string kind |
| 8585 | and re-run the fix function. */ |
| 8586 | maxchar_new = fixfct((PyUnicodeObject*)u); |
| 8587 | if (maxchar_new == 0) |
| 8588 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8589 | else if (maxchar_new <= 127) |
| 8590 | maxchar_new = 127; |
| 8591 | else if (maxchar_new <= 255) |
| 8592 | maxchar_new = 255; |
| 8593 | else if (maxchar_new <= 65535) |
| 8594 | maxchar_new = 65535; |
| 8595 | else |
| 8596 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8597 | |
| 8598 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8599 | /* fixfct should return TRUE if it modified the buffer. If |
| 8600 | FALSE, return a reference to the original buffer instead |
| 8601 | (to save space, not time) */ |
| 8602 | Py_INCREF(self); |
| 8603 | Py_DECREF(u); |
| 8604 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8605 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8606 | else if (maxchar_new == maxchar_old) { |
| 8607 | return u; |
| 8608 | } |
| 8609 | else { |
| 8610 | /* In case the maximum character changed, we need to |
| 8611 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8612 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8613 | if (v == NULL) { |
| 8614 | Py_DECREF(u); |
| 8615 | return NULL; |
| 8616 | } |
| 8617 | if (maxchar_new > maxchar_old) { |
| 8618 | /* If the maxchar increased so that the kind changed, not all |
| 8619 | characters are representable anymore and we need to fix the |
| 8620 | string again. This only happens in very few cases. */ |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8621 | if (PyUnicode_CopyCharacters(v, 0, |
| 8622 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8623 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8624 | { |
| 8625 | Py_DECREF(u); |
| 8626 | return NULL; |
| 8627 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8628 | maxchar_old = fixfct((PyUnicodeObject*)v); |
| 8629 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8630 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8631 | else { |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8632 | if (PyUnicode_CopyCharacters(v, 0, |
| 8633 | u, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8634 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8635 | { |
| 8636 | Py_DECREF(u); |
| 8637 | return NULL; |
| 8638 | } |
| 8639 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8640 | |
| 8641 | Py_DECREF(u); |
| 8642 | return v; |
| 8643 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8644 | } |
| 8645 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8646 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8647 | fixupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8648 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8649 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8650 | called as a callback from fixup() which does it already. */ |
| 8651 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8652 | const int kind = PyUnicode_KIND(self); |
| 8653 | void *data = PyUnicode_DATA(self); |
| 8654 | int touched = 0; |
| 8655 | Py_UCS4 maxchar = 0; |
| 8656 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8657 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8658 | for (i = 0; i < len; ++i) { |
| 8659 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8660 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8661 | if (up != ch) { |
| 8662 | if (up > maxchar) |
| 8663 | maxchar = up; |
| 8664 | PyUnicode_WRITE(kind, data, i, up); |
| 8665 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8666 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8667 | else if (ch > maxchar) |
| 8668 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8669 | } |
| 8670 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8671 | if (touched) |
| 8672 | return maxchar; |
| 8673 | else |
| 8674 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8675 | } |
| 8676 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8677 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8678 | fixlower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8679 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8680 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8681 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8682 | const int kind = PyUnicode_KIND(self); |
| 8683 | void *data = PyUnicode_DATA(self); |
| 8684 | int touched = 0; |
| 8685 | Py_UCS4 maxchar = 0; |
| 8686 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8687 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8688 | for(i = 0; i < len; ++i) { |
| 8689 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8690 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8691 | if (lo != ch) { |
| 8692 | if (lo > maxchar) |
| 8693 | maxchar = lo; |
| 8694 | PyUnicode_WRITE(kind, data, i, lo); |
| 8695 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8696 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8697 | else if (ch > maxchar) |
| 8698 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8699 | } |
| 8700 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8701 | if (touched) |
| 8702 | return maxchar; |
| 8703 | else |
| 8704 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8705 | } |
| 8706 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8707 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8708 | fixswapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8709 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8710 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8711 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8712 | const int kind = PyUnicode_KIND(self); |
| 8713 | void *data = PyUnicode_DATA(self); |
| 8714 | int touched = 0; |
| 8715 | Py_UCS4 maxchar = 0; |
| 8716 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8717 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8718 | for(i = 0; i < len; ++i) { |
| 8719 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8720 | Py_UCS4 nu = 0; |
| 8721 | |
| 8722 | if (Py_UNICODE_ISUPPER(ch)) |
| 8723 | nu = Py_UNICODE_TOLOWER(ch); |
| 8724 | else if (Py_UNICODE_ISLOWER(ch)) |
| 8725 | nu = Py_UNICODE_TOUPPER(ch); |
| 8726 | |
| 8727 | if (nu != 0) { |
| 8728 | if (nu > maxchar) |
| 8729 | maxchar = nu; |
| 8730 | PyUnicode_WRITE(kind, data, i, nu); |
| 8731 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8732 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8733 | else if (ch > maxchar) |
| 8734 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8735 | } |
| 8736 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8737 | if (touched) |
| 8738 | return maxchar; |
| 8739 | else |
| 8740 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8741 | } |
| 8742 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8743 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8744 | fixcapitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8745 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8746 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8747 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8748 | const int kind = PyUnicode_KIND(self); |
| 8749 | void *data = PyUnicode_DATA(self); |
| 8750 | int touched = 0; |
| 8751 | Py_UCS4 maxchar = 0; |
| 8752 | Py_ssize_t i = 0; |
| 8753 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8754 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8755 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8756 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8757 | |
| 8758 | ch = PyUnicode_READ(kind, data, i); |
| 8759 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 8760 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 8761 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 8762 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8763 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8764 | ++i; |
| 8765 | for(; i < len; ++i) { |
| 8766 | ch = PyUnicode_READ(kind, data, i); |
| 8767 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 8768 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8769 | if (lo > maxchar) |
| 8770 | maxchar = lo; |
| 8771 | PyUnicode_WRITE(kind, data, i, lo); |
| 8772 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8773 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8774 | else if (ch > maxchar) |
| 8775 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8776 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8777 | |
| 8778 | if (touched) |
| 8779 | return maxchar; |
| 8780 | else |
| 8781 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8782 | } |
| 8783 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8784 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8785 | fixtitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8786 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8787 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8788 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8789 | const int kind = PyUnicode_KIND(self); |
| 8790 | void *data = PyUnicode_DATA(self); |
| 8791 | Py_UCS4 maxchar = 0; |
| 8792 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8793 | int previous_is_cased; |
| 8794 | |
| 8795 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8796 | if (len == 1) { |
| 8797 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8798 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 8799 | if (ti != ch) { |
| 8800 | PyUnicode_WRITE(kind, data, i, ti); |
| 8801 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8802 | } |
| 8803 | else |
| 8804 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8805 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8806 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8807 | for(; i < len; ++i) { |
| 8808 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8809 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8810 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8811 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8812 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8813 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8814 | nu = Py_UNICODE_TOTITLE(ch); |
| 8815 | |
| 8816 | if (nu > maxchar) |
| 8817 | maxchar = nu; |
| 8818 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8819 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8820 | if (Py_UNICODE_ISLOWER(ch) || |
| 8821 | Py_UNICODE_ISUPPER(ch) || |
| 8822 | Py_UNICODE_ISTITLE(ch)) |
| 8823 | previous_is_cased = 1; |
| 8824 | else |
| 8825 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8826 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8827 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8828 | } |
| 8829 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8830 | PyObject * |
| 8831 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8832 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8833 | PyObject *sep = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8834 | Py_ssize_t seplen = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8835 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8836 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8837 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 8838 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8839 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8840 | Py_ssize_t sz, i, res_offset; |
| 8841 | Py_UCS4 maxchar = 0; |
| 8842 | Py_UCS4 item_maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8843 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8844 | fseq = PySequence_Fast(seq, ""); |
| 8845 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8846 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8847 | } |
| 8848 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8849 | /* NOTE: the following code can't call back into Python code, |
| 8850 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8851 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8852 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8853 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 8854 | /* If empty sequence, return u"". */ |
| 8855 | if (seqlen == 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8856 | res = PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8857 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8858 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8859 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8860 | /* If singleton sequence with an exact Unicode, return that. */ |
| 8861 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8862 | item = items[0]; |
| 8863 | if (PyUnicode_CheckExact(item)) { |
| 8864 | Py_INCREF(item); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8865 | res = item; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8866 | goto Done; |
| 8867 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8868 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8869 | else { |
| 8870 | /* Set up sep and seplen */ |
| 8871 | if (separator == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8872 | /* fall back to a blank space separator */ |
| 8873 | sep = PyUnicode_FromOrdinal(' '); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8874 | if (!sep) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8875 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8876 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8877 | else { |
| 8878 | if (!PyUnicode_Check(separator)) { |
| 8879 | PyErr_Format(PyExc_TypeError, |
| 8880 | "separator: expected str instance," |
| 8881 | " %.80s found", |
| 8882 | Py_TYPE(separator)->tp_name); |
| 8883 | goto onError; |
| 8884 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8885 | if (PyUnicode_READY(separator)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8886 | goto onError; |
| 8887 | sep = separator; |
| 8888 | seplen = PyUnicode_GET_LENGTH(separator); |
| 8889 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 8890 | /* inc refcount to keep this code path symetric with the |
| 8891 | above case of a blank separator */ |
| 8892 | Py_INCREF(sep); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8893 | } |
| 8894 | } |
| 8895 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8896 | /* There are at least two things to join, or else we have a subclass |
| 8897 | * of str in the sequence. |
| 8898 | * Do a pre-pass to figure out the total amount of space we'll |
| 8899 | * need (sz), and see whether all argument are strings. |
| 8900 | */ |
| 8901 | sz = 0; |
| 8902 | for (i = 0; i < seqlen; i++) { |
| 8903 | const Py_ssize_t old_sz = sz; |
| 8904 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8905 | if (!PyUnicode_Check(item)) { |
| 8906 | PyErr_Format(PyExc_TypeError, |
| 8907 | "sequence item %zd: expected str instance," |
| 8908 | " %.80s found", |
| 8909 | i, Py_TYPE(item)->tp_name); |
| 8910 | goto onError; |
| 8911 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8912 | if (PyUnicode_READY(item) == -1) |
| 8913 | goto onError; |
| 8914 | sz += PyUnicode_GET_LENGTH(item); |
| 8915 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
| 8916 | if (item_maxchar > maxchar) |
| 8917 | maxchar = item_maxchar; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8918 | if (i != 0) |
| 8919 | sz += seplen; |
| 8920 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 8921 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8922 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8923 | goto onError; |
| 8924 | } |
| 8925 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8926 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8927 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8928 | if (res == NULL) |
| 8929 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8930 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8931 | /* Catenate everything. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8932 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8933 | Py_ssize_t itemlen, copied; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8934 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8935 | /* Copy item, and maybe the separator. */ |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8936 | if (i && seplen != 0) { |
| 8937 | copied = PyUnicode_CopyCharacters(res, res_offset, |
| 8938 | sep, 0, seplen); |
| 8939 | if (copied < 0) |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8940 | goto onError; |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8941 | #ifdef Py_DEBUG |
| 8942 | res_offset += copied; |
| 8943 | #else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8944 | res_offset += seplen; |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8945 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8946 | } |
Victor Stinner | 9ce5a83 | 2011-10-03 23:36:02 +0200 | [diff] [blame] | 8947 | itemlen = PyUnicode_GET_LENGTH(item); |
| 8948 | if (itemlen != 0) { |
| 8949 | copied = PyUnicode_CopyCharacters(res, res_offset, |
| 8950 | item, 0, itemlen); |
| 8951 | if (copied < 0) |
| 8952 | goto onError; |
| 8953 | #ifdef Py_DEBUG |
| 8954 | res_offset += copied; |
| 8955 | #else |
| 8956 | res_offset += itemlen; |
| 8957 | #endif |
| 8958 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8959 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8960 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8961 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8962 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8963 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8964 | Py_XDECREF(sep); |
| 8965 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8966 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8967 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8968 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8969 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8970 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8971 | return NULL; |
| 8972 | } |
| 8973 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8974 | #define FILL(kind, data, value, start, length) \ |
| 8975 | do { \ |
| 8976 | Py_ssize_t i_ = 0; \ |
| 8977 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 8978 | switch ((kind)) { \ |
| 8979 | case PyUnicode_1BYTE_KIND: { \ |
| 8980 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 8981 | memset(to_, (unsigned char)value, length); \ |
| 8982 | break; \ |
| 8983 | } \ |
| 8984 | case PyUnicode_2BYTE_KIND: { \ |
| 8985 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 8986 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8987 | break; \ |
| 8988 | } \ |
| 8989 | default: { \ |
| 8990 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 8991 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8992 | break; \ |
| 8993 | } \ |
| 8994 | } \ |
| 8995 | } while (0) |
| 8996 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8997 | static PyUnicodeObject * |
| 8998 | pad(PyUnicodeObject *self, |
| 8999 | Py_ssize_t left, |
| 9000 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9001 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9002 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9003 | PyObject *u; |
| 9004 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9005 | int kind; |
| 9006 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9007 | |
| 9008 | if (left < 0) |
| 9009 | left = 0; |
| 9010 | if (right < 0) |
| 9011 | right = 0; |
| 9012 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 9013 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9014 | Py_INCREF(self); |
| 9015 | return self; |
| 9016 | } |
| 9017 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9018 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 9019 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 9020 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 9021 | return NULL; |
| 9022 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9023 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9024 | if (fill > maxchar) |
| 9025 | maxchar = fill; |
| 9026 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9027 | if (!u) |
| 9028 | return NULL; |
| 9029 | |
| 9030 | kind = PyUnicode_KIND(u); |
| 9031 | data = PyUnicode_DATA(u); |
| 9032 | if (left) |
| 9033 | FILL(kind, data, fill, 0, left); |
| 9034 | if (right) |
| 9035 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9036 | if (PyUnicode_CopyCharacters(u, left, |
| 9037 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9038 | _PyUnicode_LENGTH(self)) < 0) |
| 9039 | { |
| 9040 | Py_DECREF(u); |
| 9041 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9042 | } |
| 9043 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9044 | return (PyUnicodeObject*)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9045 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9046 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9047 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9048 | PyObject * |
| 9049 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9050 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9051 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9052 | |
| 9053 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9054 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9055 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9056 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9057 | switch(PyUnicode_KIND(string)) { |
| 9058 | case PyUnicode_1BYTE_KIND: |
| 9059 | list = ucs1lib_splitlines( |
| 9060 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9061 | PyUnicode_GET_LENGTH(string), keepends); |
| 9062 | break; |
| 9063 | case PyUnicode_2BYTE_KIND: |
| 9064 | list = ucs2lib_splitlines( |
| 9065 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 9066 | PyUnicode_GET_LENGTH(string), keepends); |
| 9067 | break; |
| 9068 | case PyUnicode_4BYTE_KIND: |
| 9069 | list = ucs4lib_splitlines( |
| 9070 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 9071 | PyUnicode_GET_LENGTH(string), keepends); |
| 9072 | break; |
| 9073 | default: |
| 9074 | assert(0); |
| 9075 | list = 0; |
| 9076 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9077 | Py_DECREF(string); |
| 9078 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9079 | } |
| 9080 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9081 | static PyObject * |
| 9082 | split(PyUnicodeObject *self, |
| 9083 | PyUnicodeObject *substring, |
| 9084 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9085 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9086 | int kind1, kind2, kind; |
| 9087 | void *buf1, *buf2; |
| 9088 | Py_ssize_t len1, len2; |
| 9089 | PyObject* out; |
| 9090 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9091 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9092 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9093 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9094 | if (PyUnicode_READY(self) == -1) |
| 9095 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9096 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9097 | if (substring == NULL) |
| 9098 | switch(PyUnicode_KIND(self)) { |
| 9099 | case PyUnicode_1BYTE_KIND: |
| 9100 | return ucs1lib_split_whitespace( |
| 9101 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9102 | PyUnicode_GET_LENGTH(self), maxcount |
| 9103 | ); |
| 9104 | case PyUnicode_2BYTE_KIND: |
| 9105 | return ucs2lib_split_whitespace( |
| 9106 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9107 | PyUnicode_GET_LENGTH(self), maxcount |
| 9108 | ); |
| 9109 | case PyUnicode_4BYTE_KIND: |
| 9110 | return ucs4lib_split_whitespace( |
| 9111 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9112 | PyUnicode_GET_LENGTH(self), maxcount |
| 9113 | ); |
| 9114 | default: |
| 9115 | assert(0); |
| 9116 | return NULL; |
| 9117 | } |
| 9118 | |
| 9119 | if (PyUnicode_READY(substring) == -1) |
| 9120 | return NULL; |
| 9121 | |
| 9122 | kind1 = PyUnicode_KIND(self); |
| 9123 | kind2 = PyUnicode_KIND(substring); |
| 9124 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9125 | buf1 = PyUnicode_DATA(self); |
| 9126 | buf2 = PyUnicode_DATA(substring); |
| 9127 | if (kind1 != kind) |
| 9128 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9129 | if (!buf1) |
| 9130 | return NULL; |
| 9131 | if (kind2 != kind) |
| 9132 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9133 | if (!buf2) { |
| 9134 | if (kind1 != kind) PyMem_Free(buf1); |
| 9135 | return NULL; |
| 9136 | } |
| 9137 | len1 = PyUnicode_GET_LENGTH(self); |
| 9138 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9139 | |
| 9140 | switch(kind) { |
| 9141 | case PyUnicode_1BYTE_KIND: |
| 9142 | out = ucs1lib_split( |
| 9143 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9144 | break; |
| 9145 | case PyUnicode_2BYTE_KIND: |
| 9146 | out = ucs2lib_split( |
| 9147 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9148 | break; |
| 9149 | case PyUnicode_4BYTE_KIND: |
| 9150 | out = ucs4lib_split( |
| 9151 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9152 | break; |
| 9153 | default: |
| 9154 | out = NULL; |
| 9155 | } |
| 9156 | if (kind1 != kind) |
| 9157 | PyMem_Free(buf1); |
| 9158 | if (kind2 != kind) |
| 9159 | PyMem_Free(buf2); |
| 9160 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9161 | } |
| 9162 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9163 | static PyObject * |
| 9164 | rsplit(PyUnicodeObject *self, |
| 9165 | PyUnicodeObject *substring, |
| 9166 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9167 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9168 | int kind1, kind2, kind; |
| 9169 | void *buf1, *buf2; |
| 9170 | Py_ssize_t len1, len2; |
| 9171 | PyObject* out; |
| 9172 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9173 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9174 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9175 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9176 | if (PyUnicode_READY(self) == -1) |
| 9177 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9178 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9179 | if (substring == NULL) |
| 9180 | switch(PyUnicode_KIND(self)) { |
| 9181 | case PyUnicode_1BYTE_KIND: |
| 9182 | return ucs1lib_rsplit_whitespace( |
| 9183 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9184 | PyUnicode_GET_LENGTH(self), maxcount |
| 9185 | ); |
| 9186 | case PyUnicode_2BYTE_KIND: |
| 9187 | return ucs2lib_rsplit_whitespace( |
| 9188 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9189 | PyUnicode_GET_LENGTH(self), maxcount |
| 9190 | ); |
| 9191 | case PyUnicode_4BYTE_KIND: |
| 9192 | return ucs4lib_rsplit_whitespace( |
| 9193 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9194 | PyUnicode_GET_LENGTH(self), maxcount |
| 9195 | ); |
| 9196 | default: |
| 9197 | assert(0); |
| 9198 | return NULL; |
| 9199 | } |
| 9200 | |
| 9201 | if (PyUnicode_READY(substring) == -1) |
| 9202 | return NULL; |
| 9203 | |
| 9204 | kind1 = PyUnicode_KIND(self); |
| 9205 | kind2 = PyUnicode_KIND(substring); |
| 9206 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9207 | buf1 = PyUnicode_DATA(self); |
| 9208 | buf2 = PyUnicode_DATA(substring); |
| 9209 | if (kind1 != kind) |
| 9210 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9211 | if (!buf1) |
| 9212 | return NULL; |
| 9213 | if (kind2 != kind) |
| 9214 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9215 | if (!buf2) { |
| 9216 | if (kind1 != kind) PyMem_Free(buf1); |
| 9217 | return NULL; |
| 9218 | } |
| 9219 | len1 = PyUnicode_GET_LENGTH(self); |
| 9220 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9221 | |
| 9222 | switch(kind) { |
| 9223 | case PyUnicode_1BYTE_KIND: |
| 9224 | out = ucs1lib_rsplit( |
| 9225 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9226 | break; |
| 9227 | case PyUnicode_2BYTE_KIND: |
| 9228 | out = ucs2lib_rsplit( |
| 9229 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9230 | break; |
| 9231 | case PyUnicode_4BYTE_KIND: |
| 9232 | out = ucs4lib_rsplit( |
| 9233 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9234 | break; |
| 9235 | default: |
| 9236 | out = NULL; |
| 9237 | } |
| 9238 | if (kind1 != kind) |
| 9239 | PyMem_Free(buf1); |
| 9240 | if (kind2 != kind) |
| 9241 | PyMem_Free(buf2); |
| 9242 | return out; |
| 9243 | } |
| 9244 | |
| 9245 | static Py_ssize_t |
| 9246 | anylib_find(int kind, void *buf1, Py_ssize_t len1, |
| 9247 | void *buf2, Py_ssize_t len2, Py_ssize_t offset) |
| 9248 | { |
| 9249 | switch(kind) { |
| 9250 | case PyUnicode_1BYTE_KIND: |
| 9251 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
| 9252 | case PyUnicode_2BYTE_KIND: |
| 9253 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9254 | case PyUnicode_4BYTE_KIND: |
| 9255 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9256 | } |
| 9257 | assert(0); |
| 9258 | return -1; |
| 9259 | } |
| 9260 | |
| 9261 | static Py_ssize_t |
| 9262 | anylib_count(int kind, void* sbuf, Py_ssize_t slen, |
| 9263 | void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) |
| 9264 | { |
| 9265 | switch(kind) { |
| 9266 | case PyUnicode_1BYTE_KIND: |
| 9267 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9268 | case PyUnicode_2BYTE_KIND: |
| 9269 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9270 | case PyUnicode_4BYTE_KIND: |
| 9271 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9272 | } |
| 9273 | assert(0); |
| 9274 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9275 | } |
| 9276 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9277 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9278 | replace(PyObject *self, PyObject *str1, |
| 9279 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9280 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9281 | PyObject *u; |
| 9282 | char *sbuf = PyUnicode_DATA(self); |
| 9283 | char *buf1 = PyUnicode_DATA(str1); |
| 9284 | char *buf2 = PyUnicode_DATA(str2); |
| 9285 | int srelease = 0, release1 = 0, release2 = 0; |
| 9286 | int skind = PyUnicode_KIND(self); |
| 9287 | int kind1 = PyUnicode_KIND(str1); |
| 9288 | int kind2 = PyUnicode_KIND(str2); |
| 9289 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9290 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9291 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9292 | |
| 9293 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9294 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9295 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9296 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9297 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9298 | if (skind < kind1) |
| 9299 | /* substring too wide to be present */ |
| 9300 | goto nothing; |
| 9301 | |
| 9302 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 9303 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9304 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9305 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9306 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9307 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9308 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9309 | Py_UCS4 u1, u2, maxchar; |
| 9310 | int mayshrink, rkind; |
| 9311 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 9312 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 9313 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9314 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9315 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 9316 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9317 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 9318 | result string. */ |
| 9319 | mayshrink = maxchar > 127; |
| 9320 | if (u2 > maxchar) { |
| 9321 | maxchar = u2; |
| 9322 | mayshrink = 0; |
| 9323 | } |
| 9324 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9325 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9326 | goto error; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9327 | if (PyUnicode_CopyCharacters(u, 0, |
| 9328 | (PyObject*)self, 0, slen) < 0) |
| 9329 | { |
| 9330 | Py_DECREF(u); |
| 9331 | return NULL; |
| 9332 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9333 | rkind = PyUnicode_KIND(u); |
| 9334 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 9335 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9336 | if (--maxcount < 0) |
| 9337 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9338 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9339 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9340 | if (mayshrink) { |
| 9341 | PyObject *tmp = u; |
| 9342 | u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp), |
| 9343 | PyUnicode_GET_LENGTH(tmp)); |
| 9344 | Py_DECREF(tmp); |
| 9345 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9346 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9347 | int rkind = skind; |
| 9348 | char *res; |
| 9349 | if (kind1 < rkind) { |
| 9350 | /* widen substring */ |
| 9351 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9352 | if (!buf1) goto error; |
| 9353 | release1 = 1; |
| 9354 | } |
| 9355 | i = anylib_find(rkind, sbuf, slen, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9356 | if (i < 0) |
| 9357 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9358 | if (rkind > kind2) { |
| 9359 | /* widen replacement */ |
| 9360 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9361 | if (!buf2) goto error; |
| 9362 | release2 = 1; |
| 9363 | } |
| 9364 | else if (rkind < kind2) { |
| 9365 | /* widen self and buf1 */ |
| 9366 | rkind = kind2; |
| 9367 | if (release1) PyMem_Free(buf1); |
| 9368 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9369 | if (!sbuf) goto error; |
| 9370 | srelease = 1; |
| 9371 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9372 | if (!buf1) goto error; |
| 9373 | release1 = 1; |
| 9374 | } |
| 9375 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen)); |
| 9376 | if (!res) { |
| 9377 | PyErr_NoMemory(); |
| 9378 | goto error; |
| 9379 | } |
| 9380 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9381 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9382 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9383 | buf2, |
| 9384 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9385 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9386 | |
| 9387 | while ( --maxcount > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9388 | i = anylib_find(rkind, sbuf+PyUnicode_KIND_SIZE(rkind, i), |
| 9389 | slen-i, |
| 9390 | buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9391 | if (i == -1) |
| 9392 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9393 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9394 | buf2, |
| 9395 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9396 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9397 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9398 | |
| 9399 | u = PyUnicode_FromKindAndData(rkind, res, slen); |
| 9400 | PyMem_Free(res); |
| 9401 | if (!u) goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9402 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9403 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9404 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9405 | Py_ssize_t n, i, j, ires; |
| 9406 | Py_ssize_t product, new_size; |
| 9407 | int rkind = skind; |
| 9408 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9409 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9410 | if (kind1 < rkind) { |
| 9411 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9412 | if (!buf1) goto error; |
| 9413 | release1 = 1; |
| 9414 | } |
| 9415 | n = anylib_count(rkind, sbuf, slen, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9416 | if (n == 0) |
| 9417 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9418 | if (kind2 < rkind) { |
| 9419 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9420 | if (!buf2) goto error; |
| 9421 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9422 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9423 | else if (kind2 > rkind) { |
| 9424 | rkind = kind2; |
| 9425 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9426 | if (!sbuf) goto error; |
| 9427 | srelease = 1; |
| 9428 | if (release1) PyMem_Free(buf1); |
| 9429 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9430 | if (!buf1) goto error; |
| 9431 | release1 = 1; |
| 9432 | } |
| 9433 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9434 | PyUnicode_GET_LENGTH(str1))); */ |
| 9435 | product = n * (len2-len1); |
| 9436 | if ((product / (len2-len1)) != n) { |
| 9437 | PyErr_SetString(PyExc_OverflowError, |
| 9438 | "replace string is too long"); |
| 9439 | goto error; |
| 9440 | } |
| 9441 | new_size = slen + product; |
| 9442 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9443 | PyErr_SetString(PyExc_OverflowError, |
| 9444 | "replace string is too long"); |
| 9445 | goto error; |
| 9446 | } |
| 9447 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size)); |
| 9448 | if (!res) |
| 9449 | goto error; |
| 9450 | ires = i = 0; |
| 9451 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9452 | while (n-- > 0) { |
| 9453 | /* look for next match */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9454 | j = anylib_find(rkind, |
| 9455 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9456 | slen-i, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9457 | if (j == -1) |
| 9458 | break; |
| 9459 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9460 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9461 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9462 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9463 | PyUnicode_KIND_SIZE(rkind, j-i)); |
| 9464 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9465 | } |
| 9466 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9467 | if (len2 > 0) { |
| 9468 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9469 | buf2, |
| 9470 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9471 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9472 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9473 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9474 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9475 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9476 | /* copy tail [i:] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9477 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9478 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9479 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9480 | } else { |
| 9481 | /* interleave */ |
| 9482 | while (n > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9483 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9484 | buf2, |
| 9485 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9486 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9487 | if (--n <= 0) |
| 9488 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9489 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9490 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9491 | PyUnicode_KIND_SIZE(rkind, 1)); |
| 9492 | ires++; |
| 9493 | 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 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9496 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9497 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9498 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9499 | u = PyUnicode_FromKindAndData(rkind, res, new_size); |
Martin v. Löwis | 0b1d348 | 2011-10-01 16:35:40 +0200 | [diff] [blame] | 9500 | PyMem_Free(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9501 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9502 | if (srelease) |
| 9503 | PyMem_FREE(sbuf); |
| 9504 | if (release1) |
| 9505 | PyMem_FREE(buf1); |
| 9506 | if (release2) |
| 9507 | PyMem_FREE(buf2); |
| 9508 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9509 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9510 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9511 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9512 | if (srelease) |
| 9513 | PyMem_FREE(sbuf); |
| 9514 | if (release1) |
| 9515 | PyMem_FREE(buf1); |
| 9516 | if (release2) |
| 9517 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9518 | if (PyUnicode_CheckExact(self)) { |
| 9519 | Py_INCREF(self); |
| 9520 | return (PyObject *) self; |
| 9521 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9522 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9523 | error: |
| 9524 | if (srelease && sbuf) |
| 9525 | PyMem_FREE(sbuf); |
| 9526 | if (release1 && buf1) |
| 9527 | PyMem_FREE(buf1); |
| 9528 | if (release2 && buf2) |
| 9529 | PyMem_FREE(buf2); |
| 9530 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9531 | } |
| 9532 | |
| 9533 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9534 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9535 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9536 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9537 | \n\ |
| 9538 | 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] | 9539 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9540 | |
| 9541 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9542 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9543 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9544 | return fixup(self, fixtitle); |
| 9545 | } |
| 9546 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9547 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9548 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9549 | \n\ |
| 9550 | 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] | 9551 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9552 | |
| 9553 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9554 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9555 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9556 | return fixup(self, fixcapitalize); |
| 9557 | } |
| 9558 | |
| 9559 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9560 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9561 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9562 | \n\ |
| 9563 | 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] | 9564 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9565 | |
| 9566 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9567 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9568 | { |
| 9569 | PyObject *list; |
| 9570 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9571 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9572 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9573 | /* Split into words */ |
| 9574 | list = split(self, NULL, -1); |
| 9575 | if (!list) |
| 9576 | return NULL; |
| 9577 | |
| 9578 | /* Capitalize each word */ |
| 9579 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9580 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9581 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9582 | if (item == NULL) |
| 9583 | goto onError; |
| 9584 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9585 | PyList_SET_ITEM(list, i, item); |
| 9586 | } |
| 9587 | |
| 9588 | /* Join the words to form a new string */ |
| 9589 | item = PyUnicode_Join(NULL, list); |
| 9590 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9591 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9592 | Py_DECREF(list); |
| 9593 | return (PyObject *)item; |
| 9594 | } |
| 9595 | #endif |
| 9596 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9597 | /* Argument converter. Coerces to a single unicode character */ |
| 9598 | |
| 9599 | static int |
| 9600 | convert_uc(PyObject *obj, void *addr) |
| 9601 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9602 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9603 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9604 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9605 | uniobj = PyUnicode_FromObject(obj); |
| 9606 | if (uniobj == NULL) { |
| 9607 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9608 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9609 | return 0; |
| 9610 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9611 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9612 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9613 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9614 | Py_DECREF(uniobj); |
| 9615 | return 0; |
| 9616 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9617 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9618 | Py_DECREF(uniobj); |
| 9619 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9620 | } |
| 9621 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9622 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9623 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9624 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9625 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9626 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9627 | |
| 9628 | static PyObject * |
| 9629 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 9630 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9631 | Py_ssize_t marg, left; |
| 9632 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9633 | Py_UCS4 fillchar = ' '; |
| 9634 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9635 | 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] | 9636 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9637 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9638 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9639 | return NULL; |
| 9640 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9641 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9642 | Py_INCREF(self); |
| 9643 | return (PyObject*) self; |
| 9644 | } |
| 9645 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9646 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9647 | left = marg / 2 + (marg & width & 1); |
| 9648 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9649 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9650 | } |
| 9651 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9652 | #if 0 |
| 9653 | |
| 9654 | /* This code should go into some future Unicode collation support |
| 9655 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9656 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9657 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9658 | /* speedy UTF-16 code point order comparison */ |
| 9659 | /* gleaned from: */ |
| 9660 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 9661 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9662 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9663 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9664 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9665 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 9666 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9667 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9668 | }; |
| 9669 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9670 | static int |
| 9671 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9672 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9673 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9674 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9675 | Py_UNICODE *s1 = str1->str; |
| 9676 | Py_UNICODE *s2 = str2->str; |
| 9677 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9678 | len1 = str1->_base._base.length; |
| 9679 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9680 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9681 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9682 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9683 | |
| 9684 | c1 = *s1++; |
| 9685 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9686 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9687 | if (c1 > (1<<11) * 26) |
| 9688 | c1 += utf16Fixup[c1>>11]; |
| 9689 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9690 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9691 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9692 | |
| 9693 | if (c1 != c2) |
| 9694 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9695 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9696 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9697 | } |
| 9698 | |
| 9699 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9700 | } |
| 9701 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9702 | #else |
| 9703 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9704 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 9705 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9706 | static int |
| 9707 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9708 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9709 | int kind1, kind2; |
| 9710 | void *data1, *data2; |
| 9711 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9712 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9713 | kind1 = PyUnicode_KIND(str1); |
| 9714 | kind2 = PyUnicode_KIND(str2); |
| 9715 | data1 = PyUnicode_DATA(str1); |
| 9716 | data2 = PyUnicode_DATA(str2); |
| 9717 | len1 = PyUnicode_GET_LENGTH(str1); |
| 9718 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9719 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9720 | for (i = 0; i < len1 && i < len2; ++i) { |
| 9721 | Py_UCS4 c1, c2; |
| 9722 | c1 = PyUnicode_READ(kind1, data1, i); |
| 9723 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9724 | |
| 9725 | if (c1 != c2) |
| 9726 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9727 | } |
| 9728 | |
| 9729 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9730 | } |
| 9731 | |
| 9732 | #endif |
| 9733 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9734 | int |
| 9735 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9736 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9737 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9738 | if (PyUnicode_READY(left) == -1 || |
| 9739 | PyUnicode_READY(right) == -1) |
| 9740 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9741 | return unicode_compare((PyUnicodeObject *)left, |
| 9742 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9743 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9744 | PyErr_Format(PyExc_TypeError, |
| 9745 | "Can't compare %.100s and %.100s", |
| 9746 | left->ob_type->tp_name, |
| 9747 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9748 | return -1; |
| 9749 | } |
| 9750 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9751 | int |
| 9752 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 9753 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9754 | Py_ssize_t i; |
| 9755 | int kind; |
| 9756 | void *data; |
| 9757 | Py_UCS4 chr; |
| 9758 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 9759 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9760 | if (PyUnicode_READY(uni) == -1) |
| 9761 | return -1; |
| 9762 | kind = PyUnicode_KIND(uni); |
| 9763 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9764 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9765 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 9766 | if (chr != str[i]) |
| 9767 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 9768 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 9769 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9770 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9771 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9772 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9773 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9774 | return 0; |
| 9775 | } |
| 9776 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9777 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9778 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9779 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9780 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9781 | PyObject * |
| 9782 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9783 | { |
| 9784 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9785 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9786 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9787 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9788 | if (PyUnicode_READY(left) == -1 || |
| 9789 | PyUnicode_READY(right) == -1) |
| 9790 | return NULL; |
| 9791 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 9792 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9793 | if (op == Py_EQ) { |
| 9794 | Py_INCREF(Py_False); |
| 9795 | return Py_False; |
| 9796 | } |
| 9797 | if (op == Py_NE) { |
| 9798 | Py_INCREF(Py_True); |
| 9799 | return Py_True; |
| 9800 | } |
| 9801 | } |
| 9802 | if (left == right) |
| 9803 | result = 0; |
| 9804 | else |
| 9805 | result = unicode_compare((PyUnicodeObject *)left, |
| 9806 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9807 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9808 | /* Convert the return value to a Boolean */ |
| 9809 | switch (op) { |
| 9810 | case Py_EQ: |
| 9811 | v = TEST_COND(result == 0); |
| 9812 | break; |
| 9813 | case Py_NE: |
| 9814 | v = TEST_COND(result != 0); |
| 9815 | break; |
| 9816 | case Py_LE: |
| 9817 | v = TEST_COND(result <= 0); |
| 9818 | break; |
| 9819 | case Py_GE: |
| 9820 | v = TEST_COND(result >= 0); |
| 9821 | break; |
| 9822 | case Py_LT: |
| 9823 | v = TEST_COND(result == -1); |
| 9824 | break; |
| 9825 | case Py_GT: |
| 9826 | v = TEST_COND(result == 1); |
| 9827 | break; |
| 9828 | default: |
| 9829 | PyErr_BadArgument(); |
| 9830 | return NULL; |
| 9831 | } |
| 9832 | Py_INCREF(v); |
| 9833 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9834 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9835 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 9836 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9837 | } |
| 9838 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9839 | int |
| 9840 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9841 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9842 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9843 | int kind1, kind2, kind; |
| 9844 | void *buf1, *buf2; |
| 9845 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9846 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9847 | |
| 9848 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9849 | sub = PyUnicode_FromObject(element); |
| 9850 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9851 | PyErr_Format(PyExc_TypeError, |
| 9852 | "'in <string>' requires string as left operand, not %s", |
| 9853 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9854 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9855 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9856 | if (PyUnicode_READY(sub) == -1) |
| 9857 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9858 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9859 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9860 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9861 | Py_DECREF(sub); |
| 9862 | return -1; |
| 9863 | } |
| 9864 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9865 | kind1 = PyUnicode_KIND(str); |
| 9866 | kind2 = PyUnicode_KIND(sub); |
| 9867 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9868 | buf1 = PyUnicode_DATA(str); |
| 9869 | buf2 = PyUnicode_DATA(sub); |
| 9870 | if (kind1 != kind) |
| 9871 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 9872 | if (!buf1) { |
| 9873 | Py_DECREF(sub); |
| 9874 | return -1; |
| 9875 | } |
| 9876 | if (kind2 != kind) |
| 9877 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 9878 | if (!buf2) { |
| 9879 | Py_DECREF(sub); |
| 9880 | if (kind1 != kind) PyMem_Free(buf1); |
| 9881 | return -1; |
| 9882 | } |
| 9883 | len1 = PyUnicode_GET_LENGTH(str); |
| 9884 | len2 = PyUnicode_GET_LENGTH(sub); |
| 9885 | |
| 9886 | switch(kind) { |
| 9887 | case PyUnicode_1BYTE_KIND: |
| 9888 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9889 | break; |
| 9890 | case PyUnicode_2BYTE_KIND: |
| 9891 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9892 | break; |
| 9893 | case PyUnicode_4BYTE_KIND: |
| 9894 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9895 | break; |
| 9896 | default: |
| 9897 | result = -1; |
| 9898 | assert(0); |
| 9899 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9900 | |
| 9901 | Py_DECREF(str); |
| 9902 | Py_DECREF(sub); |
| 9903 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9904 | if (kind1 != kind) |
| 9905 | PyMem_Free(buf1); |
| 9906 | if (kind2 != kind) |
| 9907 | PyMem_Free(buf2); |
| 9908 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9909 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9910 | } |
| 9911 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9912 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 9913 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9914 | PyObject * |
| 9915 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9916 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9917 | PyObject *u = NULL, *v = NULL, *w; |
| 9918 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9919 | |
| 9920 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9921 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9922 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9923 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9924 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9925 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9926 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9927 | |
| 9928 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9929 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9930 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9931 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9932 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9933 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9934 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9935 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9936 | } |
| 9937 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9938 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 9939 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9940 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9941 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9942 | w = PyUnicode_New( |
| 9943 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 9944 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9945 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9946 | goto onError; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9947 | if (PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)) < 0) |
| 9948 | goto onError; |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9949 | if (PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9950 | v, 0, |
| 9951 | PyUnicode_GET_LENGTH(v)) < 0) |
| 9952 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9953 | Py_DECREF(u); |
| 9954 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9955 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9956 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9957 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9958 | Py_XDECREF(u); |
| 9959 | Py_XDECREF(v); |
| 9960 | return NULL; |
| 9961 | } |
| 9962 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9963 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9964 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9965 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9966 | PyObject *left, *res; |
| 9967 | |
| 9968 | if (p_left == NULL) { |
| 9969 | if (!PyErr_Occurred()) |
| 9970 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9971 | return; |
| 9972 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9973 | left = *p_left; |
| 9974 | if (right == NULL || !PyUnicode_Check(left)) { |
| 9975 | if (!PyErr_Occurred()) |
| 9976 | PyErr_BadInternalCall(); |
| 9977 | goto error; |
| 9978 | } |
| 9979 | |
| 9980 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 9981 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 9982 | && unicode_resizable(left) |
| 9983 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 9984 | || _PyUnicode_WSTR(left) != NULL)) |
| 9985 | { |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 9986 | Py_ssize_t left_len, right_len, new_len; |
| 9987 | #ifdef Py_DEBUG |
| 9988 | Py_ssize_t copied; |
| 9989 | #endif |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9990 | |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9991 | if (PyUnicode_READY(left)) |
| 9992 | goto error; |
| 9993 | if (PyUnicode_READY(right)) |
| 9994 | goto error; |
| 9995 | |
| 9996 | /* FIXME: support ascii+latin1, PyASCIIObject => PyCompactUnicodeObject */ |
| 9997 | if (PyUnicode_MAX_CHAR_VALUE(right) <= PyUnicode_MAX_CHAR_VALUE(left)) |
| 9998 | { |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 9999 | left_len = PyUnicode_GET_LENGTH(left); |
| 10000 | right_len = PyUnicode_GET_LENGTH(right); |
| 10001 | if (left_len > PY_SSIZE_T_MAX - right_len) { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10002 | PyErr_SetString(PyExc_OverflowError, |
| 10003 | "strings are too large to concat"); |
| 10004 | goto error; |
| 10005 | } |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 10006 | new_len = left_len + right_len; |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10007 | |
| 10008 | /* Now we own the last reference to 'left', so we can resize it |
| 10009 | * in-place. |
| 10010 | */ |
| 10011 | if (unicode_resize(&left, new_len) != 0) { |
| 10012 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 10013 | * deallocated so it cannot be put back into |
| 10014 | * 'variable'. The MemoryError is raised when there |
| 10015 | * is no value in 'variable', which might (very |
| 10016 | * remotely) be a cause of incompatibilities. |
| 10017 | */ |
| 10018 | goto error; |
| 10019 | } |
| 10020 | /* copy 'right' into the newly allocated area of 'left' */ |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 10021 | #ifdef Py_DEBUG |
| 10022 | copied = PyUnicode_CopyCharacters(left, left_len, |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10023 | right, 0, |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 10024 | right_len); |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10025 | assert(0 <= copied); |
Victor Stinner | b803895 | 2011-10-03 23:27:56 +0200 | [diff] [blame] | 10026 | #else |
| 10027 | PyUnicode_CopyCharacters(left, left_len, right, 0, right_len); |
| 10028 | #endif |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 10029 | *p_left = left; |
| 10030 | return; |
| 10031 | } |
| 10032 | } |
| 10033 | |
| 10034 | res = PyUnicode_Concat(left, right); |
| 10035 | if (res == NULL) |
| 10036 | goto error; |
| 10037 | Py_DECREF(left); |
| 10038 | *p_left = res; |
| 10039 | return; |
| 10040 | |
| 10041 | error: |
| 10042 | Py_DECREF(*p_left); |
| 10043 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10044 | } |
| 10045 | |
| 10046 | void |
| 10047 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 10048 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10049 | PyUnicode_Append(pleft, right); |
| 10050 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 10051 | } |
| 10052 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10053 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10054 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10055 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10056 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10057 | 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] | 10058 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10059 | |
| 10060 | static PyObject * |
| 10061 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 10062 | { |
| 10063 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10064 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10065 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10066 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10067 | int kind1, kind2, kind; |
| 10068 | void *buf1, *buf2; |
| 10069 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10070 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10071 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10072 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10073 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10074 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10075 | kind1 = PyUnicode_KIND(self); |
| 10076 | kind2 = PyUnicode_KIND(substring); |
| 10077 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10078 | buf1 = PyUnicode_DATA(self); |
| 10079 | buf2 = PyUnicode_DATA(substring); |
| 10080 | if (kind1 != kind) |
| 10081 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10082 | if (!buf1) { |
| 10083 | Py_DECREF(substring); |
| 10084 | return NULL; |
| 10085 | } |
| 10086 | if (kind2 != kind) |
| 10087 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10088 | if (!buf2) { |
| 10089 | Py_DECREF(substring); |
| 10090 | if (kind1 != kind) PyMem_Free(buf1); |
| 10091 | return NULL; |
| 10092 | } |
| 10093 | len1 = PyUnicode_GET_LENGTH(self); |
| 10094 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10095 | |
| 10096 | ADJUST_INDICES(start, end, len1); |
| 10097 | switch(kind) { |
| 10098 | case PyUnicode_1BYTE_KIND: |
| 10099 | iresult = ucs1lib_count( |
| 10100 | ((Py_UCS1*)buf1) + start, end - start, |
| 10101 | buf2, len2, PY_SSIZE_T_MAX |
| 10102 | ); |
| 10103 | break; |
| 10104 | case PyUnicode_2BYTE_KIND: |
| 10105 | iresult = ucs2lib_count( |
| 10106 | ((Py_UCS2*)buf1) + start, end - start, |
| 10107 | buf2, len2, PY_SSIZE_T_MAX |
| 10108 | ); |
| 10109 | break; |
| 10110 | case PyUnicode_4BYTE_KIND: |
| 10111 | iresult = ucs4lib_count( |
| 10112 | ((Py_UCS4*)buf1) + start, end - start, |
| 10113 | buf2, len2, PY_SSIZE_T_MAX |
| 10114 | ); |
| 10115 | break; |
| 10116 | default: |
| 10117 | assert(0); iresult = 0; |
| 10118 | } |
| 10119 | |
| 10120 | result = PyLong_FromSsize_t(iresult); |
| 10121 | |
| 10122 | if (kind1 != kind) |
| 10123 | PyMem_Free(buf1); |
| 10124 | if (kind2 != kind) |
| 10125 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10126 | |
| 10127 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10128 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10129 | return result; |
| 10130 | } |
| 10131 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10132 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10133 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10134 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10135 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10136 | 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] | 10137 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10138 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10139 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10140 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10141 | |
| 10142 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10143 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10144 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10145 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10146 | char *encoding = NULL; |
| 10147 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10148 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10149 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10150 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10151 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 10152 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10153 | } |
| 10154 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10155 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10156 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10157 | \n\ |
| 10158 | 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] | 10159 | 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] | 10160 | |
| 10161 | static PyObject* |
| 10162 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 10163 | { |
| 10164 | Py_UNICODE *e; |
| 10165 | Py_UNICODE *p; |
| 10166 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10167 | Py_UNICODE *qe; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10168 | Py_ssize_t i, j, incr, wstr_length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10169 | PyUnicodeObject *u; |
| 10170 | int tabsize = 8; |
| 10171 | |
| 10172 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10173 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10174 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10175 | if (PyUnicode_AsUnicodeAndSize((PyObject *)self, &wstr_length) == NULL) |
| 10176 | return NULL; |
| 10177 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10178 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10179 | i = 0; /* chars up to and including most recent \n or \r */ |
| 10180 | 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] | 10181 | e = _PyUnicode_WSTR(self) + wstr_length; /* end of input */ |
| 10182 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10183 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10184 | if (tabsize > 0) { |
| 10185 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 10186 | if (j > PY_SSIZE_T_MAX - incr) |
| 10187 | goto overflow1; |
| 10188 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10189 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10190 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10191 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10192 | if (j > PY_SSIZE_T_MAX - 1) |
| 10193 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10194 | j++; |
| 10195 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10196 | if (i > PY_SSIZE_T_MAX - j) |
| 10197 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10198 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10199 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10200 | } |
| 10201 | } |
| 10202 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10203 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10204 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10205 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10206 | /* Second pass: create output string and fill it */ |
| 10207 | u = _PyUnicode_New(i + j); |
| 10208 | if (!u) |
| 10209 | return NULL; |
| 10210 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10211 | j = 0; /* same as in first pass */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10212 | q = _PyUnicode_WSTR(u); /* next output char */ |
| 10213 | qe = _PyUnicode_WSTR(u) + PyUnicode_GET_SIZE(u); /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10214 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10215 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10216 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10217 | if (tabsize > 0) { |
| 10218 | i = tabsize - (j % tabsize); |
| 10219 | j += i; |
| 10220 | while (i--) { |
| 10221 | if (q >= qe) |
| 10222 | goto overflow2; |
| 10223 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10224 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10225 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10226 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10227 | else { |
| 10228 | if (q >= qe) |
| 10229 | goto overflow2; |
| 10230 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10231 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10232 | if (*p == '\n' || *p == '\r') |
| 10233 | j = 0; |
| 10234 | } |
| 10235 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 10236 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10237 | Py_DECREF(u); |
| 10238 | return NULL; |
| 10239 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10240 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10241 | |
| 10242 | overflow2: |
| 10243 | Py_DECREF(u); |
| 10244 | overflow1: |
| 10245 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10246 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10247 | } |
| 10248 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10249 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10250 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10251 | \n\ |
| 10252 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10253 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10254 | arguments start and end are interpreted as in slice notation.\n\ |
| 10255 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10256 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10257 | |
| 10258 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10259 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10260 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10261 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10262 | Py_ssize_t start; |
| 10263 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10264 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10265 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10266 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10267 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10268 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10269 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10270 | if (PyUnicode_READY(self) == -1) |
| 10271 | return NULL; |
| 10272 | if (PyUnicode_READY(substring) == -1) |
| 10273 | return NULL; |
| 10274 | |
| 10275 | result = any_find_slice( |
| 10276 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 10277 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10278 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10279 | |
| 10280 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10281 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10282 | if (result == -2) |
| 10283 | return NULL; |
| 10284 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10285 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10286 | } |
| 10287 | |
| 10288 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10289 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10290 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10291 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 10292 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10293 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10294 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10295 | } |
| 10296 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10297 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10298 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10299 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 10300 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10301 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10302 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10303 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10304 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10305 | if (_PyUnicode_HASH(self) != -1) |
| 10306 | return _PyUnicode_HASH(self); |
| 10307 | if (PyUnicode_READY(self) == -1) |
| 10308 | return -1; |
| 10309 | len = PyUnicode_GET_LENGTH(self); |
| 10310 | |
| 10311 | /* The hash function as a macro, gets expanded three times below. */ |
| 10312 | #define HASH(P) \ |
| 10313 | x = (Py_uhash_t)*P << 7; \ |
| 10314 | while (--len >= 0) \ |
| 10315 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 10316 | |
| 10317 | switch (PyUnicode_KIND(self)) { |
| 10318 | case PyUnicode_1BYTE_KIND: { |
| 10319 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10320 | HASH(c); |
| 10321 | break; |
| 10322 | } |
| 10323 | case PyUnicode_2BYTE_KIND: { |
| 10324 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10325 | HASH(s); |
| 10326 | break; |
| 10327 | } |
| 10328 | default: { |
| 10329 | Py_UCS4 *l; |
| 10330 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10331 | "Impossible switch case in unicode_hash"); |
| 10332 | l = PyUnicode_4BYTE_DATA(self); |
| 10333 | HASH(l); |
| 10334 | break; |
| 10335 | } |
| 10336 | } |
| 10337 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 10338 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10339 | if (x == -1) |
| 10340 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10341 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10342 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10343 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10344 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10345 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10346 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10347 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10348 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10349 | 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] | 10350 | |
| 10351 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10352 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10353 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10354 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10355 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10356 | Py_ssize_t start; |
| 10357 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10358 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10359 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10360 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10361 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10362 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10363 | if (PyUnicode_READY(self) == -1) |
| 10364 | return NULL; |
| 10365 | if (PyUnicode_READY(substring) == -1) |
| 10366 | return NULL; |
| 10367 | |
| 10368 | result = any_find_slice( |
| 10369 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 10370 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10371 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10372 | |
| 10373 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10374 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10375 | if (result == -2) |
| 10376 | return NULL; |
| 10377 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10378 | if (result < 0) { |
| 10379 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10380 | return NULL; |
| 10381 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10382 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10383 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10384 | } |
| 10385 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10386 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10387 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10388 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10389 | 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] | 10390 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10391 | |
| 10392 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10393 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10394 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10395 | Py_ssize_t i, length; |
| 10396 | int kind; |
| 10397 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10398 | int cased; |
| 10399 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10400 | if (PyUnicode_READY(self) == -1) |
| 10401 | return NULL; |
| 10402 | length = PyUnicode_GET_LENGTH(self); |
| 10403 | kind = PyUnicode_KIND(self); |
| 10404 | data = PyUnicode_DATA(self); |
| 10405 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10406 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10407 | if (length == 1) |
| 10408 | return PyBool_FromLong( |
| 10409 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10410 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10411 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10412 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10413 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10414 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10415 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10416 | for (i = 0; i < length; i++) { |
| 10417 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10418 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10419 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10420 | return PyBool_FromLong(0); |
| 10421 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 10422 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10423 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10424 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10425 | } |
| 10426 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10427 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10428 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10429 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10430 | 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] | 10431 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10432 | |
| 10433 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10434 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10435 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10436 | Py_ssize_t i, length; |
| 10437 | int kind; |
| 10438 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10439 | int cased; |
| 10440 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10441 | if (PyUnicode_READY(self) == -1) |
| 10442 | return NULL; |
| 10443 | length = PyUnicode_GET_LENGTH(self); |
| 10444 | kind = PyUnicode_KIND(self); |
| 10445 | data = PyUnicode_DATA(self); |
| 10446 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10447 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10448 | if (length == 1) |
| 10449 | return PyBool_FromLong( |
| 10450 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10451 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10452 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10453 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10454 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10455 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10456 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10457 | for (i = 0; i < length; i++) { |
| 10458 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10459 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10460 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10461 | return PyBool_FromLong(0); |
| 10462 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 10463 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10464 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10465 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10466 | } |
| 10467 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10468 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10469 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10470 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10471 | Return True if S is a titlecased string and there is at least one\n\ |
| 10472 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 10473 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 10474 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10475 | |
| 10476 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10477 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10478 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10479 | Py_ssize_t i, length; |
| 10480 | int kind; |
| 10481 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10482 | int cased, previous_is_cased; |
| 10483 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10484 | if (PyUnicode_READY(self) == -1) |
| 10485 | return NULL; |
| 10486 | length = PyUnicode_GET_LENGTH(self); |
| 10487 | kind = PyUnicode_KIND(self); |
| 10488 | data = PyUnicode_DATA(self); |
| 10489 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10490 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10491 | if (length == 1) { |
| 10492 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10493 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10494 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10495 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10496 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10497 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10498 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10499 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10500 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10501 | cased = 0; |
| 10502 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10503 | for (i = 0; i < length; i++) { |
| 10504 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10505 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10506 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10507 | if (previous_is_cased) |
| 10508 | return PyBool_FromLong(0); |
| 10509 | previous_is_cased = 1; |
| 10510 | cased = 1; |
| 10511 | } |
| 10512 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10513 | if (!previous_is_cased) |
| 10514 | return PyBool_FromLong(0); |
| 10515 | previous_is_cased = 1; |
| 10516 | cased = 1; |
| 10517 | } |
| 10518 | else |
| 10519 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10520 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10521 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10522 | } |
| 10523 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10524 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10525 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10526 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10527 | Return True if all characters in S are whitespace\n\ |
| 10528 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10529 | |
| 10530 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10531 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10532 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10533 | Py_ssize_t i, length; |
| 10534 | int kind; |
| 10535 | void *data; |
| 10536 | |
| 10537 | if (PyUnicode_READY(self) == -1) |
| 10538 | return NULL; |
| 10539 | length = PyUnicode_GET_LENGTH(self); |
| 10540 | kind = PyUnicode_KIND(self); |
| 10541 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10542 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10543 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10544 | if (length == 1) |
| 10545 | return PyBool_FromLong( |
| 10546 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10547 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10548 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10549 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10550 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10551 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10552 | for (i = 0; i < length; i++) { |
| 10553 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10554 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10555 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10556 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10557 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10558 | } |
| 10559 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10560 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10561 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10562 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10563 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10564 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10565 | |
| 10566 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10567 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10568 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10569 | Py_ssize_t i, length; |
| 10570 | int kind; |
| 10571 | void *data; |
| 10572 | |
| 10573 | if (PyUnicode_READY(self) == -1) |
| 10574 | return NULL; |
| 10575 | length = PyUnicode_GET_LENGTH(self); |
| 10576 | kind = PyUnicode_KIND(self); |
| 10577 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10578 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10579 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10580 | if (length == 1) |
| 10581 | return PyBool_FromLong( |
| 10582 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10583 | |
| 10584 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10585 | if (length == 0) |
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 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10588 | for (i = 0; i < length; i++) { |
| 10589 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10590 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10591 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10592 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10593 | } |
| 10594 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10595 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10596 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10597 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10598 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10599 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10600 | |
| 10601 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10602 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10603 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10604 | int kind; |
| 10605 | void *data; |
| 10606 | Py_ssize_t len, i; |
| 10607 | |
| 10608 | if (PyUnicode_READY(self) == -1) |
| 10609 | return NULL; |
| 10610 | |
| 10611 | kind = PyUnicode_KIND(self); |
| 10612 | data = PyUnicode_DATA(self); |
| 10613 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10614 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10615 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10616 | if (len == 1) { |
| 10617 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10618 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10619 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10620 | |
| 10621 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10622 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10623 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10624 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10625 | for (i = 0; i < len; i++) { |
| 10626 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10627 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10628 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10629 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10630 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10631 | } |
| 10632 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10633 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10634 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10635 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10636 | 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] | 10637 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10638 | |
| 10639 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10640 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10641 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10642 | Py_ssize_t i, length; |
| 10643 | int kind; |
| 10644 | void *data; |
| 10645 | |
| 10646 | if (PyUnicode_READY(self) == -1) |
| 10647 | return NULL; |
| 10648 | length = PyUnicode_GET_LENGTH(self); |
| 10649 | kind = PyUnicode_KIND(self); |
| 10650 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10651 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10652 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10653 | if (length == 1) |
| 10654 | return PyBool_FromLong( |
| 10655 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10656 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10657 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10658 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10659 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10660 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10661 | for (i = 0; i < length; i++) { |
| 10662 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10663 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10664 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10665 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10666 | } |
| 10667 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10668 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10669 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10670 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10671 | Return True if all characters in S are digits\n\ |
| 10672 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10673 | |
| 10674 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10675 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10676 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10677 | Py_ssize_t i, length; |
| 10678 | int kind; |
| 10679 | void *data; |
| 10680 | |
| 10681 | if (PyUnicode_READY(self) == -1) |
| 10682 | return NULL; |
| 10683 | length = PyUnicode_GET_LENGTH(self); |
| 10684 | kind = PyUnicode_KIND(self); |
| 10685 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10686 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10687 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10688 | if (length == 1) { |
| 10689 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10690 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 10691 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10692 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10693 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10694 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10695 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10697 | for (i = 0; i < length; i++) { |
| 10698 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10699 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10700 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10701 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10702 | } |
| 10703 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10704 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10705 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10706 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10707 | 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] | 10708 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10709 | |
| 10710 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10711 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10712 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10713 | Py_ssize_t i, length; |
| 10714 | int kind; |
| 10715 | void *data; |
| 10716 | |
| 10717 | if (PyUnicode_READY(self) == -1) |
| 10718 | return NULL; |
| 10719 | length = PyUnicode_GET_LENGTH(self); |
| 10720 | kind = PyUnicode_KIND(self); |
| 10721 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10722 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10723 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10724 | if (length == 1) |
| 10725 | return PyBool_FromLong( |
| 10726 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10727 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10728 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10729 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10730 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10731 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10732 | for (i = 0; i < length; i++) { |
| 10733 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10734 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10735 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10736 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10737 | } |
| 10738 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10739 | int |
| 10740 | PyUnicode_IsIdentifier(PyObject *self) |
| 10741 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10742 | int kind; |
| 10743 | void *data; |
| 10744 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10745 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10746 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10747 | if (PyUnicode_READY(self) == -1) { |
| 10748 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10749 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10750 | } |
| 10751 | |
| 10752 | /* Special case for empty strings */ |
| 10753 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10754 | return 0; |
| 10755 | kind = PyUnicode_KIND(self); |
| 10756 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10757 | |
| 10758 | /* PEP 3131 says that the first character must be in |
| 10759 | XID_Start and subsequent characters in XID_Continue, |
| 10760 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10761 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10762 | letters, digits, underscore). However, given the current |
| 10763 | definition of XID_Start and XID_Continue, it is sufficient |
| 10764 | to check just for these, except that _ must be allowed |
| 10765 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10766 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 10767 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10768 | return 0; |
| 10769 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 10770 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10771 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10772 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10773 | return 1; |
| 10774 | } |
| 10775 | |
| 10776 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10777 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10778 | \n\ |
| 10779 | Return True if S is a valid identifier according\n\ |
| 10780 | to the language definition."); |
| 10781 | |
| 10782 | static PyObject* |
| 10783 | unicode_isidentifier(PyObject *self) |
| 10784 | { |
| 10785 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 10786 | } |
| 10787 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10788 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10789 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10790 | \n\ |
| 10791 | Return True if all characters in S are considered\n\ |
| 10792 | printable in repr() or S is empty, False otherwise."); |
| 10793 | |
| 10794 | static PyObject* |
| 10795 | unicode_isprintable(PyObject *self) |
| 10796 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10797 | Py_ssize_t i, length; |
| 10798 | int kind; |
| 10799 | void *data; |
| 10800 | |
| 10801 | if (PyUnicode_READY(self) == -1) |
| 10802 | return NULL; |
| 10803 | length = PyUnicode_GET_LENGTH(self); |
| 10804 | kind = PyUnicode_KIND(self); |
| 10805 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10806 | |
| 10807 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10808 | if (length == 1) |
| 10809 | return PyBool_FromLong( |
| 10810 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10811 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10812 | for (i = 0; i < length; i++) { |
| 10813 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10814 | Py_RETURN_FALSE; |
| 10815 | } |
| 10816 | } |
| 10817 | Py_RETURN_TRUE; |
| 10818 | } |
| 10819 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10820 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 10821 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10822 | \n\ |
| 10823 | 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] | 10824 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10825 | |
| 10826 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10827 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10828 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10829 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10830 | } |
| 10831 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10832 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10833 | unicode_length(PyUnicodeObject *self) |
| 10834 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10835 | if (PyUnicode_READY(self) == -1) |
| 10836 | return -1; |
| 10837 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10838 | } |
| 10839 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10840 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10841 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10842 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10843 | 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] | 10844 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10845 | |
| 10846 | static PyObject * |
| 10847 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 10848 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10849 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10850 | Py_UCS4 fillchar = ' '; |
| 10851 | |
| 10852 | if (PyUnicode_READY(self) == -1) |
| 10853 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10854 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10855 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10856 | return NULL; |
| 10857 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10858 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10859 | Py_INCREF(self); |
| 10860 | return (PyObject*) self; |
| 10861 | } |
| 10862 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10863 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10864 | } |
| 10865 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10866 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10867 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10868 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10869 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10870 | |
| 10871 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10872 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10873 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10874 | return fixup(self, fixlower); |
| 10875 | } |
| 10876 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10877 | #define LEFTSTRIP 0 |
| 10878 | #define RIGHTSTRIP 1 |
| 10879 | #define BOTHSTRIP 2 |
| 10880 | |
| 10881 | /* Arrays indexed by above */ |
| 10882 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 10883 | |
| 10884 | #define STRIPNAME(i) (stripformat[i]+3) |
| 10885 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10886 | /* externally visible for str.strip(unicode) */ |
| 10887 | PyObject * |
| 10888 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 10889 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10890 | void *data; |
| 10891 | int kind; |
| 10892 | Py_ssize_t i, j, len; |
| 10893 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10894 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10895 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 10896 | return NULL; |
| 10897 | |
| 10898 | kind = PyUnicode_KIND(self); |
| 10899 | data = PyUnicode_DATA(self); |
| 10900 | len = PyUnicode_GET_LENGTH(self); |
| 10901 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 10902 | PyUnicode_DATA(sepobj), |
| 10903 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10904 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10905 | i = 0; |
| 10906 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10907 | while (i < len && |
| 10908 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10909 | i++; |
| 10910 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10911 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10912 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10913 | j = len; |
| 10914 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10915 | do { |
| 10916 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10917 | } while (j >= i && |
| 10918 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10919 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10920 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10921 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10922 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10923 | } |
| 10924 | |
| 10925 | PyObject* |
| 10926 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 10927 | { |
| 10928 | unsigned char *data; |
| 10929 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10930 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10931 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10932 | if (PyUnicode_READY(self) == -1) |
| 10933 | return NULL; |
| 10934 | |
| 10935 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 10936 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10937 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10938 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10939 | if (PyUnicode_CheckExact(self)) { |
| 10940 | Py_INCREF(self); |
| 10941 | return self; |
| 10942 | } |
| 10943 | else |
| 10944 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10945 | } |
| 10946 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10947 | length = end - start; |
| 10948 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10949 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10950 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10951 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10952 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10953 | return NULL; |
| 10954 | } |
| 10955 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10956 | kind = PyUnicode_KIND(self); |
| 10957 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10958 | return PyUnicode_FromKindAndData(kind, |
| 10959 | data + PyUnicode_KIND_SIZE(kind, start), |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10960 | length); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10961 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10962 | |
| 10963 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10964 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10965 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10966 | int kind; |
| 10967 | void *data; |
| 10968 | Py_ssize_t len, i, j; |
| 10969 | |
| 10970 | if (PyUnicode_READY(self) == -1) |
| 10971 | return NULL; |
| 10972 | |
| 10973 | kind = PyUnicode_KIND(self); |
| 10974 | data = PyUnicode_DATA(self); |
| 10975 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10976 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10977 | i = 0; |
| 10978 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10979 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10980 | i++; |
| 10981 | } |
| 10982 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10983 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10984 | j = len; |
| 10985 | if (striptype != LEFTSTRIP) { |
| 10986 | do { |
| 10987 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10988 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10989 | j++; |
| 10990 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10991 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10992 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10993 | } |
| 10994 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10995 | |
| 10996 | static PyObject * |
| 10997 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 10998 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10999 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11000 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11001 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 11002 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11003 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11004 | if (sep != NULL && sep != Py_None) { |
| 11005 | if (PyUnicode_Check(sep)) |
| 11006 | return _PyUnicode_XStrip(self, striptype, sep); |
| 11007 | else { |
| 11008 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11009 | "%s arg must be None or str", |
| 11010 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11011 | return NULL; |
| 11012 | } |
| 11013 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11014 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11015 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11016 | } |
| 11017 | |
| 11018 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11019 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11020 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11021 | \n\ |
| 11022 | Return a copy of the string S with leading and trailing\n\ |
| 11023 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11024 | 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] | 11025 | |
| 11026 | static PyObject * |
| 11027 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 11028 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11029 | if (PyTuple_GET_SIZE(args) == 0) |
| 11030 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 11031 | else |
| 11032 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11033 | } |
| 11034 | |
| 11035 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11036 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11037 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11038 | \n\ |
| 11039 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11040 | 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] | 11041 | |
| 11042 | static PyObject * |
| 11043 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 11044 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11045 | if (PyTuple_GET_SIZE(args) == 0) |
| 11046 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 11047 | else |
| 11048 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11049 | } |
| 11050 | |
| 11051 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11052 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11053 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11054 | \n\ |
| 11055 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11056 | 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] | 11057 | |
| 11058 | static PyObject * |
| 11059 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 11060 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11061 | if (PyTuple_GET_SIZE(args) == 0) |
| 11062 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11063 | else |
| 11064 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11065 | } |
| 11066 | |
| 11067 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11068 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11069 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11070 | { |
| 11071 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11072 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11073 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11074 | if (len < 1) { |
| 11075 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11076 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11077 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11078 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11079 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11080 | /* no repeat, return original string */ |
| 11081 | Py_INCREF(str); |
| 11082 | return (PyObject*) str; |
| 11083 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11084 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11085 | if (PyUnicode_READY(str) == -1) |
| 11086 | return NULL; |
| 11087 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11088 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11089 | PyErr_SetString(PyExc_OverflowError, |
| 11090 | "repeated string is too long"); |
| 11091 | return NULL; |
| 11092 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11093 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11094 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11095 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11096 | if (!u) |
| 11097 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11098 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11099 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11100 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11101 | const int kind = PyUnicode_KIND(str); |
| 11102 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11103 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11104 | if (kind == PyUnicode_1BYTE_KIND) |
| 11105 | memset(to, (unsigned char)fill_char, len); |
| 11106 | else { |
| 11107 | for (n = 0; n < len; ++n) |
| 11108 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11109 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11110 | } |
| 11111 | else { |
| 11112 | /* number of characters copied this far */ |
| 11113 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
| 11114 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); |
| 11115 | char *to = (char *) PyUnicode_DATA(u); |
| 11116 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11117 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11118 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11119 | n = (done <= nchars-done) ? done : nchars-done; |
| 11120 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11121 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11122 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11123 | } |
| 11124 | |
| 11125 | return (PyObject*) u; |
| 11126 | } |
| 11127 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11128 | PyObject * |
| 11129 | PyUnicode_Replace(PyObject *obj, |
| 11130 | PyObject *subobj, |
| 11131 | PyObject *replobj, |
| 11132 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11133 | { |
| 11134 | PyObject *self; |
| 11135 | PyObject *str1; |
| 11136 | PyObject *str2; |
| 11137 | PyObject *result; |
| 11138 | |
| 11139 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11140 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11141 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11142 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11143 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11144 | Py_DECREF(self); |
| 11145 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11146 | } |
| 11147 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11148 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11149 | Py_DECREF(self); |
| 11150 | Py_DECREF(str1); |
| 11151 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11152 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11153 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11154 | Py_DECREF(self); |
| 11155 | Py_DECREF(str1); |
| 11156 | Py_DECREF(str2); |
| 11157 | return result; |
| 11158 | } |
| 11159 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11160 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11161 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11162 | \n\ |
| 11163 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11164 | old replaced by new. If the optional argument count is\n\ |
| 11165 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11166 | |
| 11167 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11168 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11169 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11170 | PyObject *str1; |
| 11171 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11172 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11173 | PyObject *result; |
| 11174 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11175 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11176 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11177 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11178 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11179 | str1 = PyUnicode_FromObject(str1); |
| 11180 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11181 | return NULL; |
| 11182 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11183 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11184 | Py_DECREF(str1); |
| 11185 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11186 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11187 | |
| 11188 | result = replace(self, str1, str2, maxcount); |
| 11189 | |
| 11190 | Py_DECREF(str1); |
| 11191 | Py_DECREF(str2); |
| 11192 | return result; |
| 11193 | } |
| 11194 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11195 | static PyObject * |
| 11196 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11197 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11198 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11199 | Py_ssize_t isize; |
| 11200 | Py_ssize_t osize, squote, dquote, i, o; |
| 11201 | Py_UCS4 max, quote; |
| 11202 | int ikind, okind; |
| 11203 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11204 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11205 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11206 | return NULL; |
| 11207 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11208 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11209 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11210 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11211 | /* Compute length of output, quote characters, and |
| 11212 | maximum character */ |
| 11213 | osize = 2; /* quotes */ |
| 11214 | max = 127; |
| 11215 | squote = dquote = 0; |
| 11216 | ikind = PyUnicode_KIND(unicode); |
| 11217 | for (i = 0; i < isize; i++) { |
| 11218 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11219 | switch (ch) { |
| 11220 | case '\'': squote++; osize++; break; |
| 11221 | case '"': dquote++; osize++; break; |
| 11222 | case '\\': case '\t': case '\r': case '\n': |
| 11223 | osize += 2; break; |
| 11224 | default: |
| 11225 | /* Fast-path ASCII */ |
| 11226 | if (ch < ' ' || ch == 0x7f) |
| 11227 | osize += 4; /* \xHH */ |
| 11228 | else if (ch < 0x7f) |
| 11229 | osize++; |
| 11230 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11231 | osize++; |
| 11232 | max = ch > max ? ch : max; |
| 11233 | } |
| 11234 | else if (ch < 0x100) |
| 11235 | osize += 4; /* \xHH */ |
| 11236 | else if (ch < 0x10000) |
| 11237 | osize += 6; /* \uHHHH */ |
| 11238 | else |
| 11239 | osize += 10; /* \uHHHHHHHH */ |
| 11240 | } |
| 11241 | } |
| 11242 | |
| 11243 | quote = '\''; |
| 11244 | if (squote) { |
| 11245 | if (dquote) |
| 11246 | /* Both squote and dquote present. Use squote, |
| 11247 | and escape them */ |
| 11248 | osize += squote; |
| 11249 | else |
| 11250 | quote = '"'; |
| 11251 | } |
| 11252 | |
| 11253 | repr = PyUnicode_New(osize, max); |
| 11254 | if (repr == NULL) |
| 11255 | return NULL; |
| 11256 | okind = PyUnicode_KIND(repr); |
| 11257 | odata = PyUnicode_DATA(repr); |
| 11258 | |
| 11259 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11260 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11261 | |
| 11262 | for (i = 0, o = 1; i < isize; i++) { |
| 11263 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11264 | |
| 11265 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11266 | if ((ch == quote) || (ch == '\\')) { |
| 11267 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11268 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11269 | continue; |
| 11270 | } |
| 11271 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11272 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11273 | if (ch == '\t') { |
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++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11276 | } |
| 11277 | else if (ch == '\n') { |
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++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11280 | } |
| 11281 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11282 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11283 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11284 | } |
| 11285 | |
| 11286 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11287 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11288 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11289 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11290 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11291 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11292 | } |
| 11293 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11294 | /* Copy ASCII characters as-is */ |
| 11295 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11296 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11297 | } |
| 11298 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11299 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11300 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11301 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11302 | (categories Z* and C* except ASCII space) |
| 11303 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11304 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11305 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11306 | if (ch <= 0xff) { |
| 11307 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11308 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11309 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11310 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11311 | } |
| 11312 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11313 | else if (ch >= 0x10000) { |
| 11314 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11315 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11316 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 11317 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 11318 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 11319 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 11320 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11321 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11322 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11323 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11324 | } |
| 11325 | /* Map 16-bit characters to '\uxxxx' */ |
| 11326 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11327 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11328 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 11329 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11330 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11331 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11332 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11333 | } |
| 11334 | } |
| 11335 | /* Copy characters as-is */ |
| 11336 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11337 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11338 | } |
| 11339 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11340 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11341 | /* Closing quote already added at the beginning */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11342 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11343 | } |
| 11344 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11345 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11346 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11347 | \n\ |
| 11348 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11349 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11350 | arguments start and end are interpreted as in slice notation.\n\ |
| 11351 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11352 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11353 | |
| 11354 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11355 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11356 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11357 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11358 | Py_ssize_t start; |
| 11359 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11360 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11361 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11362 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11363 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11364 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11365 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11366 | if (PyUnicode_READY(self) == -1) |
| 11367 | return NULL; |
| 11368 | if (PyUnicode_READY(substring) == -1) |
| 11369 | return NULL; |
| 11370 | |
| 11371 | result = any_find_slice( |
| 11372 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 11373 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11374 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11375 | |
| 11376 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11377 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11378 | if (result == -2) |
| 11379 | return NULL; |
| 11380 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11381 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11382 | } |
| 11383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11384 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11385 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11386 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11387 | 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] | 11388 | |
| 11389 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11390 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11391 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11392 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11393 | Py_ssize_t start; |
| 11394 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11395 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11396 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11397 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 11398 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11399 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11400 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11401 | if (PyUnicode_READY(self) == -1) |
| 11402 | return NULL; |
| 11403 | if (PyUnicode_READY(substring) == -1) |
| 11404 | return NULL; |
| 11405 | |
| 11406 | result = any_find_slice( |
| 11407 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 11408 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11409 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11410 | |
| 11411 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11412 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11413 | if (result == -2) |
| 11414 | return NULL; |
| 11415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11416 | if (result < 0) { |
| 11417 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11418 | return NULL; |
| 11419 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11420 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11421 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11422 | } |
| 11423 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11424 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11425 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11426 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11427 | 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] | 11428 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | |
| 11430 | static PyObject * |
| 11431 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 11432 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11433 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11434 | Py_UCS4 fillchar = ' '; |
| 11435 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11436 | 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] | 11437 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11438 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11439 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11440 | return NULL; |
| 11441 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11442 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11443 | Py_INCREF(self); |
| 11444 | return (PyObject*) self; |
| 11445 | } |
| 11446 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11447 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11448 | } |
| 11449 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11450 | PyObject * |
| 11451 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11452 | { |
| 11453 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11454 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11455 | s = PyUnicode_FromObject(s); |
| 11456 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11457 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11458 | if (sep != NULL) { |
| 11459 | sep = PyUnicode_FromObject(sep); |
| 11460 | if (sep == NULL) { |
| 11461 | Py_DECREF(s); |
| 11462 | return NULL; |
| 11463 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11464 | } |
| 11465 | |
| 11466 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11467 | |
| 11468 | Py_DECREF(s); |
| 11469 | Py_XDECREF(sep); |
| 11470 | return result; |
| 11471 | } |
| 11472 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11473 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11474 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11475 | \n\ |
| 11476 | Return a list of the words in S, using sep as the\n\ |
| 11477 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11478 | 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] | 11479 | whitespace string is a separator and empty strings are\n\ |
| 11480 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11481 | |
| 11482 | static PyObject* |
| 11483 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 11484 | { |
| 11485 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11486 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11487 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11488 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11489 | return NULL; |
| 11490 | |
| 11491 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11492 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11493 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11494 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11495 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11496 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11497 | } |
| 11498 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11499 | PyObject * |
| 11500 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11501 | { |
| 11502 | PyObject* str_obj; |
| 11503 | PyObject* sep_obj; |
| 11504 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11505 | int kind1, kind2, kind; |
| 11506 | void *buf1 = NULL, *buf2 = NULL; |
| 11507 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11508 | |
| 11509 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11510 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11511 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11512 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11513 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11514 | Py_DECREF(str_obj); |
| 11515 | return NULL; |
| 11516 | } |
| 11517 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11518 | kind1 = PyUnicode_KIND(str_in); |
| 11519 | kind2 = PyUnicode_KIND(sep_obj); |
| 11520 | kind = kind1 > kind2 ? kind1 : kind2; |
| 11521 | buf1 = PyUnicode_DATA(str_in); |
| 11522 | if (kind1 != kind) |
| 11523 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11524 | if (!buf1) |
| 11525 | goto onError; |
| 11526 | buf2 = PyUnicode_DATA(sep_obj); |
| 11527 | if (kind2 != kind) |
| 11528 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11529 | if (!buf2) |
| 11530 | goto onError; |
| 11531 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11532 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11533 | |
| 11534 | switch(PyUnicode_KIND(str_in)) { |
| 11535 | case PyUnicode_1BYTE_KIND: |
| 11536 | out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11537 | break; |
| 11538 | case PyUnicode_2BYTE_KIND: |
| 11539 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11540 | break; |
| 11541 | case PyUnicode_4BYTE_KIND: |
| 11542 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11543 | break; |
| 11544 | default: |
| 11545 | assert(0); |
| 11546 | out = 0; |
| 11547 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11548 | |
| 11549 | Py_DECREF(sep_obj); |
| 11550 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11551 | if (kind1 != kind) |
| 11552 | PyMem_Free(buf1); |
| 11553 | if (kind2 != kind) |
| 11554 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11555 | |
| 11556 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11557 | onError: |
| 11558 | Py_DECREF(sep_obj); |
| 11559 | Py_DECREF(str_obj); |
| 11560 | if (kind1 != kind && buf1) |
| 11561 | PyMem_Free(buf1); |
| 11562 | if (kind2 != kind && buf2) |
| 11563 | PyMem_Free(buf2); |
| 11564 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11565 | } |
| 11566 | |
| 11567 | |
| 11568 | PyObject * |
| 11569 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11570 | { |
| 11571 | PyObject* str_obj; |
| 11572 | PyObject* sep_obj; |
| 11573 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11574 | int kind1, kind2, kind; |
| 11575 | void *buf1 = NULL, *buf2 = NULL; |
| 11576 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11577 | |
| 11578 | str_obj = PyUnicode_FromObject(str_in); |
| 11579 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11580 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11581 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11582 | if (!sep_obj) { |
| 11583 | Py_DECREF(str_obj); |
| 11584 | return NULL; |
| 11585 | } |
| 11586 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11587 | kind1 = PyUnicode_KIND(str_in); |
| 11588 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11589 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11590 | buf1 = PyUnicode_DATA(str_in); |
| 11591 | if (kind1 != kind) |
| 11592 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11593 | if (!buf1) |
| 11594 | goto onError; |
| 11595 | buf2 = PyUnicode_DATA(sep_obj); |
| 11596 | if (kind2 != kind) |
| 11597 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11598 | if (!buf2) |
| 11599 | goto onError; |
| 11600 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11601 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11602 | |
| 11603 | switch(PyUnicode_KIND(str_in)) { |
| 11604 | case PyUnicode_1BYTE_KIND: |
| 11605 | out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11606 | break; |
| 11607 | case PyUnicode_2BYTE_KIND: |
| 11608 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11609 | break; |
| 11610 | case PyUnicode_4BYTE_KIND: |
| 11611 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11612 | break; |
| 11613 | default: |
| 11614 | assert(0); |
| 11615 | out = 0; |
| 11616 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11617 | |
| 11618 | Py_DECREF(sep_obj); |
| 11619 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11620 | if (kind1 != kind) |
| 11621 | PyMem_Free(buf1); |
| 11622 | if (kind2 != kind) |
| 11623 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11624 | |
| 11625 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11626 | onError: |
| 11627 | Py_DECREF(sep_obj); |
| 11628 | Py_DECREF(str_obj); |
| 11629 | if (kind1 != kind && buf1) |
| 11630 | PyMem_Free(buf1); |
| 11631 | if (kind2 != kind && buf2) |
| 11632 | PyMem_Free(buf2); |
| 11633 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11634 | } |
| 11635 | |
| 11636 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11637 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11638 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11639 | 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] | 11640 | 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] | 11641 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11642 | |
| 11643 | static PyObject* |
| 11644 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 11645 | { |
| 11646 | return PyUnicode_Partition((PyObject *)self, separator); |
| 11647 | } |
| 11648 | |
| 11649 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11650 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11651 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11652 | 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] | 11653 | 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] | 11654 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11655 | |
| 11656 | static PyObject* |
| 11657 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 11658 | { |
| 11659 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 11660 | } |
| 11661 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11662 | PyObject * |
| 11663 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11664 | { |
| 11665 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11666 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11667 | s = PyUnicode_FromObject(s); |
| 11668 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11669 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11670 | if (sep != NULL) { |
| 11671 | sep = PyUnicode_FromObject(sep); |
| 11672 | if (sep == NULL) { |
| 11673 | Py_DECREF(s); |
| 11674 | return NULL; |
| 11675 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11676 | } |
| 11677 | |
| 11678 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11679 | |
| 11680 | Py_DECREF(s); |
| 11681 | Py_XDECREF(sep); |
| 11682 | return result; |
| 11683 | } |
| 11684 | |
| 11685 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11686 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11687 | \n\ |
| 11688 | Return a list of the words in S, using sep as the\n\ |
| 11689 | delimiter string, starting at the end of the string and\n\ |
| 11690 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 11691 | splits are done. If sep is not specified, any whitespace string\n\ |
| 11692 | is a separator."); |
| 11693 | |
| 11694 | static PyObject* |
| 11695 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 11696 | { |
| 11697 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11698 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11699 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11700 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11701 | return NULL; |
| 11702 | |
| 11703 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11704 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11705 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11706 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11707 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11708 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11709 | } |
| 11710 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11711 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11712 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11713 | \n\ |
| 11714 | 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] | 11715 | 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] | 11716 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11717 | |
| 11718 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11719 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11720 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11721 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11722 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11723 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11724 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 11725 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11726 | return NULL; |
| 11727 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11728 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11729 | } |
| 11730 | |
| 11731 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 11732 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11733 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 11734 | if (PyUnicode_CheckExact(self)) { |
| 11735 | Py_INCREF(self); |
| 11736 | return self; |
| 11737 | } else |
| 11738 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11739 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11740 | } |
| 11741 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11742 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11743 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11744 | \n\ |
| 11745 | 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] | 11746 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11747 | |
| 11748 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11749 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11750 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11751 | return fixup(self, fixswapcase); |
| 11752 | } |
| 11753 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11754 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11755 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11756 | \n\ |
| 11757 | Return a translation table usable for str.translate().\n\ |
| 11758 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 11759 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11760 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11761 | If there are two arguments, they must be strings of equal length, and\n\ |
| 11762 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 11763 | character at the same position in y. If there is a third argument, it\n\ |
| 11764 | must be a string, whose characters will be mapped to None in the result."); |
| 11765 | |
| 11766 | static PyObject* |
| 11767 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 11768 | { |
| 11769 | PyObject *x, *y = NULL, *z = NULL; |
| 11770 | PyObject *new = NULL, *key, *value; |
| 11771 | Py_ssize_t i = 0; |
| 11772 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11773 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11774 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 11775 | return NULL; |
| 11776 | new = PyDict_New(); |
| 11777 | if (!new) |
| 11778 | return NULL; |
| 11779 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11780 | int x_kind, y_kind, z_kind; |
| 11781 | void *x_data, *y_data, *z_data; |
| 11782 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11783 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11784 | if (!PyUnicode_Check(x)) { |
| 11785 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 11786 | "be a string if there is a second argument"); |
| 11787 | goto err; |
| 11788 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11789 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11790 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 11791 | "arguments must have equal length"); |
| 11792 | goto err; |
| 11793 | } |
| 11794 | /* 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] | 11795 | x_kind = PyUnicode_KIND(x); |
| 11796 | y_kind = PyUnicode_KIND(y); |
| 11797 | x_data = PyUnicode_DATA(x); |
| 11798 | y_data = PyUnicode_DATA(y); |
| 11799 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 11800 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 11801 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11802 | if (!key || !value) |
| 11803 | goto err; |
| 11804 | res = PyDict_SetItem(new, key, value); |
| 11805 | Py_DECREF(key); |
| 11806 | Py_DECREF(value); |
| 11807 | if (res < 0) |
| 11808 | goto err; |
| 11809 | } |
| 11810 | /* create entries for deleting chars in z */ |
| 11811 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11812 | z_kind = PyUnicode_KIND(z); |
| 11813 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11814 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11815 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11816 | if (!key) |
| 11817 | goto err; |
| 11818 | res = PyDict_SetItem(new, key, Py_None); |
| 11819 | Py_DECREF(key); |
| 11820 | if (res < 0) |
| 11821 | goto err; |
| 11822 | } |
| 11823 | } |
| 11824 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11825 | int kind; |
| 11826 | void *data; |
| 11827 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11828 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 11829 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11830 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 11831 | "to maketrans it must be a dict"); |
| 11832 | goto err; |
| 11833 | } |
| 11834 | /* copy entries into the new dict, converting string keys to int keys */ |
| 11835 | while (PyDict_Next(x, &i, &key, &value)) { |
| 11836 | if (PyUnicode_Check(key)) { |
| 11837 | /* convert string keys to integer keys */ |
| 11838 | PyObject *newkey; |
| 11839 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 11840 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 11841 | "table must be of length 1"); |
| 11842 | goto err; |
| 11843 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11844 | kind = PyUnicode_KIND(key); |
| 11845 | data = PyUnicode_DATA(key); |
| 11846 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11847 | if (!newkey) |
| 11848 | goto err; |
| 11849 | res = PyDict_SetItem(new, newkey, value); |
| 11850 | Py_DECREF(newkey); |
| 11851 | if (res < 0) |
| 11852 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11853 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11854 | /* just keep integer keys */ |
| 11855 | if (PyDict_SetItem(new, key, value) < 0) |
| 11856 | goto err; |
| 11857 | } else { |
| 11858 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 11859 | "be strings or integers"); |
| 11860 | goto err; |
| 11861 | } |
| 11862 | } |
| 11863 | } |
| 11864 | return new; |
| 11865 | err: |
| 11866 | Py_DECREF(new); |
| 11867 | return NULL; |
| 11868 | } |
| 11869 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11870 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11871 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11872 | \n\ |
| 11873 | Return a copy of the string S, where all characters have been mapped\n\ |
| 11874 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11875 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 11876 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 11877 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11878 | |
| 11879 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11880 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11881 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11882 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11883 | } |
| 11884 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11885 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11886 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11887 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11888 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11889 | |
| 11890 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11891 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11892 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11893 | return fixup(self, fixupper); |
| 11894 | } |
| 11895 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11896 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11897 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11898 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 11899 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 11900 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11901 | |
| 11902 | static PyObject * |
| 11903 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 11904 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11905 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11906 | PyUnicodeObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11907 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11908 | int kind; |
| 11909 | void *data; |
| 11910 | Py_UCS4 chr; |
| 11911 | |
| 11912 | if (PyUnicode_READY(self) == -1) |
| 11913 | return NULL; |
| 11914 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11915 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11916 | return NULL; |
| 11917 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11918 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 11919 | if (PyUnicode_CheckExact(self)) { |
| 11920 | Py_INCREF(self); |
| 11921 | return (PyObject*) self; |
| 11922 | } |
| 11923 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 11924 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11925 | } |
| 11926 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11927 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11928 | |
| 11929 | u = pad(self, fill, 0, '0'); |
| 11930 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11931 | if (u == NULL) |
| 11932 | return NULL; |
| 11933 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11934 | kind = PyUnicode_KIND(u); |
| 11935 | data = PyUnicode_DATA(u); |
| 11936 | chr = PyUnicode_READ(kind, data, fill); |
| 11937 | |
| 11938 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11939 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11940 | PyUnicode_WRITE(kind, data, 0, chr); |
| 11941 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11942 | } |
| 11943 | |
| 11944 | return (PyObject*) u; |
| 11945 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11946 | |
| 11947 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11948 | static PyObject * |
| 11949 | unicode__decimal2ascii(PyObject *self) |
| 11950 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11951 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11952 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11953 | #endif |
| 11954 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11955 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11956 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11957 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11958 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 11959 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11960 | With optional end, stop comparing S at that position.\n\ |
| 11961 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11962 | |
| 11963 | static PyObject * |
| 11964 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11965 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11966 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11967 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11968 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11969 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11970 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11971 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11972 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11973 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11974 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11975 | if (PyTuple_Check(subobj)) { |
| 11976 | Py_ssize_t i; |
| 11977 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11978 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11979 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11980 | if (substring == NULL) |
| 11981 | return NULL; |
| 11982 | result = tailmatch(self, substring, start, end, -1); |
| 11983 | Py_DECREF(substring); |
| 11984 | if (result) { |
| 11985 | Py_RETURN_TRUE; |
| 11986 | } |
| 11987 | } |
| 11988 | /* nothing matched */ |
| 11989 | Py_RETURN_FALSE; |
| 11990 | } |
| 11991 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11992 | if (substring == NULL) { |
| 11993 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11994 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 11995 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11996 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11997 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11998 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11999 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12000 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12001 | } |
| 12002 | |
| 12003 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12004 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12005 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12006 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 12007 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 12008 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12009 | With optional end, stop comparing S at that position.\n\ |
| 12010 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12011 | |
| 12012 | static PyObject * |
| 12013 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12014 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12015 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12016 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12017 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12018 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 12019 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12020 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12021 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 12022 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12023 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12024 | if (PyTuple_Check(subobj)) { |
| 12025 | Py_ssize_t i; |
| 12026 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 12027 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12028 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12029 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12030 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12031 | result = tailmatch(self, substring, start, end, +1); |
| 12032 | Py_DECREF(substring); |
| 12033 | if (result) { |
| 12034 | Py_RETURN_TRUE; |
| 12035 | } |
| 12036 | } |
| 12037 | Py_RETURN_FALSE; |
| 12038 | } |
| 12039 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12040 | if (substring == NULL) { |
| 12041 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 12042 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 12043 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12044 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 12045 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12046 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12047 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12048 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12049 | } |
| 12050 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12051 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12052 | |
| 12053 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12054 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12055 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12056 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 12057 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12058 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12059 | PyDoc_STRVAR(format_map__doc__, |
| 12060 | "S.format_map(mapping) -> str\n\ |
| 12061 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12062 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12063 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12064 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12065 | static PyObject * |
| 12066 | unicode__format__(PyObject* self, PyObject* args) |
| 12067 | { |
| 12068 | PyObject *format_spec; |
| 12069 | |
| 12070 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12071 | return NULL; |
| 12072 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12073 | return _PyUnicode_FormatAdvanced(self, format_spec, 0, |
| 12074 | PyUnicode_GET_LENGTH(format_spec)); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12075 | } |
| 12076 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12077 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12078 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12079 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12080 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12081 | |
| 12082 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12083 | unicode__sizeof__(PyUnicodeObject *v) |
| 12084 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12085 | Py_ssize_t size; |
| 12086 | |
| 12087 | /* If it's a compact object, account for base structure + |
| 12088 | character data. */ |
| 12089 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12090 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12091 | else if (PyUnicode_IS_COMPACT(v)) |
| 12092 | size = sizeof(PyCompactUnicodeObject) + |
| 12093 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); |
| 12094 | else { |
| 12095 | /* If it is a two-block object, account for base object, and |
| 12096 | for character block if present. */ |
| 12097 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12098 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12099 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
| 12100 | PyUnicode_CHARACTER_SIZE(v); |
| 12101 | } |
| 12102 | /* 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] | 12103 | with the data pointer. Check if the data is not shared. */ |
Victor Stinner | 0349091 | 2011-10-03 23:45:12 +0200 | [diff] [blame] | 12104 | if (_PyUnicode_HAS_WSTR_MEMORY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12105 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12106 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12107 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12108 | |
| 12109 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12110 | } |
| 12111 | |
| 12112 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12113 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12114 | |
| 12115 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12116 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12117 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12118 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12119 | if (!copy) |
| 12120 | return NULL; |
| 12121 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12122 | } |
| 12123 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12124 | static PyMethodDef unicode_methods[] = { |
| 12125 | |
| 12126 | /* Order is according to common usage: often used methods should |
| 12127 | appear first, since lookup is done sequentially. */ |
| 12128 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12129 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12130 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12131 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12132 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12133 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12134 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12135 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12136 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12137 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12138 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12139 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12140 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12141 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12142 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12143 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12144 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12145 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12146 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12147 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12148 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12149 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12150 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12151 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12152 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12153 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12154 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12155 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12156 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12157 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12158 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12159 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12160 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12161 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12162 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12163 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12164 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12165 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12166 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12167 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12168 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12169 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12170 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12171 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12172 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12173 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12174 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12175 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12176 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12177 | #endif |
| 12178 | |
| 12179 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12180 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12181 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12182 | #endif |
| 12183 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12184 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12185 | {NULL, NULL} |
| 12186 | }; |
| 12187 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12188 | static PyObject * |
| 12189 | unicode_mod(PyObject *v, PyObject *w) |
| 12190 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12191 | if (!PyUnicode_Check(v)) |
| 12192 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12193 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12194 | } |
| 12195 | |
| 12196 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12197 | 0, /*nb_add*/ |
| 12198 | 0, /*nb_subtract*/ |
| 12199 | 0, /*nb_multiply*/ |
| 12200 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12201 | }; |
| 12202 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12203 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12204 | (lenfunc) unicode_length, /* sq_length */ |
| 12205 | PyUnicode_Concat, /* sq_concat */ |
| 12206 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12207 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12208 | 0, /* sq_slice */ |
| 12209 | 0, /* sq_ass_item */ |
| 12210 | 0, /* sq_ass_slice */ |
| 12211 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12212 | }; |
| 12213 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12214 | static PyObject* |
| 12215 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 12216 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12217 | if (PyUnicode_READY(self) == -1) |
| 12218 | return NULL; |
| 12219 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12220 | if (PyIndex_Check(item)) { |
| 12221 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12222 | if (i == -1 && PyErr_Occurred()) |
| 12223 | return NULL; |
| 12224 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12225 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12226 | return unicode_getitem((PyObject*)self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12227 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12228 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12229 | const Py_UNICODE* source_buf; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12230 | Py_UNICODE* result_buf; |
| 12231 | PyObject* result; |
| 12232 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12233 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12234 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12235 | return NULL; |
| 12236 | } |
| 12237 | |
| 12238 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12239 | return PyUnicode_New(0, 0); |
| 12240 | } else if (start == 0 && step == 1 && |
| 12241 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12242 | PyUnicode_CheckExact(self)) { |
| 12243 | Py_INCREF(self); |
| 12244 | return (PyObject *)self; |
| 12245 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12246 | return PyUnicode_Substring((PyObject*)self, |
| 12247 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12248 | } else { |
| 12249 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 12250 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 12251 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12252 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12253 | if (result_buf == NULL) |
| 12254 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12255 | |
| 12256 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 12257 | result_buf[i] = source_buf[cur]; |
| 12258 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12259 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12260 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 12261 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12262 | return result; |
| 12263 | } |
| 12264 | } else { |
| 12265 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 12266 | return NULL; |
| 12267 | } |
| 12268 | } |
| 12269 | |
| 12270 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12271 | (lenfunc)unicode_length, /* mp_length */ |
| 12272 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 12273 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12274 | }; |
| 12275 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12277 | /* Helpers for PyUnicode_Format() */ |
| 12278 | |
| 12279 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12280 | 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] | 12281 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12282 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12283 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12284 | (*p_argidx)++; |
| 12285 | if (arglen < 0) |
| 12286 | return args; |
| 12287 | else |
| 12288 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12289 | } |
| 12290 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12291 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12292 | return NULL; |
| 12293 | } |
| 12294 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12295 | /* 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] | 12296 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12297 | static PyObject * |
| 12298 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12299 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12300 | char *p; |
| 12301 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12302 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12303 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12304 | x = PyFloat_AsDouble(v); |
| 12305 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12306 | return NULL; |
| 12307 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12308 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12309 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12310 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12311 | p = PyOS_double_to_string(x, type, prec, |
| 12312 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12313 | if (p == NULL) |
| 12314 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12315 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12316 | PyMem_Free(p); |
| 12317 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12318 | } |
| 12319 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12320 | static PyObject* |
| 12321 | formatlong(PyObject *val, int flags, int prec, int type) |
| 12322 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12323 | char *buf; |
| 12324 | int len; |
| 12325 | PyObject *str; /* temporary string object. */ |
| 12326 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12327 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12328 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 12329 | if (!str) |
| 12330 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12331 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12332 | Py_DECREF(str); |
| 12333 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12334 | } |
| 12335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12336 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12337 | formatchar(Py_UCS4 *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12338 | size_t buflen, |
| 12339 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12340 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12341 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12342 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12343 | if (PyUnicode_GET_LENGTH(v) == 1) { |
| 12344 | buf[0] = PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12345 | buf[1] = '\0'; |
| 12346 | return 1; |
| 12347 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12348 | goto onError; |
| 12349 | } |
| 12350 | else { |
| 12351 | /* Integer input truncated to a character */ |
| 12352 | long x; |
| 12353 | x = PyLong_AsLong(v); |
| 12354 | if (x == -1 && PyErr_Occurred()) |
| 12355 | goto onError; |
| 12356 | |
| 12357 | if (x < 0 || x > 0x10ffff) { |
| 12358 | PyErr_SetString(PyExc_OverflowError, |
| 12359 | "%c arg not in range(0x110000)"); |
| 12360 | return -1; |
| 12361 | } |
| 12362 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12363 | buf[0] = (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12364 | buf[1] = '\0'; |
| 12365 | return 1; |
| 12366 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12367 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12368 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12369 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12370 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12371 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12372 | } |
| 12373 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12374 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12375 | 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] | 12376 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12377 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12378 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12379 | PyObject * |
| 12380 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12381 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12382 | void *fmt; |
| 12383 | int fmtkind; |
| 12384 | PyObject *result; |
| 12385 | Py_UCS4 *res, *res0; |
| 12386 | Py_UCS4 max; |
| 12387 | int kind; |
| 12388 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12389 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12390 | PyObject *dict = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12391 | PyUnicodeObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12392 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12393 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12394 | PyErr_BadInternalCall(); |
| 12395 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12396 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12397 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 12398 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12399 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12400 | fmt = PyUnicode_DATA(uformat); |
| 12401 | fmtkind = PyUnicode_KIND(uformat); |
| 12402 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 12403 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12404 | |
| 12405 | reslen = rescnt = fmtcnt + 100; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12406 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); |
| 12407 | if (res0 == NULL) { |
| 12408 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12409 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12410 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12411 | |
| 12412 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12413 | arglen = PyTuple_Size(args); |
| 12414 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12415 | } |
| 12416 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12417 | arglen = -1; |
| 12418 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12419 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12420 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12421 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12422 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12423 | |
| 12424 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12425 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12426 | if (--rescnt < 0) { |
| 12427 | rescnt = fmtcnt + 100; |
| 12428 | reslen += rescnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12429 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12430 | if (res0 == NULL){ |
| 12431 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12432 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12433 | } |
| 12434 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12435 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12436 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12437 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12438 | } |
| 12439 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12440 | /* Got a format specifier */ |
| 12441 | int flags = 0; |
| 12442 | Py_ssize_t width = -1; |
| 12443 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12444 | Py_UCS4 c = '\0'; |
| 12445 | Py_UCS4 fill; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12446 | int isnumok; |
| 12447 | PyObject *v = NULL; |
| 12448 | PyObject *temp = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12449 | void *pbuf; |
| 12450 | Py_ssize_t pindex; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12451 | Py_UNICODE sign; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12452 | Py_ssize_t len, len1; |
| 12453 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12454 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12455 | fmtpos++; |
| 12456 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12457 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12458 | Py_ssize_t keylen; |
| 12459 | PyObject *key; |
| 12460 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12461 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12462 | if (dict == NULL) { |
| 12463 | PyErr_SetString(PyExc_TypeError, |
| 12464 | "format requires a mapping"); |
| 12465 | goto onError; |
| 12466 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12467 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12468 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12469 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12470 | /* Skip over balanced parentheses */ |
| 12471 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12472 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12473 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12474 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12475 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12476 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12477 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12478 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12479 | if (fmtcnt < 0 || pcount > 0) { |
| 12480 | PyErr_SetString(PyExc_ValueError, |
| 12481 | "incomplete format key"); |
| 12482 | goto onError; |
| 12483 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12484 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12485 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12486 | if (key == NULL) |
| 12487 | goto onError; |
| 12488 | if (args_owned) { |
| 12489 | Py_DECREF(args); |
| 12490 | args_owned = 0; |
| 12491 | } |
| 12492 | args = PyObject_GetItem(dict, key); |
| 12493 | Py_DECREF(key); |
| 12494 | if (args == NULL) { |
| 12495 | goto onError; |
| 12496 | } |
| 12497 | args_owned = 1; |
| 12498 | arglen = -1; |
| 12499 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12500 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12501 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12502 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12503 | case '-': flags |= F_LJUST; continue; |
| 12504 | case '+': flags |= F_SIGN; continue; |
| 12505 | case ' ': flags |= F_BLANK; continue; |
| 12506 | case '#': flags |= F_ALT; continue; |
| 12507 | case '0': flags |= F_ZERO; continue; |
| 12508 | } |
| 12509 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12510 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12511 | if (c == '*') { |
| 12512 | v = getnextarg(args, arglen, &argidx); |
| 12513 | if (v == NULL) |
| 12514 | goto onError; |
| 12515 | if (!PyLong_Check(v)) { |
| 12516 | PyErr_SetString(PyExc_TypeError, |
| 12517 | "* wants int"); |
| 12518 | goto onError; |
| 12519 | } |
| 12520 | width = PyLong_AsLong(v); |
| 12521 | if (width == -1 && PyErr_Occurred()) |
| 12522 | goto onError; |
| 12523 | if (width < 0) { |
| 12524 | flags |= F_LJUST; |
| 12525 | width = -width; |
| 12526 | } |
| 12527 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12528 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12529 | } |
| 12530 | else if (c >= '0' && c <= '9') { |
| 12531 | width = c - '0'; |
| 12532 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12533 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12534 | if (c < '0' || c > '9') |
| 12535 | break; |
| 12536 | if ((width*10) / 10 != width) { |
| 12537 | PyErr_SetString(PyExc_ValueError, |
| 12538 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12539 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12540 | } |
| 12541 | width = width*10 + (c - '0'); |
| 12542 | } |
| 12543 | } |
| 12544 | if (c == '.') { |
| 12545 | prec = 0; |
| 12546 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12547 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12548 | if (c == '*') { |
| 12549 | v = getnextarg(args, arglen, &argidx); |
| 12550 | if (v == NULL) |
| 12551 | goto onError; |
| 12552 | if (!PyLong_Check(v)) { |
| 12553 | PyErr_SetString(PyExc_TypeError, |
| 12554 | "* wants int"); |
| 12555 | goto onError; |
| 12556 | } |
| 12557 | prec = PyLong_AsLong(v); |
| 12558 | if (prec == -1 && PyErr_Occurred()) |
| 12559 | goto onError; |
| 12560 | if (prec < 0) |
| 12561 | prec = 0; |
| 12562 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12563 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12564 | } |
| 12565 | else if (c >= '0' && c <= '9') { |
| 12566 | prec = c - '0'; |
| 12567 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12568 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12569 | if (c < '0' || c > '9') |
| 12570 | break; |
| 12571 | if ((prec*10) / 10 != prec) { |
| 12572 | PyErr_SetString(PyExc_ValueError, |
| 12573 | "prec too big"); |
| 12574 | goto onError; |
| 12575 | } |
| 12576 | prec = prec*10 + (c - '0'); |
| 12577 | } |
| 12578 | } |
| 12579 | } /* prec */ |
| 12580 | if (fmtcnt >= 0) { |
| 12581 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12582 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12583 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12584 | } |
| 12585 | } |
| 12586 | if (fmtcnt < 0) { |
| 12587 | PyErr_SetString(PyExc_ValueError, |
| 12588 | "incomplete format"); |
| 12589 | goto onError; |
| 12590 | } |
| 12591 | if (c != '%') { |
| 12592 | v = getnextarg(args, arglen, &argidx); |
| 12593 | if (v == NULL) |
| 12594 | goto onError; |
| 12595 | } |
| 12596 | sign = 0; |
| 12597 | fill = ' '; |
| 12598 | switch (c) { |
| 12599 | |
| 12600 | case '%': |
| 12601 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12602 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12603 | /* presume that buffer length is at least 1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12604 | PyUnicode_WRITE(kind, pbuf, 0, '%'); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12605 | len = 1; |
| 12606 | break; |
| 12607 | |
| 12608 | case 's': |
| 12609 | case 'r': |
| 12610 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12611 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12612 | temp = v; |
| 12613 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12614 | } |
| 12615 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12616 | if (c == 's') |
| 12617 | temp = PyObject_Str(v); |
| 12618 | else if (c == 'r') |
| 12619 | temp = PyObject_Repr(v); |
| 12620 | else |
| 12621 | temp = PyObject_ASCII(v); |
| 12622 | if (temp == NULL) |
| 12623 | goto onError; |
| 12624 | if (PyUnicode_Check(temp)) |
| 12625 | /* nothing to do */; |
| 12626 | else { |
| 12627 | Py_DECREF(temp); |
| 12628 | PyErr_SetString(PyExc_TypeError, |
| 12629 | "%s argument has non-string str()"); |
| 12630 | goto onError; |
| 12631 | } |
| 12632 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12633 | if (PyUnicode_READY(temp) == -1) { |
| 12634 | Py_CLEAR(temp); |
| 12635 | goto onError; |
| 12636 | } |
| 12637 | pbuf = PyUnicode_DATA(temp); |
| 12638 | kind = PyUnicode_KIND(temp); |
| 12639 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12640 | if (prec >= 0 && len > prec) |
| 12641 | len = prec; |
| 12642 | break; |
| 12643 | |
| 12644 | case 'i': |
| 12645 | case 'd': |
| 12646 | case 'u': |
| 12647 | case 'o': |
| 12648 | case 'x': |
| 12649 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12650 | isnumok = 0; |
| 12651 | if (PyNumber_Check(v)) { |
| 12652 | PyObject *iobj=NULL; |
| 12653 | |
| 12654 | if (PyLong_Check(v)) { |
| 12655 | iobj = v; |
| 12656 | Py_INCREF(iobj); |
| 12657 | } |
| 12658 | else { |
| 12659 | iobj = PyNumber_Long(v); |
| 12660 | } |
| 12661 | if (iobj!=NULL) { |
| 12662 | if (PyLong_Check(iobj)) { |
| 12663 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 12664 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12665 | Py_DECREF(iobj); |
| 12666 | if (!temp) |
| 12667 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12668 | if (PyUnicode_READY(temp) == -1) { |
| 12669 | Py_CLEAR(temp); |
| 12670 | goto onError; |
| 12671 | } |
| 12672 | pbuf = PyUnicode_DATA(temp); |
| 12673 | kind = PyUnicode_KIND(temp); |
| 12674 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12675 | sign = 1; |
| 12676 | } |
| 12677 | else { |
| 12678 | Py_DECREF(iobj); |
| 12679 | } |
| 12680 | } |
| 12681 | } |
| 12682 | if (!isnumok) { |
| 12683 | PyErr_Format(PyExc_TypeError, |
| 12684 | "%%%c format: a number is required, " |
| 12685 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 12686 | goto onError; |
| 12687 | } |
| 12688 | if (flags & F_ZERO) |
| 12689 | fill = '0'; |
| 12690 | break; |
| 12691 | |
| 12692 | case 'e': |
| 12693 | case 'E': |
| 12694 | case 'f': |
| 12695 | case 'F': |
| 12696 | case 'g': |
| 12697 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12698 | temp = formatfloat(v, flags, prec, c); |
| 12699 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12700 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12701 | if (PyUnicode_READY(temp) == -1) { |
| 12702 | Py_CLEAR(temp); |
| 12703 | goto onError; |
| 12704 | } |
| 12705 | pbuf = PyUnicode_DATA(temp); |
| 12706 | kind = PyUnicode_KIND(temp); |
| 12707 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12708 | sign = 1; |
| 12709 | if (flags & F_ZERO) |
| 12710 | fill = '0'; |
| 12711 | break; |
| 12712 | |
| 12713 | case 'c': |
| 12714 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12715 | kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | b9dcffb | 2011-09-29 00:39:24 +0200 | [diff] [blame] | 12716 | len = formatchar(pbuf, Py_ARRAY_LENGTH(formatbuf), v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12717 | if (len < 0) |
| 12718 | goto onError; |
| 12719 | break; |
| 12720 | |
| 12721 | default: |
| 12722 | PyErr_Format(PyExc_ValueError, |
| 12723 | "unsupported format character '%c' (0x%x) " |
| 12724 | "at index %zd", |
| 12725 | (31<=c && c<=126) ? (char)c : '?', |
| 12726 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12727 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12728 | goto onError; |
| 12729 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12730 | /* pbuf is initialized here. */ |
| 12731 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12732 | if (sign) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12733 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || |
| 12734 | PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 12735 | sign = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12736 | len--; |
| 12737 | } |
| 12738 | else if (flags & F_SIGN) |
| 12739 | sign = '+'; |
| 12740 | else if (flags & F_BLANK) |
| 12741 | sign = ' '; |
| 12742 | else |
| 12743 | sign = 0; |
| 12744 | } |
| 12745 | if (width < len) |
| 12746 | width = len; |
| 12747 | if (rescnt - (sign != 0) < width) { |
| 12748 | reslen -= rescnt; |
| 12749 | rescnt = width + fmtcnt + 100; |
| 12750 | reslen += rescnt; |
| 12751 | if (reslen < 0) { |
| 12752 | Py_XDECREF(temp); |
| 12753 | PyErr_NoMemory(); |
| 12754 | goto onError; |
| 12755 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12756 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12757 | if (res0 == 0) { |
| 12758 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12759 | Py_XDECREF(temp); |
| 12760 | goto onError; |
| 12761 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12762 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12763 | } |
| 12764 | if (sign) { |
| 12765 | if (fill != ' ') |
| 12766 | *res++ = sign; |
| 12767 | rescnt--; |
| 12768 | if (width > len) |
| 12769 | width--; |
| 12770 | } |
| 12771 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12772 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12773 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12774 | if (fill != ' ') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12775 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12776 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12777 | } |
| 12778 | rescnt -= 2; |
| 12779 | width -= 2; |
| 12780 | if (width < 0) |
| 12781 | width = 0; |
| 12782 | len -= 2; |
| 12783 | } |
| 12784 | if (width > len && !(flags & F_LJUST)) { |
| 12785 | do { |
| 12786 | --rescnt; |
| 12787 | *res++ = fill; |
| 12788 | } while (--width > len); |
| 12789 | } |
| 12790 | if (fill == ' ') { |
| 12791 | if (sign) |
| 12792 | *res++ = sign; |
| 12793 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12794 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12795 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
| 12796 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12797 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12798 | } |
| 12799 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12800 | /* Copy all characters, preserving len */ |
| 12801 | len1 = len; |
| 12802 | while (len1--) { |
| 12803 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12804 | rescnt--; |
| 12805 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12806 | while (--width >= len) { |
| 12807 | --rescnt; |
| 12808 | *res++ = ' '; |
| 12809 | } |
| 12810 | if (dict && (argidx < arglen) && c != '%') { |
| 12811 | PyErr_SetString(PyExc_TypeError, |
| 12812 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 12813 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12814 | goto onError; |
| 12815 | } |
| 12816 | Py_XDECREF(temp); |
| 12817 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12818 | } /* until end */ |
| 12819 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12820 | PyErr_SetString(PyExc_TypeError, |
| 12821 | "not all arguments converted during string formatting"); |
| 12822 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12823 | } |
| 12824 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12825 | |
| 12826 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) |
| 12827 | if (*res > max) |
| 12828 | max = *res; |
| 12829 | result = PyUnicode_New(reslen - rescnt, max); |
| 12830 | if (!result) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12831 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12832 | kind = PyUnicode_KIND(result); |
| 12833 | for (res = res0; res < res0+reslen-rescnt; res++) |
| 12834 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); |
| 12835 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12836 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12837 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12838 | } |
| 12839 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12840 | return (PyObject *)result; |
| 12841 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12842 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12843 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12844 | Py_DECREF(uformat); |
| 12845 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12846 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12847 | } |
| 12848 | return NULL; |
| 12849 | } |
| 12850 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 12851 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12852 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 12853 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12854 | static PyObject * |
| 12855 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12856 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12857 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12858 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 12859 | char *encoding = NULL; |
| 12860 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12861 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12862 | if (type != &PyUnicode_Type) |
| 12863 | return unicode_subtype_new(type, args, kwds); |
| 12864 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12865 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12866 | return NULL; |
| 12867 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12868 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12869 | if (encoding == NULL && errors == NULL) |
| 12870 | return PyObject_Str(x); |
| 12871 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12872 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12873 | } |
| 12874 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12875 | static PyObject * |
| 12876 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12877 | { |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12878 | PyUnicodeObject *unicode, *self; |
| 12879 | Py_ssize_t length, char_size; |
| 12880 | int share_wstr, share_utf8; |
| 12881 | unsigned int kind; |
| 12882 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12883 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12884 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12885 | |
| 12886 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 12887 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12888 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 12889 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 12890 | if (_PyUnicode_READY_REPLACE(&unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12891 | return NULL; |
| 12892 | |
| 12893 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); |
| 12894 | if (self == NULL) { |
| 12895 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12896 | return NULL; |
| 12897 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12898 | kind = PyUnicode_KIND(unicode); |
| 12899 | length = PyUnicode_GET_LENGTH(unicode); |
| 12900 | |
| 12901 | _PyUnicode_LENGTH(self) = length; |
| 12902 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 12903 | _PyUnicode_STATE(self).interned = 0; |
| 12904 | _PyUnicode_STATE(self).kind = kind; |
| 12905 | _PyUnicode_STATE(self).compact = 0; |
Victor Stinner | 3cf4637 | 2011-10-03 14:42:15 +0200 | [diff] [blame] | 12906 | _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12907 | _PyUnicode_STATE(self).ready = 1; |
| 12908 | _PyUnicode_WSTR(self) = NULL; |
| 12909 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 12910 | _PyUnicode_UTF8(self) = NULL; |
| 12911 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12912 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12913 | |
| 12914 | share_utf8 = 0; |
| 12915 | share_wstr = 0; |
| 12916 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12917 | char_size = 1; |
| 12918 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 12919 | share_utf8 = 1; |
| 12920 | } |
| 12921 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12922 | char_size = 2; |
| 12923 | if (sizeof(wchar_t) == 2) |
| 12924 | share_wstr = 1; |
| 12925 | } |
| 12926 | else { |
| 12927 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12928 | char_size = 4; |
| 12929 | if (sizeof(wchar_t) == 4) |
| 12930 | share_wstr = 1; |
| 12931 | } |
| 12932 | |
| 12933 | /* Ensure we won't overflow the length. */ |
| 12934 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 12935 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12936 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12937 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12938 | data = PyObject_MALLOC((length + 1) * char_size); |
| 12939 | if (data == NULL) { |
| 12940 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12941 | goto onError; |
| 12942 | } |
| 12943 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12944 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12945 | if (share_utf8) { |
| 12946 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 12947 | _PyUnicode_UTF8(self) = data; |
| 12948 | } |
| 12949 | if (share_wstr) { |
| 12950 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 12951 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 12952 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12953 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12954 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
| 12955 | PyUnicode_KIND_SIZE(kind, length + 1)); |
| 12956 | Py_DECREF(unicode); |
| 12957 | return (PyObject *)self; |
| 12958 | |
| 12959 | onError: |
| 12960 | Py_DECREF(unicode); |
| 12961 | Py_DECREF(self); |
| 12962 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12963 | } |
| 12964 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12965 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12966 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12967 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 12968 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 12969 | encoding defaults to the current default string encoding.\n\ |
| 12970 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12971 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12972 | static PyObject *unicode_iter(PyObject *seq); |
| 12973 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12974 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 12975 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12976 | "str", /* tp_name */ |
| 12977 | sizeof(PyUnicodeObject), /* tp_size */ |
| 12978 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12979 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12980 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 12981 | 0, /* tp_print */ |
| 12982 | 0, /* tp_getattr */ |
| 12983 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12984 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12985 | unicode_repr, /* tp_repr */ |
| 12986 | &unicode_as_number, /* tp_as_number */ |
| 12987 | &unicode_as_sequence, /* tp_as_sequence */ |
| 12988 | &unicode_as_mapping, /* tp_as_mapping */ |
| 12989 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 12990 | 0, /* tp_call*/ |
| 12991 | (reprfunc) unicode_str, /* tp_str */ |
| 12992 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 12993 | 0, /* tp_setattro */ |
| 12994 | 0, /* tp_as_buffer */ |
| 12995 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12996 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12997 | unicode_doc, /* tp_doc */ |
| 12998 | 0, /* tp_traverse */ |
| 12999 | 0, /* tp_clear */ |
| 13000 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 13001 | 0, /* tp_weaklistoffset */ |
| 13002 | unicode_iter, /* tp_iter */ |
| 13003 | 0, /* tp_iternext */ |
| 13004 | unicode_methods, /* tp_methods */ |
| 13005 | 0, /* tp_members */ |
| 13006 | 0, /* tp_getset */ |
| 13007 | &PyBaseObject_Type, /* tp_base */ |
| 13008 | 0, /* tp_dict */ |
| 13009 | 0, /* tp_descr_get */ |
| 13010 | 0, /* tp_descr_set */ |
| 13011 | 0, /* tp_dictoffset */ |
| 13012 | 0, /* tp_init */ |
| 13013 | 0, /* tp_alloc */ |
| 13014 | unicode_new, /* tp_new */ |
| 13015 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13016 | }; |
| 13017 | |
| 13018 | /* Initialize the Unicode implementation */ |
| 13019 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13020 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13021 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13022 | int i; |
| 13023 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13024 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13025 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13026 | 0x000A, /* LINE FEED */ |
| 13027 | 0x000D, /* CARRIAGE RETURN */ |
| 13028 | 0x001C, /* FILE SEPARATOR */ |
| 13029 | 0x001D, /* GROUP SEPARATOR */ |
| 13030 | 0x001E, /* RECORD SEPARATOR */ |
| 13031 | 0x0085, /* NEXT LINE */ |
| 13032 | 0x2028, /* LINE SEPARATOR */ |
| 13033 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 13034 | }; |
| 13035 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 13036 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 13037 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13038 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13039 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13040 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13041 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13042 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 13043 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13044 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13045 | |
| 13046 | /* initialize the linebreak bloom filter */ |
| 13047 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13048 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 13049 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13050 | |
| 13051 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13052 | } |
| 13053 | |
| 13054 | /* Finalize the Unicode implementation */ |
| 13055 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13056 | int |
| 13057 | PyUnicode_ClearFreeList(void) |
| 13058 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13059 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13060 | } |
| 13061 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13062 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13063 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13064 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13065 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13066 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13067 | Py_XDECREF(unicode_empty); |
| 13068 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13069 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13070 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13071 | if (unicode_latin1[i]) { |
| 13072 | Py_DECREF(unicode_latin1[i]); |
| 13073 | unicode_latin1[i] = NULL; |
| 13074 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13075 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13076 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13077 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13078 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13079 | void |
| 13080 | PyUnicode_InternInPlace(PyObject **p) |
| 13081 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13082 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 13083 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13084 | #ifdef Py_DEBUG |
| 13085 | assert(s != NULL); |
| 13086 | assert(_PyUnicode_CHECK(s)); |
| 13087 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13088 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13089 | return; |
| 13090 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13091 | /* If it's a subclass, we don't really know what putting |
| 13092 | it in the interned dict might do. */ |
| 13093 | if (!PyUnicode_CheckExact(s)) |
| 13094 | return; |
| 13095 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13096 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13097 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13098 | assert(0 && "PyUnicode_READY fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13099 | return; |
| 13100 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13101 | s = (PyUnicodeObject *)(*p); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13102 | if (interned == NULL) { |
| 13103 | interned = PyDict_New(); |
| 13104 | if (interned == NULL) { |
| 13105 | PyErr_Clear(); /* Don't leave an exception */ |
| 13106 | return; |
| 13107 | } |
| 13108 | } |
| 13109 | /* It might be that the GetItem call fails even |
| 13110 | though the key is present in the dictionary, |
| 13111 | namely when this happens during a stack overflow. */ |
| 13112 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13113 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13114 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13115 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13116 | if (t) { |
| 13117 | Py_INCREF(t); |
| 13118 | Py_DECREF(*p); |
| 13119 | *p = t; |
| 13120 | return; |
| 13121 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13122 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13123 | PyThreadState_GET()->recursion_critical = 1; |
| 13124 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 13125 | PyErr_Clear(); |
| 13126 | PyThreadState_GET()->recursion_critical = 0; |
| 13127 | return; |
| 13128 | } |
| 13129 | PyThreadState_GET()->recursion_critical = 0; |
| 13130 | /* The two references in interned are not counted by refcnt. |
| 13131 | The deallocator will take care of this */ |
| 13132 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13133 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13134 | } |
| 13135 | |
| 13136 | void |
| 13137 | PyUnicode_InternImmortal(PyObject **p) |
| 13138 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13139 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 13140 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13141 | PyUnicode_InternInPlace(p); |
| 13142 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13143 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13144 | Py_INCREF(*p); |
| 13145 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13146 | } |
| 13147 | |
| 13148 | PyObject * |
| 13149 | PyUnicode_InternFromString(const char *cp) |
| 13150 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13151 | PyObject *s = PyUnicode_FromString(cp); |
| 13152 | if (s == NULL) |
| 13153 | return NULL; |
| 13154 | PyUnicode_InternInPlace(&s); |
| 13155 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13156 | } |
| 13157 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13158 | void |
| 13159 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13160 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13161 | PyObject *keys; |
| 13162 | PyUnicodeObject *s; |
| 13163 | Py_ssize_t i, n; |
| 13164 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13165 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13166 | if (interned == NULL || !PyDict_Check(interned)) |
| 13167 | return; |
| 13168 | keys = PyDict_Keys(interned); |
| 13169 | if (keys == NULL || !PyList_Check(keys)) { |
| 13170 | PyErr_Clear(); |
| 13171 | return; |
| 13172 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13173 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13174 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 13175 | detector, interned unicode strings are not forcibly deallocated; |
| 13176 | rather, we give them their stolen references back, and then clear |
| 13177 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13178 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13179 | n = PyList_GET_SIZE(keys); |
| 13180 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13181 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13182 | for (i = 0; i < n; i++) { |
| 13183 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13184 | if (PyUnicode_READY(s) == -1) |
| 13185 | fprintf(stderr, "could not ready string\n"); |
| 13186 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13187 | case SSTATE_NOT_INTERNED: |
| 13188 | /* XXX Shouldn't happen */ |
| 13189 | break; |
| 13190 | case SSTATE_INTERNED_IMMORTAL: |
| 13191 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13192 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13193 | break; |
| 13194 | case SSTATE_INTERNED_MORTAL: |
| 13195 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13196 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13197 | break; |
| 13198 | default: |
| 13199 | Py_FatalError("Inconsistent interned string state."); |
| 13200 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13201 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13202 | } |
| 13203 | fprintf(stderr, "total size of all interned strings: " |
| 13204 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 13205 | "mortal/immortal\n", mortal_size, immortal_size); |
| 13206 | Py_DECREF(keys); |
| 13207 | PyDict_Clear(interned); |
| 13208 | Py_DECREF(interned); |
| 13209 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13210 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13211 | |
| 13212 | |
| 13213 | /********************* Unicode Iterator **************************/ |
| 13214 | |
| 13215 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13216 | PyObject_HEAD |
| 13217 | Py_ssize_t it_index; |
| 13218 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13219 | } unicodeiterobject; |
| 13220 | |
| 13221 | static void |
| 13222 | unicodeiter_dealloc(unicodeiterobject *it) |
| 13223 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13224 | _PyObject_GC_UNTRACK(it); |
| 13225 | Py_XDECREF(it->it_seq); |
| 13226 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13227 | } |
| 13228 | |
| 13229 | static int |
| 13230 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 13231 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13232 | Py_VISIT(it->it_seq); |
| 13233 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13234 | } |
| 13235 | |
| 13236 | static PyObject * |
| 13237 | unicodeiter_next(unicodeiterobject *it) |
| 13238 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13239 | PyUnicodeObject *seq; |
| 13240 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13241 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13242 | assert(it != NULL); |
| 13243 | seq = it->it_seq; |
| 13244 | if (seq == NULL) |
| 13245 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13246 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13247 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13248 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 13249 | int kind = PyUnicode_KIND(seq); |
| 13250 | void *data = PyUnicode_DATA(seq); |
| 13251 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 13252 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13253 | if (item != NULL) |
| 13254 | ++it->it_index; |
| 13255 | return item; |
| 13256 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13257 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13258 | Py_DECREF(seq); |
| 13259 | it->it_seq = NULL; |
| 13260 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13261 | } |
| 13262 | |
| 13263 | static PyObject * |
| 13264 | unicodeiter_len(unicodeiterobject *it) |
| 13265 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13266 | Py_ssize_t len = 0; |
| 13267 | if (it->it_seq) |
| 13268 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 13269 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13270 | } |
| 13271 | |
| 13272 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 13273 | |
| 13274 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13275 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13276 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13277 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13278 | }; |
| 13279 | |
| 13280 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13281 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 13282 | "str_iterator", /* tp_name */ |
| 13283 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 13284 | 0, /* tp_itemsize */ |
| 13285 | /* methods */ |
| 13286 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 13287 | 0, /* tp_print */ |
| 13288 | 0, /* tp_getattr */ |
| 13289 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13290 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13291 | 0, /* tp_repr */ |
| 13292 | 0, /* tp_as_number */ |
| 13293 | 0, /* tp_as_sequence */ |
| 13294 | 0, /* tp_as_mapping */ |
| 13295 | 0, /* tp_hash */ |
| 13296 | 0, /* tp_call */ |
| 13297 | 0, /* tp_str */ |
| 13298 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13299 | 0, /* tp_setattro */ |
| 13300 | 0, /* tp_as_buffer */ |
| 13301 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 13302 | 0, /* tp_doc */ |
| 13303 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 13304 | 0, /* tp_clear */ |
| 13305 | 0, /* tp_richcompare */ |
| 13306 | 0, /* tp_weaklistoffset */ |
| 13307 | PyObject_SelfIter, /* tp_iter */ |
| 13308 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 13309 | unicodeiter_methods, /* tp_methods */ |
| 13310 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13311 | }; |
| 13312 | |
| 13313 | static PyObject * |
| 13314 | unicode_iter(PyObject *seq) |
| 13315 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13316 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13317 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13318 | if (!PyUnicode_Check(seq)) { |
| 13319 | PyErr_BadInternalCall(); |
| 13320 | return NULL; |
| 13321 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13322 | if (PyUnicode_READY(seq) == -1) |
| 13323 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13324 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 13325 | if (it == NULL) |
| 13326 | return NULL; |
| 13327 | it->it_index = 0; |
| 13328 | Py_INCREF(seq); |
| 13329 | it->it_seq = (PyUnicodeObject *)seq; |
| 13330 | _PyObject_GC_TRACK(it); |
| 13331 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13332 | } |
| 13333 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13334 | #define UNIOP(x) Py_UNICODE_##x |
| 13335 | #define UNIOP_t Py_UNICODE |
| 13336 | #include "uniops.h" |
| 13337 | #undef UNIOP |
| 13338 | #undef UNIOP_t |
| 13339 | #define UNIOP(x) Py_UCS4_##x |
| 13340 | #define UNIOP_t Py_UCS4 |
| 13341 | #include "uniops.h" |
| 13342 | #undef UNIOP |
| 13343 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 13344 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13345 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 13346 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13347 | { |
| 13348 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 13349 | Py_UNICODE *copy; |
| 13350 | Py_ssize_t size; |
| 13351 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13352 | if (!PyUnicode_Check(unicode)) { |
| 13353 | PyErr_BadArgument(); |
| 13354 | return NULL; |
| 13355 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13356 | /* Ensure we won't overflow the size. */ |
| 13357 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 13358 | PyErr_NoMemory(); |
| 13359 | return NULL; |
| 13360 | } |
| 13361 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 13362 | size *= sizeof(Py_UNICODE); |
| 13363 | copy = PyMem_Malloc(size); |
| 13364 | if (copy == NULL) { |
| 13365 | PyErr_NoMemory(); |
| 13366 | return NULL; |
| 13367 | } |
| 13368 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 13369 | return copy; |
| 13370 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13371 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13372 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 13373 | to the string.Formatter class implemented in Python. */ |
| 13374 | |
| 13375 | static PyMethodDef _string_methods[] = { |
| 13376 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 13377 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 13378 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 13379 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 13380 | {NULL, NULL} |
| 13381 | }; |
| 13382 | |
| 13383 | static struct PyModuleDef _string_module = { |
| 13384 | PyModuleDef_HEAD_INIT, |
| 13385 | "_string", |
| 13386 | PyDoc_STR("string helper module"), |
| 13387 | 0, |
| 13388 | _string_methods, |
| 13389 | NULL, |
| 13390 | NULL, |
| 13391 | NULL, |
| 13392 | NULL |
| 13393 | }; |
| 13394 | |
| 13395 | PyMODINIT_FUNC |
| 13396 | PyInit__string(void) |
| 13397 | { |
| 13398 | return PyModule_Create(&_string_module); |
| 13399 | } |
| 13400 | |
| 13401 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13402 | #ifdef __cplusplus |
| 13403 | } |
| 13404 | #endif |