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 | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 154 | /* Generic helper macro to convert characters of different types. |
| 155 | from_type and to_type have to be valid type names, begin and end |
| 156 | are pointers to the source characters which should be of type |
| 157 | "from_type *". to is a pointer of type "to_type *" and points to the |
| 158 | buffer where the result characters are written to. */ |
| 159 | #define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \ |
| 160 | do { \ |
| 161 | const from_type *iter_; to_type *to_; \ |
| 162 | for (iter_ = (begin), to_ = (to_type *)(to); \ |
| 163 | iter_ < (end); \ |
| 164 | ++iter_, ++to_) { \ |
| 165 | *to_ = (to_type)*iter_; \ |
| 166 | } \ |
| 167 | } while (0) |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 168 | |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 169 | /* The Unicode string has been modified: reset the hash */ |
| 170 | #define _PyUnicode_DIRTY(op) do { _PyUnicode_HASH(op) = -1; } while (0) |
| 171 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 172 | /* This dictionary holds all interned unicode strings. Note that references |
| 173 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 174 | When the interned string reaches a refcnt of 0 the string deallocation |
| 175 | function will delete the reference from this dictionary. |
| 176 | |
| 177 | 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] | 178 | 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] | 179 | */ |
| 180 | static PyObject *interned; |
| 181 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 182 | /* The empty Unicode object is shared to improve performance. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 183 | static PyObject *unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 184 | |
| 185 | /* Single character Unicode strings in the Latin-1 range are being |
| 186 | shared as well. */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 187 | static PyObject *unicode_latin1[256]; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 188 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 189 | /* Fast detection of the most frequent whitespace characters */ |
| 190 | const unsigned char _Py_ascii_whitespace[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 191 | 0, 0, 0, 0, 0, 0, 0, 0, |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 192 | /* case 0x0009: * CHARACTER TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 193 | /* case 0x000A: * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 194 | /* case 0x000B: * LINE TABULATION */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 195 | /* case 0x000C: * FORM FEED */ |
| 196 | /* case 0x000D: * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 197 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 198 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 199 | /* case 0x001C: * FILE SEPARATOR */ |
| 200 | /* case 0x001D: * GROUP SEPARATOR */ |
| 201 | /* case 0x001E: * RECORD SEPARATOR */ |
| 202 | /* case 0x001F: * UNIT SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 203 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 204 | /* case 0x0020: * SPACE */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 205 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 206 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 207 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 208 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 209 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 210 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 211 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 212 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 213 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 214 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 215 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 216 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 217 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 220 | /* forward */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 221 | static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 222 | static PyObject* get_latin1_char(unsigned char ch); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 223 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 224 | static PyObject * |
| 225 | unicode_encode_call_errorhandler(const char *errors, |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 226 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 227 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 228 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 229 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 230 | static void |
| 231 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 232 | const char *encoding, |
| 233 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 234 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 235 | const char *reason); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 236 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 237 | /* Same for linebreaks */ |
| 238 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 239 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 240 | /* 0x000A, * LINE FEED */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 241 | /* 0x000B, * LINE TABULATION */ |
| 242 | /* 0x000C, * FORM FEED */ |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 243 | /* 0x000D, * CARRIAGE RETURN */ |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 244 | 0, 0, 1, 1, 1, 1, 0, 0, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 245 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 246 | /* 0x001C, * FILE SEPARATOR */ |
| 247 | /* 0x001D, * GROUP SEPARATOR */ |
| 248 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 249 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 250 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 251 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 252 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 253 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 254 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 255 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 256 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 257 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 258 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 259 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 260 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 261 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 262 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 265 | /* The max unicode value is always 0x10FFFF while using the PEP-393 API. |
| 266 | 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] | 267 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 268 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 269 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 270 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 271 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 272 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 273 | /* This is actually an illegal character, so it should |
| 274 | not be passed to unichr. */ |
| 275 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 276 | #endif |
| 277 | } |
| 278 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 279 | #ifdef Py_DEBUG |
| 280 | static int |
| 281 | _PyUnicode_CheckConsistency(void *op) |
| 282 | { |
| 283 | PyASCIIObject *ascii; |
| 284 | unsigned int kind; |
| 285 | |
| 286 | assert(PyUnicode_Check(op)); |
| 287 | |
| 288 | ascii = (PyASCIIObject *)op; |
| 289 | kind = ascii->state.kind; |
| 290 | |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 291 | if (ascii->state.ascii == 1 && ascii->state.compact == 1) { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 292 | assert(kind == PyUnicode_1BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 293 | assert(ascii->state.ready == 1); |
| 294 | } |
| 295 | else if (ascii->state.compact == 1) { |
| 296 | assert(kind == PyUnicode_1BYTE_KIND |
| 297 | || kind == PyUnicode_2BYTE_KIND |
| 298 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 299 | assert(ascii->state.ascii == 0); |
| 300 | assert(ascii->state.ready == 1); |
| 301 | } else { |
| 302 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 303 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 304 | |
| 305 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 306 | assert(ascii->state.compact == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 307 | assert(ascii->state.ascii == 0); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 308 | assert(ascii->state.ready == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 309 | assert(ascii->wstr != NULL); |
| 310 | assert(unicode->data.any == NULL); |
| 311 | assert(compact->utf8 == NULL); |
| 312 | assert(ascii->state.interned == SSTATE_NOT_INTERNED); |
| 313 | } |
| 314 | else { |
| 315 | assert(kind == PyUnicode_1BYTE_KIND |
| 316 | || kind == PyUnicode_2BYTE_KIND |
| 317 | || kind == PyUnicode_4BYTE_KIND); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 318 | assert(ascii->state.compact == 0); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 319 | assert(ascii->state.ready == 1); |
| 320 | assert(unicode->data.any != NULL); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | return 1; |
| 324 | } |
| 325 | #endif |
| 326 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 327 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 328 | |
| 329 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 330 | to keep things simple, we use a single bitmask, using the least 5 |
| 331 | bits from each unicode characters as the bit index. */ |
| 332 | |
| 333 | /* the linebreak mask is set up by Unicode_Init below */ |
| 334 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 335 | #if LONG_BIT >= 128 |
| 336 | #define BLOOM_WIDTH 128 |
| 337 | #elif LONG_BIT >= 64 |
| 338 | #define BLOOM_WIDTH 64 |
| 339 | #elif LONG_BIT >= 32 |
| 340 | #define BLOOM_WIDTH 32 |
| 341 | #else |
| 342 | #error "LONG_BIT is smaller than 32" |
| 343 | #endif |
| 344 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 345 | #define BLOOM_MASK unsigned long |
| 346 | |
| 347 | static BLOOM_MASK bloom_linebreak; |
| 348 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 349 | #define BLOOM_ADD(mask, ch) ((mask |= (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
| 350 | #define BLOOM(mask, ch) ((mask & (1UL << ((ch) & (BLOOM_WIDTH - 1))))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 351 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 352 | #define BLOOM_LINEBREAK(ch) \ |
| 353 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 354 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 355 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 356 | Py_LOCAL_INLINE(BLOOM_MASK) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 357 | make_bloom_mask(int kind, void* ptr, Py_ssize_t len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 358 | { |
| 359 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 360 | |
Antoine Pitrou | f068f94 | 2010-01-13 14:19:12 +0000 | [diff] [blame] | 361 | BLOOM_MASK mask; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 362 | Py_ssize_t i; |
| 363 | |
| 364 | mask = 0; |
| 365 | for (i = 0; i < len; i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 366 | BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 367 | |
| 368 | return mask; |
| 369 | } |
| 370 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 371 | #define BLOOM_MEMBER(mask, chr, str) \ |
| 372 | (BLOOM(mask, chr) \ |
| 373 | && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 374 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 375 | /* --- Unicode Object ----------------------------------------------------- */ |
| 376 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 377 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 378 | fixup(PyUnicodeObject *self, Py_UCS4 (*fixfct)(PyUnicodeObject *s)); |
| 379 | |
| 380 | Py_LOCAL_INLINE(char *) findchar(void *s, int kind, |
| 381 | Py_ssize_t size, Py_UCS4 ch, |
| 382 | int direction) |
| 383 | { |
| 384 | /* like wcschr, but doesn't stop at NULL characters */ |
| 385 | Py_ssize_t i; |
| 386 | if (direction == 1) { |
| 387 | for(i = 0; i < size; i++) |
| 388 | if (PyUnicode_READ(kind, s, i) == ch) |
| 389 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 390 | } |
| 391 | else { |
| 392 | for(i = size-1; i >= 0; i--) |
| 393 | if (PyUnicode_READ(kind, s, i) == ch) |
| 394 | return (char*)s + PyUnicode_KIND_SIZE(kind, i); |
| 395 | } |
| 396 | return NULL; |
| 397 | } |
| 398 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 399 | static PyObject* |
| 400 | resize_compact(PyObject *unicode, Py_ssize_t length) |
| 401 | { |
| 402 | Py_ssize_t char_size; |
| 403 | Py_ssize_t struct_size; |
| 404 | Py_ssize_t new_size; |
| 405 | int share_wstr; |
| 406 | |
| 407 | assert(PyUnicode_IS_READY(unicode)); |
| 408 | char_size = PyUnicode_CHARACTER_SIZE(unicode); |
| 409 | if (PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 410 | struct_size = sizeof(PyASCIIObject); |
| 411 | else |
| 412 | struct_size = sizeof(PyCompactUnicodeObject); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 413 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 414 | |
| 415 | _Py_DEC_REFTOTAL; |
| 416 | _Py_ForgetReference(unicode); |
| 417 | |
| 418 | if (length > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) { |
| 419 | PyErr_NoMemory(); |
| 420 | return NULL; |
| 421 | } |
| 422 | new_size = (struct_size + (length + 1) * char_size); |
| 423 | |
| 424 | unicode = (PyObject *)PyObject_REALLOC((char *)unicode, new_size); |
| 425 | if (unicode == NULL) { |
| 426 | PyObject_Del(unicode); |
| 427 | PyErr_NoMemory(); |
| 428 | return NULL; |
| 429 | } |
| 430 | _Py_NewReference(unicode); |
| 431 | _PyUnicode_LENGTH(unicode) = length; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 432 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 433 | _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 434 | if (!PyUnicode_IS_COMPACT_ASCII(unicode)) |
| 435 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 436 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 437 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 438 | length, 0); |
| 439 | return unicode; |
| 440 | } |
| 441 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 442 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 443 | resize_inplace(register PyUnicodeObject *unicode, Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 444 | { |
| 445 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 446 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 447 | assert(!PyUnicode_IS_COMPACT(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 448 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 449 | assert(Py_REFCNT(unicode) == 1); |
Victor Stinner | b15d4d8 | 2011-09-28 23:59:20 +0200 | [diff] [blame] | 450 | _PyUnicode_DIRTY(unicode); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 451 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 452 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
| 453 | { |
| 454 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
| 455 | _PyUnicode_UTF8(unicode) = NULL; |
| 456 | } |
| 457 | |
| 458 | if (PyUnicode_IS_READY(unicode)) { |
| 459 | Py_ssize_t char_size; |
| 460 | Py_ssize_t new_size; |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 461 | int share_wstr, share_utf8; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 462 | void *data; |
| 463 | |
| 464 | data = _PyUnicode_DATA_ANY(unicode); |
| 465 | assert(data != NULL); |
| 466 | char_size = PyUnicode_CHARACTER_SIZE(unicode); |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 467 | share_wstr = _PyUnicode_SHARE_WSTR(unicode); |
| 468 | share_utf8 = _PyUnicode_SHARE_UTF8(unicode); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 469 | |
| 470 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 471 | PyErr_NoMemory(); |
| 472 | return -1; |
| 473 | } |
| 474 | new_size = (length + 1) * char_size; |
| 475 | |
| 476 | data = (PyObject *)PyObject_REALLOC(data, new_size); |
| 477 | if (data == NULL) { |
| 478 | PyErr_NoMemory(); |
| 479 | return -1; |
| 480 | } |
| 481 | _PyUnicode_DATA_ANY(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 482 | if (share_wstr) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 483 | _PyUnicode_WSTR(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 484 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 485 | } |
| 486 | if (share_utf8) { |
Victor Stinner | 1c8d0c7 | 2011-10-03 12:11:00 +0200 | [diff] [blame] | 487 | _PyUnicode_UTF8(unicode) = data; |
Victor Stinner | c379ead | 2011-10-03 12:52:27 +0200 | [diff] [blame] | 488 | _PyUnicode_UTF8_LENGTH(unicode) = length; |
| 489 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 490 | _PyUnicode_LENGTH(unicode) = length; |
| 491 | PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0); |
| 492 | if (share_wstr) |
| 493 | return 0; |
| 494 | } |
| 495 | if (_PyUnicode_WSTR(unicode) != NULL) { |
| 496 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 497 | |
| 498 | oldstr = _PyUnicode_WSTR(unicode); |
| 499 | _PyUnicode_WSTR(unicode) = PyObject_REALLOC(_PyUnicode_WSTR(unicode), |
| 500 | sizeof(Py_UNICODE) * (length + 1)); |
| 501 | if (!_PyUnicode_WSTR(unicode)) { |
| 502 | _PyUnicode_WSTR(unicode) = (Py_UNICODE *)oldstr; |
| 503 | PyErr_NoMemory(); |
| 504 | return -1; |
| 505 | } |
| 506 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 507 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 508 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 512 | static PyObject* |
| 513 | resize_copy(PyObject *unicode, Py_ssize_t length) |
| 514 | { |
| 515 | Py_ssize_t copy_length; |
| 516 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 517 | PyObject *copy; |
| 518 | assert(PyUnicode_IS_READY(unicode)); |
| 519 | |
| 520 | copy = PyUnicode_New(length, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 521 | if (copy == NULL) |
| 522 | return NULL; |
| 523 | |
| 524 | copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); |
| 525 | if (PyUnicode_CopyCharacters(copy, 0, |
| 526 | unicode, 0, |
| 527 | copy_length) < 0) |
| 528 | { |
| 529 | Py_DECREF(copy); |
| 530 | return NULL; |
| 531 | } |
| 532 | return copy; |
| 533 | } else { |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 534 | PyUnicodeObject *w; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 535 | assert(_PyUnicode_WSTR(unicode) != NULL); |
| 536 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 537 | w = _PyUnicode_New(length); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 538 | if (w == NULL) |
| 539 | return NULL; |
| 540 | copy_length = _PyUnicode_WSTR_LENGTH(unicode); |
| 541 | copy_length = Py_MIN(copy_length, length); |
| 542 | Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(unicode), |
| 543 | copy_length); |
| 544 | return (PyObject*)w; |
| 545 | } |
| 546 | } |
| 547 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 548 | /* 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] | 549 | Ux0000 terminated; some code (e.g. new_identifier) |
| 550 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 551 | |
| 552 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 553 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 554 | |
| 555 | */ |
| 556 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 557 | #ifdef Py_DEBUG |
| 558 | int unicode_old_new_calls = 0; |
| 559 | #endif |
| 560 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 561 | static PyUnicodeObject * |
| 562 | _PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 563 | { |
| 564 | register PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 565 | size_t new_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 566 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 567 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 568 | if (length == 0 && unicode_empty != NULL) { |
| 569 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 570 | return (PyUnicodeObject*)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 573 | /* Ensure we won't overflow the size. */ |
| 574 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 575 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 576 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 577 | if (length < 0) { |
| 578 | PyErr_SetString(PyExc_SystemError, |
| 579 | "Negative size passed to _PyUnicode_New"); |
| 580 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 583 | #ifdef Py_DEBUG |
| 584 | ++unicode_old_new_calls; |
| 585 | #endif |
| 586 | |
| 587 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
| 588 | if (unicode == NULL) |
| 589 | return NULL; |
| 590 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 591 | _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size); |
| 592 | if (!_PyUnicode_WSTR(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 593 | PyErr_NoMemory(); |
| 594 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 595 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 596 | |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 597 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 598 | * the caller fails before initializing str -- unicode_resize() |
| 599 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 600 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 601 | * We don't want unicode_resize to read uninitialized memory in |
| 602 | * that case. |
| 603 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 604 | _PyUnicode_WSTR(unicode)[0] = 0; |
| 605 | _PyUnicode_WSTR(unicode)[length] = 0; |
| 606 | _PyUnicode_WSTR_LENGTH(unicode) = length; |
| 607 | _PyUnicode_HASH(unicode) = -1; |
| 608 | _PyUnicode_STATE(unicode).interned = 0; |
| 609 | _PyUnicode_STATE(unicode).kind = 0; |
| 610 | _PyUnicode_STATE(unicode).compact = 0; |
| 611 | _PyUnicode_STATE(unicode).ready = 0; |
| 612 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 613 | _PyUnicode_DATA_ANY(unicode) = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 614 | _PyUnicode_LENGTH(unicode) = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 615 | _PyUnicode_UTF8(unicode) = NULL; |
| 616 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 617 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 618 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 619 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 620 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 621 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 622 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 623 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 624 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 627 | static const char* |
| 628 | unicode_kind_name(PyObject *unicode) |
| 629 | { |
Victor Stinner | 42dfd71 | 2011-10-03 14:41:45 +0200 | [diff] [blame^] | 630 | /* don't check consistency: unicode_kind_name() is called from |
| 631 | _PyUnicode_Dump() */ |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 632 | if (!PyUnicode_IS_COMPACT(unicode)) |
| 633 | { |
| 634 | if (!PyUnicode_IS_READY(unicode)) |
| 635 | return "wstr"; |
| 636 | switch(PyUnicode_KIND(unicode)) |
| 637 | { |
| 638 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 639 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 640 | return "legacy ascii"; |
| 641 | else |
| 642 | return "legacy latin1"; |
| 643 | case PyUnicode_2BYTE_KIND: |
| 644 | return "legacy UCS2"; |
| 645 | case PyUnicode_4BYTE_KIND: |
| 646 | return "legacy UCS4"; |
| 647 | default: |
| 648 | return "<legacy invalid kind>"; |
| 649 | } |
| 650 | } |
| 651 | assert(PyUnicode_IS_READY(unicode)); |
| 652 | switch(PyUnicode_KIND(unicode)) |
| 653 | { |
| 654 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 655 | if (PyUnicode_IS_ASCII(unicode)) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 656 | return "ascii"; |
| 657 | else |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 658 | return "latin1"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 659 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 660 | return "UCS2"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 661 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 662 | return "UCS4"; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 663 | default: |
| 664 | return "<invalid compact kind>"; |
| 665 | } |
| 666 | } |
| 667 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 668 | #ifdef Py_DEBUG |
| 669 | int unicode_new_new_calls = 0; |
| 670 | |
| 671 | /* Functions wrapping macros for use in debugger */ |
| 672 | char *_PyUnicode_utf8(void *unicode){ |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 673 | return PyUnicode_UTF8(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | void *_PyUnicode_compact_data(void *unicode) { |
| 677 | return _PyUnicode_COMPACT_DATA(unicode); |
| 678 | } |
| 679 | void *_PyUnicode_data(void *unicode){ |
| 680 | printf("obj %p\n", unicode); |
| 681 | printf("compact %d\n", PyUnicode_IS_COMPACT(unicode)); |
| 682 | printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode)); |
| 683 | printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1))); |
| 684 | printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1))); |
| 685 | printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode)); |
| 686 | return PyUnicode_DATA(unicode); |
| 687 | } |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 688 | |
| 689 | void |
| 690 | _PyUnicode_Dump(PyObject *op) |
| 691 | { |
| 692 | PyASCIIObject *ascii = (PyASCIIObject *)op; |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 693 | PyCompactUnicodeObject *compact = (PyCompactUnicodeObject *)op; |
| 694 | PyUnicodeObject *unicode = (PyUnicodeObject *)op; |
| 695 | void *data; |
| 696 | printf("%s: len=%zu, ",unicode_kind_name(op), ascii->length); |
| 697 | if (ascii->state.compact) |
| 698 | data = (compact + 1); |
| 699 | else |
| 700 | data = unicode->data.any; |
| 701 | if (ascii->wstr == data) |
| 702 | printf("shared "); |
| 703 | printf("wstr=%p", ascii->wstr); |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 704 | if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 705 | printf(" (%zu), ", compact->wstr_length); |
| 706 | if (!ascii->state.compact && compact->utf8 == unicode->data.any) |
| 707 | printf("shared "); |
| 708 | printf("utf8=%p (%zu)", compact->utf8, compact->utf8_length); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 709 | } |
Victor Stinner | a849a4b | 2011-10-03 12:12:11 +0200 | [diff] [blame] | 710 | printf(", data=%p\n", data); |
Victor Stinner | fe0c155 | 2011-10-03 02:59:31 +0200 | [diff] [blame] | 711 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 712 | #endif |
| 713 | |
| 714 | PyObject * |
| 715 | PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) |
| 716 | { |
| 717 | PyObject *obj; |
| 718 | PyCompactUnicodeObject *unicode; |
| 719 | void *data; |
| 720 | int kind_state; |
| 721 | int is_sharing = 0, is_ascii = 0; |
| 722 | Py_ssize_t char_size; |
| 723 | Py_ssize_t struct_size; |
| 724 | |
| 725 | /* Optimization for empty strings */ |
| 726 | if (size == 0 && unicode_empty != NULL) { |
| 727 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 728 | return unicode_empty; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | #ifdef Py_DEBUG |
| 732 | ++unicode_new_new_calls; |
| 733 | #endif |
| 734 | |
| 735 | struct_size = sizeof(PyCompactUnicodeObject); |
| 736 | if (maxchar < 128) { |
| 737 | kind_state = PyUnicode_1BYTE_KIND; |
| 738 | char_size = 1; |
| 739 | is_ascii = 1; |
| 740 | struct_size = sizeof(PyASCIIObject); |
| 741 | } |
| 742 | else if (maxchar < 256) { |
| 743 | kind_state = PyUnicode_1BYTE_KIND; |
| 744 | char_size = 1; |
| 745 | } |
| 746 | else if (maxchar < 65536) { |
| 747 | kind_state = PyUnicode_2BYTE_KIND; |
| 748 | char_size = 2; |
| 749 | if (sizeof(wchar_t) == 2) |
| 750 | is_sharing = 1; |
| 751 | } |
| 752 | else { |
| 753 | kind_state = PyUnicode_4BYTE_KIND; |
| 754 | char_size = 4; |
| 755 | if (sizeof(wchar_t) == 4) |
| 756 | is_sharing = 1; |
| 757 | } |
| 758 | |
| 759 | /* Ensure we won't overflow the size. */ |
| 760 | if (size < 0) { |
| 761 | PyErr_SetString(PyExc_SystemError, |
| 762 | "Negative size passed to PyUnicode_New"); |
| 763 | return NULL; |
| 764 | } |
| 765 | if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1)) |
| 766 | return PyErr_NoMemory(); |
| 767 | |
| 768 | /* Duplicated allocation code from _PyObject_New() instead of a call to |
| 769 | * PyObject_New() so we are able to allocate space for the object and |
| 770 | * it's data buffer. |
| 771 | */ |
| 772 | obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size); |
| 773 | if (obj == NULL) |
| 774 | return PyErr_NoMemory(); |
| 775 | obj = PyObject_INIT(obj, &PyUnicode_Type); |
| 776 | if (obj == NULL) |
| 777 | return NULL; |
| 778 | |
| 779 | unicode = (PyCompactUnicodeObject *)obj; |
| 780 | if (is_ascii) |
| 781 | data = ((PyASCIIObject*)obj) + 1; |
| 782 | else |
| 783 | data = unicode + 1; |
| 784 | _PyUnicode_LENGTH(unicode) = size; |
| 785 | _PyUnicode_HASH(unicode) = -1; |
| 786 | _PyUnicode_STATE(unicode).interned = 0; |
| 787 | _PyUnicode_STATE(unicode).kind = kind_state; |
| 788 | _PyUnicode_STATE(unicode).compact = 1; |
| 789 | _PyUnicode_STATE(unicode).ready = 1; |
| 790 | _PyUnicode_STATE(unicode).ascii = is_ascii; |
| 791 | if (is_ascii) { |
| 792 | ((char*)data)[size] = 0; |
| 793 | _PyUnicode_WSTR(unicode) = NULL; |
| 794 | } |
| 795 | else if (kind_state == PyUnicode_1BYTE_KIND) { |
| 796 | ((char*)data)[size] = 0; |
| 797 | _PyUnicode_WSTR(unicode) = NULL; |
| 798 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 799 | unicode->utf8_length = 0; |
| 800 | unicode->utf8 = NULL; |
| 801 | } |
| 802 | else { |
| 803 | unicode->utf8 = NULL; |
| 804 | if (kind_state == PyUnicode_2BYTE_KIND) |
| 805 | ((Py_UCS2*)data)[size] = 0; |
| 806 | else /* kind_state == PyUnicode_4BYTE_KIND */ |
| 807 | ((Py_UCS4*)data)[size] = 0; |
| 808 | if (is_sharing) { |
| 809 | _PyUnicode_WSTR_LENGTH(unicode) = size; |
| 810 | _PyUnicode_WSTR(unicode) = (wchar_t *)data; |
| 811 | } |
| 812 | else { |
| 813 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 814 | _PyUnicode_WSTR(unicode) = NULL; |
| 815 | } |
| 816 | } |
| 817 | return obj; |
| 818 | } |
| 819 | |
| 820 | #if SIZEOF_WCHAR_T == 2 |
| 821 | /* Helper function to convert a 16-bits wchar_t representation to UCS4, this |
| 822 | will decode surrogate pairs, the other conversions are implemented as macros |
| 823 | for efficency. |
| 824 | |
| 825 | This function assumes that unicode can hold one more code point than wstr |
| 826 | characters for a terminating null character. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 827 | static void |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 828 | unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, |
| 829 | PyUnicodeObject *unicode) |
| 830 | { |
| 831 | const wchar_t *iter; |
| 832 | Py_UCS4 *ucs4_out; |
| 833 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 834 | assert(unicode != NULL); |
| 835 | assert(_PyUnicode_CHECK(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 836 | assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND); |
| 837 | ucs4_out = PyUnicode_4BYTE_DATA(unicode); |
| 838 | |
| 839 | for (iter = begin; iter < end; ) { |
| 840 | assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) + |
| 841 | _PyUnicode_GET_LENGTH(unicode))); |
| 842 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 843 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 844 | { |
| 845 | *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000; |
| 846 | iter += 2; |
| 847 | } |
| 848 | else { |
| 849 | *ucs4_out++ = *iter; |
| 850 | iter++; |
| 851 | } |
| 852 | } |
| 853 | assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) + |
| 854 | _PyUnicode_GET_LENGTH(unicode))); |
| 855 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 856 | } |
| 857 | #endif |
| 858 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 859 | static int |
| 860 | _PyUnicode_Dirty(PyObject *unicode) |
| 861 | { |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 862 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 863 | if (Py_REFCNT(unicode) != 1) { |
| 864 | PyErr_SetString(PyExc_ValueError, |
| 865 | "Cannot modify a string having more than 1 reference"); |
| 866 | return -1; |
| 867 | } |
| 868 | _PyUnicode_DIRTY(unicode); |
| 869 | return 0; |
| 870 | } |
| 871 | |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 872 | Py_ssize_t |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 873 | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, |
| 874 | PyObject *from, Py_ssize_t from_start, |
| 875 | Py_ssize_t how_many) |
| 876 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 877 | unsigned int from_kind, to_kind; |
| 878 | void *from_data, *to_data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 879 | |
Victor Stinner | b153615 | 2011-09-30 02:26:10 +0200 | [diff] [blame] | 880 | if (!PyUnicode_Check(from) || !PyUnicode_Check(to)) { |
| 881 | PyErr_BadInternalCall(); |
| 882 | return -1; |
| 883 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 884 | |
| 885 | if (PyUnicode_READY(from)) |
| 886 | return -1; |
| 887 | if (PyUnicode_READY(to)) |
| 888 | return -1; |
| 889 | |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 890 | how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 891 | if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { |
| 892 | PyErr_Format(PyExc_ValueError, |
| 893 | "Cannot write %zi characters at %zi " |
| 894 | "in a string of %zi characters", |
| 895 | how_many, to_start, PyUnicode_GET_LENGTH(to)); |
| 896 | return -1; |
| 897 | } |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 898 | if (how_many == 0) |
| 899 | return 0; |
| 900 | |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 901 | if (_PyUnicode_Dirty(to)) |
Victor Stinner | f5ca1a2 | 2011-09-28 23:54:59 +0200 | [diff] [blame] | 902 | return -1; |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 903 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 904 | from_kind = PyUnicode_KIND(from); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 905 | from_data = PyUnicode_DATA(from); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 906 | to_kind = PyUnicode_KIND(to); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 907 | to_data = PyUnicode_DATA(to); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 908 | |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 909 | if (from_kind == to_kind |
| 910 | /* deny latin1 => ascii */ |
| 911 | && PyUnicode_MAX_CHAR_VALUE(to) >= PyUnicode_MAX_CHAR_VALUE(from)) |
| 912 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 913 | Py_MEMCPY((char*)to_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 914 | + PyUnicode_KIND_SIZE(to_kind, to_start), |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 915 | (char*)from_data |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 916 | + PyUnicode_KIND_SIZE(from_kind, from_start), |
| 917 | PyUnicode_KIND_SIZE(to_kind, how_many)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 918 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 919 | else if (from_kind == PyUnicode_1BYTE_KIND |
| 920 | && to_kind == PyUnicode_2BYTE_KIND) |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 921 | { |
| 922 | _PyUnicode_CONVERT_BYTES( |
| 923 | Py_UCS1, Py_UCS2, |
| 924 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 925 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 926 | PyUnicode_2BYTE_DATA(to) + to_start |
| 927 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 928 | } |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 929 | else if (from_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 930 | && to_kind == PyUnicode_4BYTE_KIND) |
| 931 | { |
| 932 | _PyUnicode_CONVERT_BYTES( |
| 933 | Py_UCS1, Py_UCS4, |
| 934 | PyUnicode_1BYTE_DATA(from) + from_start, |
| 935 | PyUnicode_1BYTE_DATA(from) + from_start + how_many, |
| 936 | PyUnicode_4BYTE_DATA(to) + to_start |
| 937 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 938 | } |
| 939 | else if (from_kind == PyUnicode_2BYTE_KIND |
| 940 | && to_kind == PyUnicode_4BYTE_KIND) |
| 941 | { |
| 942 | _PyUnicode_CONVERT_BYTES( |
| 943 | Py_UCS2, Py_UCS4, |
| 944 | PyUnicode_2BYTE_DATA(from) + from_start, |
| 945 | PyUnicode_2BYTE_DATA(from) + from_start + how_many, |
| 946 | PyUnicode_4BYTE_DATA(to) + to_start |
| 947 | ); |
Victor Stinner | be78eaf | 2011-09-28 21:37:03 +0200 | [diff] [blame] | 948 | } |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 949 | else { |
| 950 | int invalid_kinds; |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 951 | |
| 952 | /* check if max_char(from substring) <= max_char(to) */ |
| 953 | if (from_kind > to_kind |
| 954 | /* latin1 => ascii */ |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 955 | || (PyUnicode_IS_ASCII(to) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 956 | && to_kind == PyUnicode_1BYTE_KIND |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 957 | && !PyUnicode_IS_ASCII(from))) |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 958 | { |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 959 | /* slow path to check for character overflow */ |
| 960 | const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to); |
| 961 | Py_UCS4 ch, maxchar; |
| 962 | Py_ssize_t i; |
| 963 | |
| 964 | maxchar = 0; |
| 965 | invalid_kinds = 0; |
| 966 | for (i=0; i < how_many; i++) { |
| 967 | ch = PyUnicode_READ(from_kind, from_data, from_start + i); |
| 968 | if (ch > maxchar) { |
| 969 | maxchar = ch; |
| 970 | if (maxchar > to_maxchar) { |
| 971 | invalid_kinds = 1; |
| 972 | break; |
| 973 | } |
| 974 | } |
| 975 | PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); |
| 976 | } |
| 977 | } |
| 978 | else |
| 979 | invalid_kinds = 1; |
| 980 | if (invalid_kinds) { |
| 981 | PyErr_Format(PyExc_ValueError, |
Victor Stinner | f42dc44 | 2011-10-02 23:33:16 +0200 | [diff] [blame] | 982 | "Cannot copy %s characters " |
| 983 | "into a string of %s characters", |
| 984 | unicode_kind_name(from), |
| 985 | unicode_kind_name(to)); |
Victor Stinner | a0702ab | 2011-09-29 14:14:38 +0200 | [diff] [blame] | 986 | return -1; |
| 987 | } |
| 988 | } |
| 989 | return how_many; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 990 | } |
| 991 | |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 992 | /* Find the maximum code point and count the number of surrogate pairs so a |
| 993 | correct string length can be computed before converting a string to UCS4. |
| 994 | This function counts single surrogates as a character and not as a pair. |
| 995 | |
| 996 | Return 0 on success, or -1 on error. */ |
| 997 | static int |
| 998 | find_maxchar_surrogates(const wchar_t *begin, const wchar_t *end, |
| 999 | Py_UCS4 *maxchar, Py_ssize_t *num_surrogates) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1000 | { |
| 1001 | const wchar_t *iter; |
| 1002 | |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1003 | assert(num_surrogates != NULL && maxchar != NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1004 | if (num_surrogates == NULL || maxchar == NULL) { |
| 1005 | PyErr_SetString(PyExc_SystemError, |
| 1006 | "unexpected NULL arguments to " |
| 1007 | "PyUnicode_FindMaxCharAndNumSurrogatePairs"); |
| 1008 | return -1; |
| 1009 | } |
| 1010 | |
| 1011 | *num_surrogates = 0; |
| 1012 | *maxchar = 0; |
| 1013 | |
| 1014 | for (iter = begin; iter < end; ) { |
| 1015 | if (*iter > *maxchar) |
| 1016 | *maxchar = *iter; |
| 1017 | #if SIZEOF_WCHAR_T == 2 |
| 1018 | if (*iter >= 0xD800 && *iter <= 0xDBFF |
| 1019 | && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF) |
| 1020 | { |
| 1021 | Py_UCS4 surrogate_val; |
| 1022 | surrogate_val = (((iter[0] & 0x3FF)<<10) |
| 1023 | | (iter[1] & 0x3FF)) + 0x10000; |
| 1024 | ++(*num_surrogates); |
| 1025 | if (surrogate_val > *maxchar) |
| 1026 | *maxchar = surrogate_val; |
| 1027 | iter += 2; |
| 1028 | } |
| 1029 | else |
| 1030 | iter++; |
| 1031 | #else |
| 1032 | iter++; |
| 1033 | #endif |
| 1034 | } |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | #ifdef Py_DEBUG |
| 1039 | int unicode_ready_calls = 0; |
| 1040 | #endif |
| 1041 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1042 | static int |
| 1043 | unicode_ready(PyObject **p_obj, int replace) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1044 | { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1045 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1046 | wchar_t *end; |
| 1047 | Py_UCS4 maxchar = 0; |
| 1048 | Py_ssize_t num_surrogates; |
| 1049 | #if SIZEOF_WCHAR_T == 2 |
| 1050 | Py_ssize_t length_wo_surrogates; |
| 1051 | #endif |
| 1052 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1053 | assert(p_obj != NULL); |
| 1054 | unicode = (PyUnicodeObject *)*p_obj; |
| 1055 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1056 | /* _PyUnicode_Ready() is only intented for old-style API usage where |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1057 | strings were created using _PyObject_New() and where no canonical |
| 1058 | representation (the str field) has been set yet aka strings |
| 1059 | which are not yet ready. */ |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1060 | assert(_PyUnicode_CHECK(unicode)); |
| 1061 | assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1062 | assert(_PyUnicode_WSTR(unicode) != NULL); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1063 | assert(_PyUnicode_DATA_ANY(unicode) == NULL); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1064 | assert(_PyUnicode_UTF8(unicode) == NULL); |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1065 | /* Actually, it should neither be interned nor be anything else: */ |
| 1066 | assert(_PyUnicode_STATE(unicode).interned == SSTATE_NOT_INTERNED); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1067 | |
| 1068 | #ifdef Py_DEBUG |
| 1069 | ++unicode_ready_calls; |
| 1070 | #endif |
| 1071 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1072 | #ifdef Py_DEBUG |
| 1073 | assert(!replace || Py_REFCNT(unicode) == 1); |
| 1074 | #else |
| 1075 | if (replace && Py_REFCNT(unicode) != 1) |
| 1076 | replace = 0; |
| 1077 | #endif |
| 1078 | if (replace) { |
| 1079 | Py_ssize_t len = _PyUnicode_WSTR_LENGTH(unicode); |
| 1080 | wchar_t *wstr = _PyUnicode_WSTR(unicode); |
| 1081 | /* Optimization for empty strings */ |
| 1082 | if (len == 0) { |
| 1083 | Py_INCREF(unicode_empty); |
| 1084 | Py_DECREF(*p_obj); |
| 1085 | *p_obj = unicode_empty; |
| 1086 | return 0; |
| 1087 | } |
| 1088 | if (len == 1 && wstr[0] < 256) { |
| 1089 | PyObject *latin1_char = get_latin1_char((unsigned char)wstr[0]); |
| 1090 | if (latin1_char == NULL) |
| 1091 | return -1; |
| 1092 | Py_DECREF(*p_obj); |
| 1093 | *p_obj = latin1_char; |
| 1094 | return 0; |
| 1095 | } |
| 1096 | } |
| 1097 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1098 | end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | 1722216 | 2011-09-28 22:15:37 +0200 | [diff] [blame] | 1099 | if (find_maxchar_surrogates(_PyUnicode_WSTR(unicode), end, |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1100 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1101 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1102 | |
| 1103 | if (maxchar < 256) { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1104 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1); |
| 1105 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1106 | PyErr_NoMemory(); |
| 1107 | return -1; |
| 1108 | } |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1109 | _PyUnicode_CONVERT_BYTES(wchar_t, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1110 | _PyUnicode_WSTR(unicode), end, |
| 1111 | PyUnicode_1BYTE_DATA(unicode)); |
| 1112 | PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1113 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1114 | _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND; |
| 1115 | if (maxchar < 128) { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1116 | _PyUnicode_STATE(unicode).ascii = 1; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1117 | _PyUnicode_UTF8(unicode) = _PyUnicode_DATA_ANY(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1118 | _PyUnicode_UTF8_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1119 | } |
| 1120 | else { |
Victor Stinner | a3b334d | 2011-10-03 13:53:37 +0200 | [diff] [blame] | 1121 | _PyUnicode_STATE(unicode).ascii = 0; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1122 | _PyUnicode_UTF8(unicode) = NULL; |
| 1123 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1124 | } |
| 1125 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1126 | _PyUnicode_WSTR(unicode) = NULL; |
| 1127 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1128 | } |
| 1129 | /* In this case we might have to convert down from 4-byte native |
| 1130 | wchar_t to 2-byte unicode. */ |
| 1131 | else if (maxchar < 65536) { |
| 1132 | assert(num_surrogates == 0 && |
| 1133 | "FindMaxCharAndNumSurrogatePairs() messed up"); |
| 1134 | |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1135 | #if SIZEOF_WCHAR_T == 2 |
| 1136 | /* We can share representations and are done. */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1137 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1138 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1139 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1140 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1141 | _PyUnicode_UTF8(unicode) = NULL; |
| 1142 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1143 | #else |
| 1144 | /* sizeof(wchar_t) == 4 */ |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1145 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC( |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1146 | 2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1)); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1147 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1148 | PyErr_NoMemory(); |
| 1149 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1150 | } |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1151 | _PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2, |
| 1152 | _PyUnicode_WSTR(unicode), end, |
| 1153 | PyUnicode_2BYTE_DATA(unicode)); |
| 1154 | PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0'; |
| 1155 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
| 1156 | _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1157 | _PyUnicode_UTF8(unicode) = NULL; |
| 1158 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 506f592 | 2011-09-28 22:34:18 +0200 | [diff] [blame] | 1159 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1160 | _PyUnicode_WSTR(unicode) = NULL; |
| 1161 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1162 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1163 | } |
| 1164 | /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */ |
| 1165 | else { |
| 1166 | #if SIZEOF_WCHAR_T == 2 |
| 1167 | /* in case the native representation is 2-bytes, we need to allocate a |
| 1168 | new normalized 4-byte version. */ |
| 1169 | length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1170 | _PyUnicode_DATA_ANY(unicode) = PyObject_MALLOC(4 * (length_wo_surrogates + 1)); |
| 1171 | if (!_PyUnicode_DATA_ANY(unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1172 | PyErr_NoMemory(); |
| 1173 | return -1; |
| 1174 | } |
| 1175 | _PyUnicode_LENGTH(unicode) = length_wo_surrogates; |
| 1176 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1177 | _PyUnicode_UTF8(unicode) = NULL; |
| 1178 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Victor Stinner | 126c559 | 2011-10-03 04:17:10 +0200 | [diff] [blame] | 1179 | /* unicode_convert_wchar_to_ucs4() requires a ready string */ |
| 1180 | _PyUnicode_STATE(unicode).ready = 1; |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1181 | unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1182 | PyObject_FREE(_PyUnicode_WSTR(unicode)); |
| 1183 | _PyUnicode_WSTR(unicode) = NULL; |
| 1184 | _PyUnicode_WSTR_LENGTH(unicode) = 0; |
| 1185 | #else |
| 1186 | assert(num_surrogates == 0); |
| 1187 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1188 | _PyUnicode_DATA_ANY(unicode) = _PyUnicode_WSTR(unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1189 | _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode); |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1190 | _PyUnicode_UTF8(unicode) = NULL; |
| 1191 | _PyUnicode_UTF8_LENGTH(unicode) = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1192 | _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND; |
| 1193 | #endif |
| 1194 | PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0'; |
| 1195 | } |
| 1196 | _PyUnicode_STATE(unicode).ready = 1; |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 1200 | int |
| 1201 | _PyUnicode_ReadyReplace(PyObject **op) |
| 1202 | { |
| 1203 | return unicode_ready(op, 1); |
| 1204 | } |
| 1205 | |
| 1206 | int |
| 1207 | _PyUnicode_Ready(PyObject *op) |
| 1208 | { |
| 1209 | return unicode_ready(&op, 0); |
| 1210 | } |
| 1211 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1212 | static void |
| 1213 | unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1214 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1215 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1216 | case SSTATE_NOT_INTERNED: |
| 1217 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1218 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1219 | case SSTATE_INTERNED_MORTAL: |
| 1220 | /* revive dead object temporarily for DelItem */ |
| 1221 | Py_REFCNT(unicode) = 3; |
| 1222 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 1223 | Py_FatalError( |
| 1224 | "deletion of interned string failed"); |
| 1225 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1226 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1227 | case SSTATE_INTERNED_IMMORTAL: |
| 1228 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1229 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1230 | default: |
| 1231 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1234 | if (_PyUnicode_WSTR(unicode) && |
| 1235 | (!PyUnicode_IS_READY(unicode) || |
| 1236 | _PyUnicode_WSTR(unicode) != PyUnicode_DATA(unicode))) |
| 1237 | PyObject_DEL(_PyUnicode_WSTR(unicode)); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 1238 | if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 1239 | PyObject_DEL(_PyUnicode_UTF8(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1240 | |
| 1241 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1242 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1243 | } |
| 1244 | else { |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 1245 | if (_PyUnicode_DATA_ANY(unicode)) |
| 1246 | PyObject_DEL(_PyUnicode_DATA_ANY(unicode)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1247 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1251 | static int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1252 | unicode_resizable(PyObject *unicode) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1253 | { |
Victor Stinner | a3be613 | 2011-10-03 02:16:37 +0200 | [diff] [blame] | 1254 | Py_ssize_t len; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1255 | if (Py_REFCNT(unicode) != 1) |
| 1256 | return 0; |
| 1257 | if (PyUnicode_CHECK_INTERNED(unicode)) |
| 1258 | return 0; |
| 1259 | if (unicode == unicode_empty) |
| 1260 | return 0; |
Victor Stinner | a3be613 | 2011-10-03 02:16:37 +0200 | [diff] [blame] | 1261 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
| 1262 | len = PyUnicode_WSTR_LENGTH(unicode); |
| 1263 | else |
| 1264 | len = PyUnicode_GET_LENGTH(unicode); |
| 1265 | if (len == 1) { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1266 | Py_UCS4 ch; |
Victor Stinner | a3be613 | 2011-10-03 02:16:37 +0200 | [diff] [blame] | 1267 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1268 | ch = _PyUnicode_WSTR(unicode)[0]; |
Victor Stinner | a3be613 | 2011-10-03 02:16:37 +0200 | [diff] [blame] | 1269 | else |
| 1270 | ch = PyUnicode_READ_CHAR(unicode, 0); |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1271 | if (ch < 256 && unicode_latin1[ch] == unicode) |
| 1272 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1273 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1274 | return 1; |
| 1275 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1276 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1277 | static int |
| 1278 | unicode_resize(PyObject **p_unicode, Py_ssize_t length) |
| 1279 | { |
| 1280 | PyObject *unicode; |
| 1281 | Py_ssize_t old_length; |
| 1282 | |
| 1283 | assert(p_unicode != NULL); |
| 1284 | unicode = *p_unicode; |
| 1285 | |
| 1286 | assert(unicode != NULL); |
| 1287 | assert(PyUnicode_Check(unicode)); |
| 1288 | assert(0 <= length); |
| 1289 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 1290 | if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1291 | old_length = PyUnicode_WSTR_LENGTH(unicode); |
| 1292 | else |
| 1293 | old_length = PyUnicode_GET_LENGTH(unicode); |
| 1294 | if (old_length == length) |
| 1295 | return 0; |
| 1296 | |
| 1297 | /* FIXME: really create a new object? */ |
| 1298 | if (!unicode_resizable(unicode)) { |
| 1299 | PyObject *copy = resize_copy(unicode, length); |
| 1300 | if (copy == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1301 | return -1; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1302 | Py_DECREF(*p_unicode); |
| 1303 | *p_unicode = copy; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1304 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1307 | if (PyUnicode_IS_COMPACT(unicode)) { |
| 1308 | *p_unicode = resize_compact(unicode, length); |
| 1309 | if (*p_unicode == NULL) |
| 1310 | return -1; |
| 1311 | return 0; |
| 1312 | } else |
| 1313 | return resize_inplace((PyUnicodeObject*)unicode, length); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1316 | int |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1317 | PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length) |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1318 | { |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 1319 | PyObject *unicode; |
| 1320 | if (p_unicode == NULL) { |
| 1321 | PyErr_BadInternalCall(); |
| 1322 | return -1; |
| 1323 | } |
| 1324 | unicode = *p_unicode; |
| 1325 | if (unicode == NULL || !PyUnicode_Check(unicode) || length < 0 |
| 1326 | || _PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND) |
| 1327 | { |
| 1328 | PyErr_BadInternalCall(); |
| 1329 | return -1; |
| 1330 | } |
| 1331 | return unicode_resize(p_unicode, length); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1332 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1333 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1334 | static PyObject* |
| 1335 | get_latin1_char(unsigned char ch) |
| 1336 | { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1337 | PyObject *unicode = unicode_latin1[ch]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1338 | if (!unicode) { |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1339 | unicode = PyUnicode_New(1, ch); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1340 | if (!unicode) |
| 1341 | return NULL; |
| 1342 | PyUnicode_1BYTE_DATA(unicode)[0] = ch; |
| 1343 | unicode_latin1[ch] = unicode; |
| 1344 | } |
| 1345 | Py_INCREF(unicode); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1346 | return unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1349 | PyObject * |
| 1350 | PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1351 | { |
| 1352 | PyUnicodeObject *unicode; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1353 | Py_UCS4 maxchar = 0; |
| 1354 | Py_ssize_t num_surrogates; |
| 1355 | |
| 1356 | if (u == NULL) |
| 1357 | return (PyObject*)_PyUnicode_New(size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1358 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1359 | /* If the Unicode data is known at construction time, we can apply |
| 1360 | some optimizations which share commonly used objects. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1361 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1362 | /* Optimization for empty strings */ |
| 1363 | if (size == 0 && unicode_empty != NULL) { |
| 1364 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1365 | return unicode_empty; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 1366 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1367 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1368 | /* Single character Unicode objects in the Latin-1 range are |
| 1369 | shared when using this constructor */ |
| 1370 | if (size == 1 && *u < 256) |
| 1371 | return get_latin1_char((unsigned char)*u); |
| 1372 | |
| 1373 | /* If not empty and not single character, copy the Unicode data |
| 1374 | into the new object */ |
Victor Stinner | d8f6510 | 2011-09-29 19:43:17 +0200 | [diff] [blame] | 1375 | if (find_maxchar_surrogates(u, u + size, |
| 1376 | &maxchar, &num_surrogates) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1377 | return NULL; |
| 1378 | |
| 1379 | unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates, |
| 1380 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1381 | if (!unicode) |
| 1382 | return NULL; |
| 1383 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1384 | switch (PyUnicode_KIND(unicode)) { |
| 1385 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1386 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1387 | u, u + size, PyUnicode_1BYTE_DATA(unicode)); |
| 1388 | break; |
| 1389 | case PyUnicode_2BYTE_KIND: |
| 1390 | #if Py_UNICODE_SIZE == 2 |
| 1391 | Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2); |
| 1392 | #else |
Victor Stinner | fb5f5f2 | 2011-09-28 21:39:49 +0200 | [diff] [blame] | 1393 | _PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1394 | u, u + size, PyUnicode_2BYTE_DATA(unicode)); |
| 1395 | #endif |
| 1396 | break; |
| 1397 | case PyUnicode_4BYTE_KIND: |
| 1398 | #if SIZEOF_WCHAR_T == 2 |
| 1399 | /* This is the only case which has to process surrogates, thus |
| 1400 | a simple copy loop is not enough and we need a function. */ |
Victor Stinner | c53be96 | 2011-10-02 21:33:54 +0200 | [diff] [blame] | 1401 | unicode_convert_wchar_to_ucs4(u, u + size, unicode); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1402 | #else |
| 1403 | assert(num_surrogates == 0); |
| 1404 | Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4); |
| 1405 | #endif |
| 1406 | break; |
| 1407 | default: |
| 1408 | assert(0 && "Impossible state"); |
| 1409 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1410 | |
| 1411 | return (PyObject *)unicode; |
| 1412 | } |
| 1413 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1414 | PyObject * |
| 1415 | PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1416 | { |
| 1417 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1418 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1419 | if (size < 0) { |
| 1420 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1421 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1422 | return NULL; |
| 1423 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1424 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1425 | /* 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] | 1426 | some optimizations which share commonly used objects. |
| 1427 | Also, this means the input must be UTF-8, so fall back to the |
| 1428 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1429 | if (u != NULL) { |
| 1430 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1431 | /* Optimization for empty strings */ |
| 1432 | if (size == 0 && unicode_empty != NULL) { |
| 1433 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 1434 | return unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1435 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1436 | |
| 1437 | /* Single characters are shared when using this constructor. |
| 1438 | Restrict to ASCII, since the input must be UTF-8. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1439 | if (size == 1 && Py_CHARMASK(*u) < 128) |
| 1440 | return get_latin1_char(Py_CHARMASK(*u)); |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 1441 | |
| 1442 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 1445 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1446 | if (!unicode) |
| 1447 | return NULL; |
| 1448 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 1449 | return (PyObject *)unicode; |
| 1450 | } |
| 1451 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1452 | PyObject * |
| 1453 | PyUnicode_FromString(const char *u) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1454 | { |
| 1455 | size_t size = strlen(u); |
| 1456 | if (size > PY_SSIZE_T_MAX) { |
| 1457 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 1458 | return NULL; |
| 1459 | } |
| 1460 | |
| 1461 | return PyUnicode_FromStringAndSize(u, size); |
| 1462 | } |
| 1463 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1464 | static PyObject* |
| 1465 | _PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1466 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1467 | PyObject *res; |
| 1468 | unsigned char max = 127; |
| 1469 | Py_ssize_t i; |
| 1470 | for (i = 0; i < size; i++) { |
| 1471 | if (u[i] & 0x80) { |
| 1472 | max = 255; |
| 1473 | break; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1474 | } |
| 1475 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1476 | res = PyUnicode_New(size, max); |
| 1477 | if (!res) |
| 1478 | return NULL; |
| 1479 | memcpy(PyUnicode_1BYTE_DATA(res), u, size); |
| 1480 | return res; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1483 | static PyObject* |
| 1484 | _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1485 | { |
| 1486 | PyObject *res; |
| 1487 | Py_UCS2 max = 0; |
| 1488 | Py_ssize_t i; |
| 1489 | for (i = 0; i < size; i++) |
| 1490 | if (u[i] > max) |
| 1491 | max = u[i]; |
| 1492 | res = PyUnicode_New(size, max); |
| 1493 | if (!res) |
| 1494 | return NULL; |
| 1495 | if (max >= 256) |
| 1496 | memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size); |
| 1497 | else |
| 1498 | for (i = 0; i < size; i++) |
| 1499 | PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i]; |
| 1500 | return res; |
| 1501 | } |
| 1502 | |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1503 | static PyObject* |
| 1504 | _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1505 | { |
| 1506 | PyObject *res; |
| 1507 | Py_UCS4 max = 0; |
| 1508 | Py_ssize_t i; |
| 1509 | for (i = 0; i < size; i++) |
| 1510 | if (u[i] > max) |
| 1511 | max = u[i]; |
| 1512 | res = PyUnicode_New(size, max); |
| 1513 | if (!res) |
| 1514 | return NULL; |
| 1515 | if (max >= 0x10000) |
| 1516 | memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size); |
| 1517 | else { |
| 1518 | int kind = PyUnicode_KIND(res); |
| 1519 | void *data = PyUnicode_DATA(res); |
| 1520 | for (i = 0; i < size; i++) |
| 1521 | PyUnicode_WRITE(kind, data, i, u[i]); |
| 1522 | } |
| 1523 | return res; |
| 1524 | } |
| 1525 | |
| 1526 | PyObject* |
| 1527 | PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size) |
| 1528 | { |
| 1529 | switch(kind) { |
| 1530 | case PyUnicode_1BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1531 | return _PyUnicode_FromUCS1(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1532 | case PyUnicode_2BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1533 | return _PyUnicode_FromUCS2(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1534 | case PyUnicode_4BYTE_KIND: |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 1535 | return _PyUnicode_FromUCS4(buffer, size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1536 | } |
Victor Stinner | 202b62b | 2011-10-01 23:48:37 +0200 | [diff] [blame] | 1537 | PyErr_SetString(PyExc_ValueError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1538 | return NULL; |
| 1539 | } |
| 1540 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1541 | PyObject* |
| 1542 | PyUnicode_Copy(PyObject *unicode) |
| 1543 | { |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1544 | Py_ssize_t size; |
| 1545 | PyObject *copy; |
| 1546 | void *data; |
| 1547 | |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1548 | if (!PyUnicode_Check(unicode)) { |
| 1549 | PyErr_BadInternalCall(); |
| 1550 | return NULL; |
| 1551 | } |
| 1552 | if (PyUnicode_READY(unicode)) |
| 1553 | return NULL; |
Victor Stinner | c841e7d | 2011-10-01 01:34:32 +0200 | [diff] [blame] | 1554 | |
| 1555 | size = PyUnicode_GET_LENGTH(unicode); |
| 1556 | copy = PyUnicode_New(size, PyUnicode_MAX_CHAR_VALUE(unicode)); |
| 1557 | if (!copy) |
| 1558 | return NULL; |
| 1559 | assert(PyUnicode_KIND(copy) == PyUnicode_KIND(unicode)); |
| 1560 | |
| 1561 | data = PyUnicode_DATA(unicode); |
| 1562 | switch (PyUnicode_KIND(unicode)) |
| 1563 | { |
| 1564 | case PyUnicode_1BYTE_KIND: |
| 1565 | memcpy(PyUnicode_1BYTE_DATA(copy), data, size); |
| 1566 | break; |
| 1567 | case PyUnicode_2BYTE_KIND: |
| 1568 | memcpy(PyUnicode_2BYTE_DATA(copy), data, sizeof(Py_UCS2) * size); |
| 1569 | break; |
| 1570 | case PyUnicode_4BYTE_KIND: |
| 1571 | memcpy(PyUnicode_4BYTE_DATA(copy), data, sizeof(Py_UCS4) * size); |
| 1572 | break; |
| 1573 | default: |
| 1574 | assert(0); |
| 1575 | break; |
| 1576 | } |
| 1577 | return copy; |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 1578 | } |
| 1579 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1580 | |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1581 | /* Widen Unicode objects to larger buffers. Don't write terminating null |
| 1582 | character. Return NULL on error. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1583 | |
| 1584 | void* |
| 1585 | _PyUnicode_AsKind(PyObject *s, unsigned int kind) |
| 1586 | { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1587 | Py_ssize_t len; |
| 1588 | void *result; |
| 1589 | unsigned int skind; |
| 1590 | |
| 1591 | if (PyUnicode_READY(s)) |
| 1592 | return NULL; |
| 1593 | |
| 1594 | len = PyUnicode_GET_LENGTH(s); |
| 1595 | skind = PyUnicode_KIND(s); |
| 1596 | if (skind >= kind) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1597 | PyErr_SetString(PyExc_RuntimeError, "invalid widening attempt"); |
| 1598 | return NULL; |
| 1599 | } |
| 1600 | switch(kind) { |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1601 | case PyUnicode_2BYTE_KIND: |
| 1602 | result = PyMem_Malloc(len * sizeof(Py_UCS2)); |
| 1603 | if (!result) |
| 1604 | return PyErr_NoMemory(); |
| 1605 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1606 | _PyUnicode_CONVERT_BYTES( |
| 1607 | Py_UCS1, Py_UCS2, |
| 1608 | PyUnicode_1BYTE_DATA(s), |
| 1609 | PyUnicode_1BYTE_DATA(s) + len, |
| 1610 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1611 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1612 | case PyUnicode_4BYTE_KIND: |
| 1613 | result = PyMem_Malloc(len * sizeof(Py_UCS4)); |
| 1614 | if (!result) |
| 1615 | return PyErr_NoMemory(); |
| 1616 | if (skind == PyUnicode_2BYTE_KIND) { |
| 1617 | _PyUnicode_CONVERT_BYTES( |
| 1618 | Py_UCS2, Py_UCS4, |
| 1619 | PyUnicode_2BYTE_DATA(s), |
| 1620 | PyUnicode_2BYTE_DATA(s) + len, |
| 1621 | result); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1622 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1623 | else { |
| 1624 | assert(skind == PyUnicode_1BYTE_KIND); |
| 1625 | _PyUnicode_CONVERT_BYTES( |
| 1626 | Py_UCS1, Py_UCS4, |
| 1627 | PyUnicode_1BYTE_DATA(s), |
| 1628 | PyUnicode_1BYTE_DATA(s) + len, |
| 1629 | result); |
| 1630 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1631 | return result; |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1632 | default: |
| 1633 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1634 | } |
Victor Stinner | bc603d1 | 2011-10-02 01:00:40 +0200 | [diff] [blame] | 1635 | PyErr_SetString(PyExc_ValueError, "invalid kind"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1636 | return NULL; |
| 1637 | } |
| 1638 | |
| 1639 | static Py_UCS4* |
| 1640 | as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1641 | int copy_null) |
| 1642 | { |
| 1643 | int kind; |
| 1644 | void *data; |
| 1645 | Py_ssize_t len, targetlen; |
| 1646 | if (PyUnicode_READY(string) == -1) |
| 1647 | return NULL; |
| 1648 | kind = PyUnicode_KIND(string); |
| 1649 | data = PyUnicode_DATA(string); |
| 1650 | len = PyUnicode_GET_LENGTH(string); |
| 1651 | targetlen = len; |
| 1652 | if (copy_null) |
| 1653 | targetlen++; |
| 1654 | if (!target) { |
| 1655 | if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) { |
| 1656 | PyErr_NoMemory(); |
| 1657 | return NULL; |
| 1658 | } |
| 1659 | target = PyMem_Malloc(targetlen * sizeof(Py_UCS4)); |
| 1660 | if (!target) { |
| 1661 | PyErr_NoMemory(); |
| 1662 | return NULL; |
| 1663 | } |
| 1664 | } |
| 1665 | else { |
| 1666 | if (targetsize < targetlen) { |
| 1667 | PyErr_Format(PyExc_SystemError, |
| 1668 | "string is longer than the buffer"); |
| 1669 | if (copy_null && 0 < targetsize) |
| 1670 | target[0] = 0; |
| 1671 | return NULL; |
| 1672 | } |
| 1673 | } |
| 1674 | if (kind != PyUnicode_4BYTE_KIND) { |
| 1675 | Py_ssize_t i; |
| 1676 | for (i = 0; i < len; i++) |
| 1677 | target[i] = PyUnicode_READ(kind, data, i); |
| 1678 | } |
| 1679 | else |
| 1680 | Py_MEMCPY(target, data, len * sizeof(Py_UCS4)); |
| 1681 | if (copy_null) |
| 1682 | target[len] = 0; |
| 1683 | return target; |
| 1684 | } |
| 1685 | |
| 1686 | Py_UCS4* |
| 1687 | PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize, |
| 1688 | int copy_null) |
| 1689 | { |
| 1690 | if (target == NULL || targetsize < 1) { |
| 1691 | PyErr_BadInternalCall(); |
| 1692 | return NULL; |
| 1693 | } |
| 1694 | return as_ucs4(string, target, targetsize, copy_null); |
| 1695 | } |
| 1696 | |
| 1697 | Py_UCS4* |
| 1698 | PyUnicode_AsUCS4Copy(PyObject *string) |
| 1699 | { |
| 1700 | return as_ucs4(string, NULL, 0, 1); |
| 1701 | } |
| 1702 | |
| 1703 | #ifdef HAVE_WCHAR_H |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1704 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 1705 | PyObject * |
| 1706 | PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1707 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1708 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1709 | if (size == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1710 | return PyUnicode_New(0, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1711 | PyErr_BadInternalCall(); |
| 1712 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1715 | if (size == -1) { |
| 1716 | size = wcslen(w); |
| 1717 | } |
| 1718 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1719 | return PyUnicode_FromUnicode(w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1722 | #endif /* HAVE_WCHAR_H */ |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1723 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1724 | static void |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1725 | makefmt(char *fmt, int longflag, int longlongflag, int size_tflag, |
| 1726 | int zeropad, int width, int precision, char c) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1727 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1728 | *fmt++ = '%'; |
| 1729 | if (width) { |
| 1730 | if (zeropad) |
| 1731 | *fmt++ = '0'; |
| 1732 | fmt += sprintf(fmt, "%d", width); |
| 1733 | } |
| 1734 | if (precision) |
| 1735 | fmt += sprintf(fmt, ".%d", precision); |
| 1736 | if (longflag) |
| 1737 | *fmt++ = 'l'; |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1738 | else if (longlongflag) { |
| 1739 | /* longlongflag should only ever be nonzero on machines with |
| 1740 | HAVE_LONG_LONG defined */ |
| 1741 | #ifdef HAVE_LONG_LONG |
| 1742 | char *f = PY_FORMAT_LONG_LONG; |
| 1743 | while (*f) |
| 1744 | *fmt++ = *f++; |
| 1745 | #else |
| 1746 | /* we shouldn't ever get here */ |
| 1747 | assert(0); |
| 1748 | *fmt++ = 'l'; |
| 1749 | #endif |
| 1750 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1751 | else if (size_tflag) { |
| 1752 | char *f = PY_FORMAT_SIZE_T; |
| 1753 | while (*f) |
| 1754 | *fmt++ = *f++; |
| 1755 | } |
| 1756 | *fmt++ = c; |
| 1757 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1760 | /* helper for PyUnicode_FromFormatV() */ |
| 1761 | |
| 1762 | static const char* |
| 1763 | parse_format_flags(const char *f, |
| 1764 | int *p_width, int *p_precision, |
| 1765 | int *p_longflag, int *p_longlongflag, int *p_size_tflag) |
| 1766 | { |
| 1767 | int width, precision, longflag, longlongflag, size_tflag; |
| 1768 | |
| 1769 | /* parse the width.precision part, e.g. "%2.5s" => width=2, precision=5 */ |
| 1770 | f++; |
| 1771 | width = 0; |
| 1772 | while (Py_ISDIGIT((unsigned)*f)) |
| 1773 | width = (width*10) + *f++ - '0'; |
| 1774 | precision = 0; |
| 1775 | if (*f == '.') { |
| 1776 | f++; |
| 1777 | while (Py_ISDIGIT((unsigned)*f)) |
| 1778 | precision = (precision*10) + *f++ - '0'; |
| 1779 | if (*f == '%') { |
| 1780 | /* "%.3%s" => f points to "3" */ |
| 1781 | f--; |
| 1782 | } |
| 1783 | } |
| 1784 | if (*f == '\0') { |
| 1785 | /* bogus format "%.1" => go backward, f points to "1" */ |
| 1786 | f--; |
| 1787 | } |
| 1788 | if (p_width != NULL) |
| 1789 | *p_width = width; |
| 1790 | if (p_precision != NULL) |
| 1791 | *p_precision = precision; |
| 1792 | |
| 1793 | /* Handle %ld, %lu, %lld and %llu. */ |
| 1794 | longflag = 0; |
| 1795 | longlongflag = 0; |
Victor Stinner | e7faec1 | 2011-03-02 00:01:53 +0000 | [diff] [blame] | 1796 | size_tflag = 0; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1797 | |
| 1798 | if (*f == 'l') { |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1799 | if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1800 | longflag = 1; |
| 1801 | ++f; |
| 1802 | } |
| 1803 | #ifdef HAVE_LONG_LONG |
| 1804 | else if (f[1] == 'l' && |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1805 | (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1806 | longlongflag = 1; |
| 1807 | f += 2; |
| 1808 | } |
| 1809 | #endif |
| 1810 | } |
| 1811 | /* handle the size_t flag. */ |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 1812 | 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] | 1813 | size_tflag = 1; |
| 1814 | ++f; |
| 1815 | } |
| 1816 | if (p_longflag != NULL) |
| 1817 | *p_longflag = longflag; |
| 1818 | if (p_longlongflag != NULL) |
| 1819 | *p_longlongflag = longlongflag; |
| 1820 | if (p_size_tflag != NULL) |
| 1821 | *p_size_tflag = size_tflag; |
| 1822 | return f; |
| 1823 | } |
| 1824 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1825 | /* maximum number of characters required for output of %ld. 21 characters |
| 1826 | allows for 64-bit integers (in decimal) and an optional sign. */ |
| 1827 | #define MAX_LONG_CHARS 21 |
| 1828 | /* maximum number of characters required for output of %lld. |
| 1829 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 1830 | plus 1 for the sign. 53/22 is an upper bound for log10(256). */ |
| 1831 | #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22) |
| 1832 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1833 | PyObject * |
| 1834 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 1835 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1836 | va_list count; |
| 1837 | Py_ssize_t callcount = 0; |
| 1838 | PyObject **callresults = NULL; |
| 1839 | PyObject **callresult = NULL; |
| 1840 | Py_ssize_t n = 0; |
| 1841 | int width = 0; |
| 1842 | int precision = 0; |
| 1843 | int zeropad; |
| 1844 | const char* f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1845 | PyUnicodeObject *string; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1846 | /* used by sprintf */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1847 | char fmt[61]; /* should be enough for %0width.precisionlld */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1848 | Py_UCS4 maxchar = 127; /* result is ASCII by default */ |
| 1849 | Py_UCS4 argmaxchar; |
| 1850 | Py_ssize_t numbersize = 0; |
| 1851 | char *numberresults = NULL; |
| 1852 | char *numberresult = NULL; |
| 1853 | Py_ssize_t i; |
| 1854 | int kind; |
| 1855 | void *data; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1856 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1857 | Py_VA_COPY(count, vargs); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 1858 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 1859 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 1860 | * 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] | 1861 | * result in an array) |
| 1862 | * also esimate a upper bound for all the number formats in the string, |
| 1863 | * numbers will be formated in step 3 and be keept in a '\0'-separated |
| 1864 | * buffer before putting everything together. */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1865 | for (f = format; *f; f++) { |
| 1866 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 1867 | int longlongflag; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1868 | /* skip width or width.precision (eg. "1.2" of "%1.2f") */ |
| 1869 | f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL); |
| 1870 | if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V') |
| 1871 | ++callcount; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1872 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1873 | else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') { |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1874 | #ifdef HAVE_LONG_LONG |
| 1875 | if (longlongflag) { |
| 1876 | if (width < MAX_LONG_LONG_CHARS) |
| 1877 | width = MAX_LONG_LONG_CHARS; |
| 1878 | } |
| 1879 | else |
| 1880 | #endif |
| 1881 | /* MAX_LONG_CHARS is enough to hold a 64-bit integer, |
| 1882 | including sign. Decimal takes the most space. This |
| 1883 | isn't enough for octal. If a width is specified we |
| 1884 | need more (which we allocate later). */ |
| 1885 | if (width < MAX_LONG_CHARS) |
| 1886 | width = MAX_LONG_CHARS; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1887 | |
| 1888 | /* account for the size + '\0' to separate numbers |
| 1889 | inside of the numberresults buffer */ |
| 1890 | numbersize += (width + 1); |
| 1891 | } |
| 1892 | } |
| 1893 | else if ((unsigned char)*f > 127) { |
| 1894 | PyErr_Format(PyExc_ValueError, |
| 1895 | "PyUnicode_FromFormatV() expects an ASCII-encoded format " |
| 1896 | "string, got a non-ASCII byte: 0x%02x", |
| 1897 | (unsigned char)*f); |
| 1898 | return NULL; |
| 1899 | } |
| 1900 | } |
| 1901 | /* step 2: allocate memory for the results of |
| 1902 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
| 1903 | if (callcount) { |
| 1904 | callresults = PyObject_Malloc(sizeof(PyObject *) * callcount); |
| 1905 | if (!callresults) { |
| 1906 | PyErr_NoMemory(); |
| 1907 | return NULL; |
| 1908 | } |
| 1909 | callresult = callresults; |
| 1910 | } |
| 1911 | /* step 2.5: allocate memory for the results of formating numbers */ |
| 1912 | if (numbersize) { |
| 1913 | numberresults = PyObject_Malloc(numbersize); |
| 1914 | if (!numberresults) { |
| 1915 | PyErr_NoMemory(); |
| 1916 | goto fail; |
| 1917 | } |
| 1918 | numberresult = numberresults; |
| 1919 | } |
| 1920 | |
| 1921 | /* step 3: format numbers and figure out how large a buffer we need */ |
| 1922 | for (f = format; *f; f++) { |
| 1923 | if (*f == '%') { |
| 1924 | const char* p; |
| 1925 | int longflag; |
| 1926 | int longlongflag; |
| 1927 | int size_tflag; |
| 1928 | int numprinted; |
| 1929 | |
| 1930 | p = f; |
| 1931 | zeropad = (f[1] == '0'); |
| 1932 | f = parse_format_flags(f, &width, &precision, |
| 1933 | &longflag, &longlongflag, &size_tflag); |
| 1934 | switch (*f) { |
| 1935 | case 'c': |
| 1936 | { |
| 1937 | Py_UCS4 ordinal = va_arg(count, int); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 1938 | maxchar = Py_MAX(maxchar, ordinal); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1939 | n++; |
| 1940 | break; |
| 1941 | } |
| 1942 | case '%': |
| 1943 | n++; |
| 1944 | break; |
| 1945 | case 'i': |
| 1946 | case 'd': |
| 1947 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1948 | width, precision, *f); |
| 1949 | if (longflag) |
| 1950 | numprinted = sprintf(numberresult, fmt, |
| 1951 | va_arg(count, long)); |
| 1952 | #ifdef HAVE_LONG_LONG |
| 1953 | else if (longlongflag) |
| 1954 | numprinted = sprintf(numberresult, fmt, |
| 1955 | va_arg(count, PY_LONG_LONG)); |
| 1956 | #endif |
| 1957 | else if (size_tflag) |
| 1958 | numprinted = sprintf(numberresult, fmt, |
| 1959 | va_arg(count, Py_ssize_t)); |
| 1960 | else |
| 1961 | numprinted = sprintf(numberresult, fmt, |
| 1962 | va_arg(count, int)); |
| 1963 | n += numprinted; |
| 1964 | /* advance by +1 to skip over the '\0' */ |
| 1965 | numberresult += (numprinted + 1); |
| 1966 | assert(*(numberresult - 1) == '\0'); |
| 1967 | assert(*(numberresult - 2) != '\0'); |
| 1968 | assert(numprinted >= 0); |
| 1969 | assert(numberresult <= numberresults + numbersize); |
| 1970 | break; |
| 1971 | case 'u': |
| 1972 | makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, |
| 1973 | width, precision, 'u'); |
| 1974 | if (longflag) |
| 1975 | numprinted = sprintf(numberresult, fmt, |
| 1976 | va_arg(count, unsigned long)); |
| 1977 | #ifdef HAVE_LONG_LONG |
| 1978 | else if (longlongflag) |
| 1979 | numprinted = sprintf(numberresult, fmt, |
| 1980 | va_arg(count, unsigned PY_LONG_LONG)); |
| 1981 | #endif |
| 1982 | else if (size_tflag) |
| 1983 | numprinted = sprintf(numberresult, fmt, |
| 1984 | va_arg(count, size_t)); |
| 1985 | else |
| 1986 | numprinted = sprintf(numberresult, fmt, |
| 1987 | va_arg(count, unsigned int)); |
| 1988 | n += numprinted; |
| 1989 | numberresult += (numprinted + 1); |
| 1990 | assert(*(numberresult - 1) == '\0'); |
| 1991 | assert(*(numberresult - 2) != '\0'); |
| 1992 | assert(numprinted >= 0); |
| 1993 | assert(numberresult <= numberresults + numbersize); |
| 1994 | break; |
| 1995 | case 'x': |
| 1996 | makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x'); |
| 1997 | numprinted = sprintf(numberresult, fmt, va_arg(count, int)); |
| 1998 | n += numprinted; |
| 1999 | numberresult += (numprinted + 1); |
| 2000 | assert(*(numberresult - 1) == '\0'); |
| 2001 | assert(*(numberresult - 2) != '\0'); |
| 2002 | assert(numprinted >= 0); |
| 2003 | assert(numberresult <= numberresults + numbersize); |
| 2004 | break; |
| 2005 | case 'p': |
| 2006 | numprinted = sprintf(numberresult, "%p", va_arg(count, void*)); |
| 2007 | /* %p is ill-defined: ensure leading 0x. */ |
| 2008 | if (numberresult[1] == 'X') |
| 2009 | numberresult[1] = 'x'; |
| 2010 | else if (numberresult[1] != 'x') { |
| 2011 | memmove(numberresult + 2, numberresult, |
| 2012 | strlen(numberresult) + 1); |
| 2013 | numberresult[0] = '0'; |
| 2014 | numberresult[1] = 'x'; |
| 2015 | numprinted += 2; |
| 2016 | } |
| 2017 | n += numprinted; |
| 2018 | numberresult += (numprinted + 1); |
| 2019 | assert(*(numberresult - 1) == '\0'); |
| 2020 | assert(*(numberresult - 2) != '\0'); |
| 2021 | assert(numprinted >= 0); |
| 2022 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2023 | break; |
| 2024 | case 's': |
| 2025 | { |
| 2026 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 2027 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2028 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 2029 | if (!str) |
| 2030 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2031 | /* since PyUnicode_DecodeUTF8 returns already flexible |
| 2032 | unicode objects, there is no need to call ready on them */ |
| 2033 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2034 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2035 | n += PyUnicode_GET_LENGTH(str); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2036 | /* Remember the str and switch to the next slot */ |
| 2037 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2038 | break; |
| 2039 | } |
| 2040 | case 'U': |
| 2041 | { |
| 2042 | PyObject *obj = va_arg(count, PyObject *); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2043 | assert(obj && _PyUnicode_CHECK(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2044 | if (PyUnicode_READY(obj) == -1) |
| 2045 | goto fail; |
| 2046 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2047 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2048 | n += PyUnicode_GET_LENGTH(obj); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2049 | break; |
| 2050 | } |
| 2051 | case 'V': |
| 2052 | { |
| 2053 | PyObject *obj = va_arg(count, PyObject *); |
| 2054 | const char *str = va_arg(count, const char *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2055 | PyObject *str_obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2056 | assert(obj || str); |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 2057 | assert(!obj || _PyUnicode_CHECK(obj)); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2058 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2059 | if (PyUnicode_READY(obj) == -1) |
| 2060 | goto fail; |
| 2061 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2062 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2063 | n += PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2064 | *callresult++ = NULL; |
| 2065 | } |
| 2066 | else { |
| 2067 | str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace"); |
| 2068 | if (!str_obj) |
| 2069 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2070 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2071 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2072 | n += PyUnicode_GET_LENGTH(str_obj); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2073 | *callresult++ = str_obj; |
| 2074 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2075 | break; |
| 2076 | } |
| 2077 | case 'S': |
| 2078 | { |
| 2079 | PyObject *obj = va_arg(count, PyObject *); |
| 2080 | PyObject *str; |
| 2081 | assert(obj); |
| 2082 | str = PyObject_Str(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2083 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2084 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2085 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(str); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2086 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2087 | n += PyUnicode_GET_LENGTH(str); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2088 | /* Remember the str and switch to the next slot */ |
| 2089 | *callresult++ = str; |
| 2090 | break; |
| 2091 | } |
| 2092 | case 'R': |
| 2093 | { |
| 2094 | PyObject *obj = va_arg(count, PyObject *); |
| 2095 | PyObject *repr; |
| 2096 | assert(obj); |
| 2097 | repr = PyObject_Repr(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2098 | if (!repr || PyUnicode_READY(repr) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2099 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2100 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2101 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2102 | n += PyUnicode_GET_LENGTH(repr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2103 | /* Remember the repr and switch to the next slot */ |
| 2104 | *callresult++ = repr; |
| 2105 | break; |
| 2106 | } |
| 2107 | case 'A': |
| 2108 | { |
| 2109 | PyObject *obj = va_arg(count, PyObject *); |
| 2110 | PyObject *ascii; |
| 2111 | assert(obj); |
| 2112 | ascii = PyObject_ASCII(obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2113 | if (!ascii || PyUnicode_READY(ascii) == -1) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2114 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2115 | argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 2116 | maxchar = Py_MAX(maxchar, argmaxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2117 | n += PyUnicode_GET_LENGTH(ascii); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2118 | /* Remember the repr and switch to the next slot */ |
| 2119 | *callresult++ = ascii; |
| 2120 | break; |
| 2121 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2122 | default: |
| 2123 | /* if we stumble upon an unknown |
| 2124 | formatting code, copy the rest of |
| 2125 | the format string to the output |
| 2126 | string. (we cannot just skip the |
| 2127 | code, since there's no way to know |
| 2128 | what's in the argument list) */ |
| 2129 | n += strlen(p); |
| 2130 | goto expand; |
| 2131 | } |
| 2132 | } else |
| 2133 | n++; |
| 2134 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2135 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2136 | /* step 4: fill the buffer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2137 | /* Since we've analyzed how much space we need, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2138 | we don't have to resize the string. |
| 2139 | There can be no errors beyond this point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2140 | string = (PyUnicodeObject *)PyUnicode_New(n, maxchar); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2141 | if (!string) |
| 2142 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2143 | kind = PyUnicode_KIND(string); |
| 2144 | data = PyUnicode_DATA(string); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2145 | callresult = callresults; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2146 | numberresult = numberresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2147 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2148 | for (i = 0, f = format; *f; f++) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2149 | if (*f == '%') { |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2150 | const char* p; |
Victor Stinner | 9686545 | 2011-03-01 23:44:09 +0000 | [diff] [blame] | 2151 | |
| 2152 | p = f; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2153 | f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL); |
| 2154 | /* checking for == because the last argument could be a empty |
| 2155 | string, which causes i to point to end, the assert at the end of |
| 2156 | the loop */ |
| 2157 | assert(i <= PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2158 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2159 | switch (*f) { |
| 2160 | case 'c': |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2161 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2162 | const int ordinal = va_arg(vargs, int); |
| 2163 | PyUnicode_WRITE(kind, data, i++, ordinal); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2164 | break; |
Victor Stinner | 5ed8b2c | 2011-02-21 21:13:44 +0000 | [diff] [blame] | 2165 | } |
Victor Stinner | 6d970f4 | 2011-03-02 00:04:25 +0000 | [diff] [blame] | 2166 | case 'i': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2167 | case 'd': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2168 | case 'u': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2169 | case 'x': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2170 | case 'p': |
| 2171 | /* unused, since we already have the result */ |
| 2172 | if (*f == 'p') |
| 2173 | (void) va_arg(vargs, void *); |
| 2174 | else |
| 2175 | (void) va_arg(vargs, int); |
| 2176 | /* extract the result from numberresults and append. */ |
| 2177 | for (; *numberresult; ++i, ++numberresult) |
| 2178 | PyUnicode_WRITE(kind, data, i, *numberresult); |
| 2179 | /* skip over the separating '\0' */ |
| 2180 | assert(*numberresult == '\0'); |
| 2181 | numberresult++; |
| 2182 | assert(numberresult <= numberresults + numbersize); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2183 | break; |
| 2184 | case 's': |
| 2185 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2186 | /* unused, since we already have the result */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2187 | Py_ssize_t size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2188 | (void) va_arg(vargs, char *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2189 | size = PyUnicode_GET_LENGTH(*callresult); |
| 2190 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2191 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2192 | *callresult, 0, |
| 2193 | size) < 0) |
| 2194 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2195 | i += size; |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 2196 | /* We're done with the unicode()/repr() => forget it */ |
| 2197 | Py_DECREF(*callresult); |
| 2198 | /* switch to next unicode()/repr() result */ |
| 2199 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2200 | break; |
| 2201 | } |
| 2202 | case 'U': |
| 2203 | { |
| 2204 | PyObject *obj = va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2205 | Py_ssize_t size; |
| 2206 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
| 2207 | size = PyUnicode_GET_LENGTH(obj); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2208 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2209 | obj, 0, |
| 2210 | size) < 0) |
| 2211 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2212 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2213 | break; |
| 2214 | } |
| 2215 | case 'V': |
| 2216 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2217 | Py_ssize_t size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2218 | PyObject *obj = va_arg(vargs, PyObject *); |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2219 | va_arg(vargs, const char *); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2220 | if (obj) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2221 | size = PyUnicode_GET_LENGTH(obj); |
| 2222 | assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2223 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2224 | obj, 0, |
| 2225 | size) < 0) |
| 2226 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2227 | i += size; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2228 | } else { |
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) <= |
| 2231 | PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2232 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2233 | *callresult, |
| 2234 | 0, size) < 0) |
| 2235 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2236 | i += size; |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2237 | Py_DECREF(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2238 | } |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2239 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2240 | break; |
| 2241 | } |
| 2242 | case 'S': |
| 2243 | case 'R': |
Victor Stinner | 9a90900 | 2010-10-18 20:59:24 +0000 | [diff] [blame] | 2244 | case 'A': |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2245 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2246 | /* unused, since we already have the result */ |
| 2247 | (void) va_arg(vargs, PyObject *); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2248 | assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 2249 | if (PyUnicode_CopyCharacters((PyObject*)string, i, |
| 2250 | *callresult, 0, |
| 2251 | PyUnicode_GET_LENGTH(*callresult)) < 0) |
| 2252 | goto fail; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2253 | i += PyUnicode_GET_LENGTH(*callresult); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2254 | /* We're done with the unicode()/repr() => forget it */ |
| 2255 | Py_DECREF(*callresult); |
| 2256 | /* switch to next unicode()/repr() result */ |
| 2257 | ++callresult; |
| 2258 | break; |
| 2259 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2260 | case '%': |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2261 | PyUnicode_WRITE(kind, data, i++, '%'); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2262 | break; |
| 2263 | default: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2264 | for (; *p; ++p, ++i) |
| 2265 | PyUnicode_WRITE(kind, data, i, *p); |
| 2266 | assert(i == PyUnicode_GET_LENGTH(string)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2267 | goto end; |
| 2268 | } |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2269 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2270 | else { |
| 2271 | assert(i < PyUnicode_GET_LENGTH(string)); |
| 2272 | PyUnicode_WRITE(kind, data, i++, *f); |
| 2273 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2274 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2275 | assert(i == PyUnicode_GET_LENGTH(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2276 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2277 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2278 | if (callresults) |
| 2279 | PyObject_Free(callresults); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2280 | if (numberresults) |
| 2281 | PyObject_Free(numberresults); |
| 2282 | return (PyObject *)string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2283 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2284 | if (callresults) { |
| 2285 | PyObject **callresult2 = callresults; |
| 2286 | while (callresult2 < callresult) { |
Victor Stinner | 2512a8b | 2011-03-01 22:46:52 +0000 | [diff] [blame] | 2287 | Py_XDECREF(*callresult2); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2288 | ++callresult2; |
| 2289 | } |
| 2290 | PyObject_Free(callresults); |
| 2291 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2292 | if (numberresults) |
| 2293 | PyObject_Free(numberresults); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2294 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2297 | PyObject * |
| 2298 | PyUnicode_FromFormat(const char *format, ...) |
| 2299 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2300 | PyObject* ret; |
| 2301 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2302 | |
| 2303 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2304 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2305 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2306 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2307 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2308 | ret = PyUnicode_FromFormatV(format, vargs); |
| 2309 | va_end(vargs); |
| 2310 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2313 | #ifdef HAVE_WCHAR_H |
| 2314 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2315 | /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(): |
| 2316 | convert a Unicode object to a wide character string. |
| 2317 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2318 | - 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] | 2319 | character) required to convert the unicode object. Ignore size argument. |
| 2320 | |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2321 | - Otherwise: return the number of wide characters (excluding the null |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2322 | character) written into w. Write at most size wide characters (including |
Victor Stinner | d88d983 | 2011-09-06 02:00:05 +0200 | [diff] [blame] | 2323 | the null character). */ |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2324 | static Py_ssize_t |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2325 | unicode_aswidechar(PyUnicodeObject *unicode, |
| 2326 | wchar_t *w, |
| 2327 | Py_ssize_t size) |
| 2328 | { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2329 | Py_ssize_t res; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2330 | const wchar_t *wstr; |
| 2331 | |
| 2332 | wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res); |
| 2333 | if (wstr == NULL) |
| 2334 | return -1; |
| 2335 | |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2336 | if (w != NULL) { |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2337 | if (size > res) |
| 2338 | size = res + 1; |
| 2339 | else |
| 2340 | res = size; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2341 | Py_MEMCPY(w, wstr, size * sizeof(wchar_t)); |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2342 | return res; |
| 2343 | } |
| 2344 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2345 | return res + 1; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | Py_ssize_t |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2349 | PyUnicode_AsWideChar(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2350 | wchar_t *w, |
| 2351 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2352 | { |
| 2353 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2354 | PyErr_BadInternalCall(); |
| 2355 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2356 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 2357 | return unicode_aswidechar((PyUnicodeObject*)unicode, w, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2360 | wchar_t* |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2361 | PyUnicode_AsWideCharString(PyObject *unicode, |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2362 | Py_ssize_t *size) |
| 2363 | { |
| 2364 | wchar_t* buffer; |
| 2365 | Py_ssize_t buflen; |
| 2366 | |
| 2367 | if (unicode == NULL) { |
| 2368 | PyErr_BadInternalCall(); |
| 2369 | return NULL; |
| 2370 | } |
| 2371 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2372 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2373 | if (buflen == -1) |
| 2374 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2375 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) { |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2376 | PyErr_NoMemory(); |
| 2377 | return NULL; |
| 2378 | } |
| 2379 | |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2380 | buffer = PyMem_MALLOC(buflen * sizeof(wchar_t)); |
| 2381 | if (buffer == NULL) { |
| 2382 | PyErr_NoMemory(); |
| 2383 | return NULL; |
| 2384 | } |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2385 | buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2386 | if (buflen == -1) |
| 2387 | return NULL; |
Victor Stinner | 5593d8a | 2010-10-02 11:11:27 +0000 | [diff] [blame] | 2388 | if (size != NULL) |
| 2389 | *size = buflen; |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 2390 | return buffer; |
| 2391 | } |
| 2392 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2393 | #endif /* HAVE_WCHAR_H */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2394 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2395 | PyObject * |
| 2396 | PyUnicode_FromOrdinal(int ordinal) |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2397 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2398 | PyObject *v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2399 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2400 | PyErr_SetString(PyExc_ValueError, |
| 2401 | "chr() arg not in range(0x110000)"); |
| 2402 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2403 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 2404 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2405 | if (ordinal < 256) |
| 2406 | return get_latin1_char(ordinal); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2407 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2408 | v = PyUnicode_New(1, ordinal); |
| 2409 | if (v == NULL) |
| 2410 | return NULL; |
| 2411 | PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal); |
| 2412 | return v; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2415 | PyObject * |
| 2416 | PyUnicode_FromObject(register PyObject *obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2417 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2418 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2419 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2420 | if (PyUnicode_CheckExact(obj)) { |
Victor Stinner | d3a83d5 | 2011-10-01 03:09:33 +0200 | [diff] [blame] | 2421 | if (PyUnicode_READY(obj)) |
| 2422 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2423 | Py_INCREF(obj); |
| 2424 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2425 | } |
| 2426 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2427 | /* For a Unicode subtype that's not a Unicode object, |
| 2428 | return a true Unicode object with the same data. */ |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 2429 | return PyUnicode_Copy(obj); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2430 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2431 | PyErr_Format(PyExc_TypeError, |
| 2432 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2433 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2434 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2437 | PyObject * |
| 2438 | PyUnicode_FromEncodedObject(register PyObject *obj, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2439 | const char *encoding, |
| 2440 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2441 | { |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2442 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2443 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2445 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2446 | PyErr_BadInternalCall(); |
| 2447 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2448 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2449 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2450 | /* Decoding bytes objects is the most common case and should be fast */ |
| 2451 | if (PyBytes_Check(obj)) { |
| 2452 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2453 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2454 | v = unicode_empty; |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2455 | } |
| 2456 | else { |
| 2457 | v = PyUnicode_Decode( |
| 2458 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 2459 | encoding, errors); |
| 2460 | } |
| 2461 | return v; |
| 2462 | } |
| 2463 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2464 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2465 | PyErr_SetString(PyExc_TypeError, |
| 2466 | "decoding str is not supported"); |
| 2467 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2468 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 2469 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2470 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 2471 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 2472 | PyErr_Format(PyExc_TypeError, |
| 2473 | "coercing to str: need bytes, bytearray " |
| 2474 | "or buffer-like object, %.80s found", |
| 2475 | Py_TYPE(obj)->tp_name); |
| 2476 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 2477 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2478 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2479 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2480 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 2481 | v = unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2482 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2483 | else |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2484 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 2485 | |
Antoine Pitrou | b0fa831 | 2010-09-01 15:10:12 +0000 | [diff] [blame] | 2486 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 2487 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2490 | /* Convert encoding to lower case and replace '_' with '-' in order to |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2491 | catch e.g. UTF_8. Return 0 on error (encoding is longer than lower_len-1), |
| 2492 | 1 on success. */ |
| 2493 | static int |
| 2494 | normalize_encoding(const char *encoding, |
| 2495 | char *lower, |
| 2496 | size_t lower_len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2497 | { |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2498 | const char *e; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2499 | char *l; |
| 2500 | char *l_end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2501 | |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2502 | e = encoding; |
| 2503 | l = lower; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2504 | l_end = &lower[lower_len - 1]; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2505 | while (*e) { |
| 2506 | if (l == l_end) |
| 2507 | return 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2508 | if (Py_ISUPPER(*e)) { |
| 2509 | *l++ = Py_TOLOWER(*e++); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2510 | } |
| 2511 | else if (*e == '_') { |
| 2512 | *l++ = '-'; |
| 2513 | e++; |
| 2514 | } |
| 2515 | else { |
| 2516 | *l++ = *e++; |
| 2517 | } |
| 2518 | } |
| 2519 | *l = '\0'; |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2520 | return 1; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2523 | PyObject * |
| 2524 | PyUnicode_Decode(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2525 | Py_ssize_t size, |
| 2526 | const char *encoding, |
| 2527 | const char *errors) |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2528 | { |
| 2529 | PyObject *buffer = NULL, *unicode; |
| 2530 | Py_buffer info; |
| 2531 | char lower[11]; /* Enough for any encoding shortcut */ |
| 2532 | |
| 2533 | if (encoding == NULL) |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2534 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2535 | |
| 2536 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2537 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2538 | if ((strcmp(lower, "utf-8") == 0) || |
| 2539 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2540 | return PyUnicode_DecodeUTF8(s, size, errors); |
| 2541 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2542 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2543 | (strcmp(lower, "iso-8859-1") == 0)) |
| 2544 | return PyUnicode_DecodeLatin1(s, size, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2545 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2546 | else if (strcmp(lower, "mbcs") == 0) |
| 2547 | return PyUnicode_DecodeMBCS(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2548 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2549 | else if (strcmp(lower, "ascii") == 0) |
| 2550 | return PyUnicode_DecodeASCII(s, size, errors); |
| 2551 | else if (strcmp(lower, "utf-16") == 0) |
| 2552 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 2553 | else if (strcmp(lower, "utf-32") == 0) |
| 2554 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
| 2555 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2556 | |
| 2557 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 2558 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 2559 | 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] | 2560 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 2561 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2562 | if (buffer == NULL) |
| 2563 | goto onError; |
| 2564 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 2565 | if (unicode == NULL) |
| 2566 | goto onError; |
| 2567 | if (!PyUnicode_Check(unicode)) { |
| 2568 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2569 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2570 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2571 | Py_DECREF(unicode); |
| 2572 | goto onError; |
| 2573 | } |
| 2574 | Py_DECREF(buffer); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 2575 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2576 | Py_DECREF(unicode); |
| 2577 | return NULL; |
| 2578 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2579 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2580 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2581 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2582 | Py_XDECREF(buffer); |
| 2583 | return NULL; |
| 2584 | } |
| 2585 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2586 | PyObject * |
| 2587 | PyUnicode_AsDecodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2588 | const char *encoding, |
| 2589 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2590 | { |
| 2591 | PyObject *v; |
| 2592 | |
| 2593 | if (!PyUnicode_Check(unicode)) { |
| 2594 | PyErr_BadArgument(); |
| 2595 | goto onError; |
| 2596 | } |
| 2597 | |
| 2598 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2599 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2600 | |
| 2601 | /* Decode via the codec registry */ |
| 2602 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2603 | if (v == NULL) |
| 2604 | goto onError; |
| 2605 | return v; |
| 2606 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2607 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2608 | return NULL; |
| 2609 | } |
| 2610 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2611 | PyObject * |
| 2612 | PyUnicode_AsDecodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2613 | const char *encoding, |
| 2614 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2615 | { |
| 2616 | PyObject *v; |
| 2617 | |
| 2618 | if (!PyUnicode_Check(unicode)) { |
| 2619 | PyErr_BadArgument(); |
| 2620 | goto onError; |
| 2621 | } |
| 2622 | |
| 2623 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2624 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2625 | |
| 2626 | /* Decode via the codec registry */ |
| 2627 | v = PyCodec_Decode(unicode, encoding, errors); |
| 2628 | if (v == NULL) |
| 2629 | goto onError; |
| 2630 | if (!PyUnicode_Check(v)) { |
| 2631 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2632 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2633 | Py_TYPE(v)->tp_name); |
| 2634 | Py_DECREF(v); |
| 2635 | goto onError; |
| 2636 | } |
| 2637 | return v; |
| 2638 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2639 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2640 | return NULL; |
| 2641 | } |
| 2642 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2643 | PyObject * |
| 2644 | PyUnicode_Encode(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2645 | Py_ssize_t size, |
| 2646 | const char *encoding, |
| 2647 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2648 | { |
| 2649 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2650 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2651 | unicode = PyUnicode_FromUnicode(s, size); |
| 2652 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2653 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2654 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 2655 | Py_DECREF(unicode); |
| 2656 | return v; |
| 2657 | } |
| 2658 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2659 | PyObject * |
| 2660 | PyUnicode_AsEncodedObject(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2661 | const char *encoding, |
| 2662 | const char *errors) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2663 | { |
| 2664 | PyObject *v; |
| 2665 | |
| 2666 | if (!PyUnicode_Check(unicode)) { |
| 2667 | PyErr_BadArgument(); |
| 2668 | goto onError; |
| 2669 | } |
| 2670 | |
| 2671 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2672 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2673 | |
| 2674 | /* Encode via the codec registry */ |
| 2675 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2676 | if (v == NULL) |
| 2677 | goto onError; |
| 2678 | return v; |
| 2679 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2680 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2681 | return NULL; |
| 2682 | } |
| 2683 | |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2684 | PyObject * |
| 2685 | PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2686 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2687 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2688 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2689 | PyUnicode_GET_SIZE(unicode), |
| 2690 | NULL); |
| 2691 | #elif defined(__APPLE__) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2692 | return _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2693 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2694 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2695 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2696 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2697 | the Python codec requires to encode at least its own filename. Use the C |
| 2698 | version of the locale codec until the codec registry is initialized and |
| 2699 | the Python codec is loaded. |
| 2700 | |
| 2701 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2702 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2703 | subinterpreters. */ |
| 2704 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2705 | return PyUnicode_AsEncodedString(unicode, |
| 2706 | Py_FileSystemDefaultEncoding, |
| 2707 | "surrogateescape"); |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2708 | } |
| 2709 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2710 | /* locale encoding with surrogateescape */ |
| 2711 | wchar_t *wchar; |
| 2712 | char *bytes; |
| 2713 | PyObject *bytes_obj; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2714 | size_t error_pos; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2715 | |
| 2716 | wchar = PyUnicode_AsWideCharString(unicode, NULL); |
| 2717 | if (wchar == NULL) |
| 2718 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2719 | bytes = _Py_wchar2char(wchar, &error_pos); |
| 2720 | if (bytes == NULL) { |
| 2721 | if (error_pos != (size_t)-1) { |
| 2722 | char *errmsg = strerror(errno); |
| 2723 | PyObject *exc = NULL; |
| 2724 | if (errmsg == NULL) |
| 2725 | errmsg = "Py_wchar2char() failed"; |
| 2726 | raise_encode_exception(&exc, |
| 2727 | "filesystemencoding", |
| 2728 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 2729 | error_pos, error_pos+1, |
| 2730 | errmsg); |
| 2731 | Py_XDECREF(exc); |
| 2732 | } |
| 2733 | else |
| 2734 | PyErr_NoMemory(); |
| 2735 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2736 | return NULL; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 2737 | } |
| 2738 | PyMem_Free(wchar); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2739 | |
| 2740 | bytes_obj = PyBytes_FromString(bytes); |
| 2741 | PyMem_Free(bytes); |
| 2742 | return bytes_obj; |
Victor Stinner | c39211f | 2010-09-29 16:35:47 +0000 | [diff] [blame] | 2743 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2744 | #endif |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2747 | PyObject * |
| 2748 | PyUnicode_AsEncodedString(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2749 | const char *encoding, |
| 2750 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2751 | { |
| 2752 | PyObject *v; |
Victor Stinner | 600d3be | 2010-06-10 12:00:55 +0000 | [diff] [blame] | 2753 | char lower[11]; /* Enough for any encoding shortcut */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2754 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2755 | if (!PyUnicode_Check(unicode)) { |
| 2756 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2757 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2758 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2759 | |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2760 | if (encoding == NULL) { |
| 2761 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2762 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2763 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2764 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2765 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 2766 | |
| 2767 | /* Shortcuts for common default encodings */ |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2768 | if (normalize_encoding(encoding, lower, sizeof(lower))) { |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2769 | if ((strcmp(lower, "utf-8") == 0) || |
| 2770 | (strcmp(lower, "utf8") == 0)) |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2771 | { |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2772 | if (errors == NULL || strcmp(errors, "strict") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2773 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Victor Stinner | 2f283c2 | 2011-03-02 01:21:46 +0000 | [diff] [blame] | 2774 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2775 | return _PyUnicode_AsUTF8String(unicode, errors); |
Victor Stinner | a5c68c3 | 2011-03-02 01:03:14 +0000 | [diff] [blame] | 2776 | } |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2777 | else if ((strcmp(lower, "latin-1") == 0) || |
Alexander Belopolsky | 1d52146 | 2011-02-25 19:19:57 +0000 | [diff] [blame] | 2778 | (strcmp(lower, "latin1") == 0) || |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2779 | (strcmp(lower, "iso-8859-1") == 0)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2780 | return _PyUnicode_AsLatin1String(unicode, errors); |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2781 | #ifdef HAVE_MBCS |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2782 | else if (strcmp(lower, "mbcs") == 0) |
| 2783 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2784 | PyUnicode_GET_SIZE(unicode), |
| 2785 | errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2786 | #endif |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2787 | else if (strcmp(lower, "ascii") == 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2788 | return _PyUnicode_AsASCIIString(unicode, errors); |
Victor Stinner | 37296e8 | 2010-06-10 13:36:23 +0000 | [diff] [blame] | 2789 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2790 | |
| 2791 | /* Encode via the codec registry */ |
| 2792 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2793 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2794 | return NULL; |
| 2795 | |
| 2796 | /* The normal path */ |
| 2797 | if (PyBytes_Check(v)) |
| 2798 | return v; |
| 2799 | |
| 2800 | /* 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] | 2801 | if (PyByteArray_Check(v)) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2802 | int error; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2803 | PyObject *b; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 2804 | |
| 2805 | error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 2806 | "encoder %s returned bytearray instead of bytes", |
| 2807 | encoding); |
| 2808 | if (error) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2809 | Py_DECREF(v); |
| 2810 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2811 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2812 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 2813 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 2814 | Py_DECREF(v); |
| 2815 | return b; |
| 2816 | } |
| 2817 | |
| 2818 | PyErr_Format(PyExc_TypeError, |
| 2819 | "encoder did not return a bytes object (type=%.400s)", |
| 2820 | Py_TYPE(v)->tp_name); |
| 2821 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2822 | return NULL; |
| 2823 | } |
| 2824 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 2825 | PyObject * |
| 2826 | PyUnicode_AsEncodedUnicode(PyObject *unicode, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2827 | const char *encoding, |
| 2828 | const char *errors) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2829 | { |
| 2830 | PyObject *v; |
| 2831 | |
| 2832 | if (!PyUnicode_Check(unicode)) { |
| 2833 | PyErr_BadArgument(); |
| 2834 | goto onError; |
| 2835 | } |
| 2836 | |
| 2837 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2838 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2839 | |
| 2840 | /* Encode via the codec registry */ |
| 2841 | v = PyCodec_Encode(unicode, encoding, errors); |
| 2842 | if (v == NULL) |
| 2843 | goto onError; |
| 2844 | if (!PyUnicode_Check(v)) { |
| 2845 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 2846 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 2847 | Py_TYPE(v)->tp_name); |
| 2848 | Py_DECREF(v); |
| 2849 | goto onError; |
| 2850 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2851 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2852 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2853 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2854 | return NULL; |
| 2855 | } |
| 2856 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2857 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2858 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2859 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2860 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 2861 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2862 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 2863 | PyObject* |
| 2864 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 2865 | { |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 2866 | #ifdef HAVE_MBCS |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2867 | return PyUnicode_DecodeMBCS(s, size, NULL); |
| 2868 | #elif defined(__APPLE__) |
| 2869 | return PyUnicode_DecodeUTF8(s, size, "surrogateescape"); |
| 2870 | #else |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 2871 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 2872 | /* Bootstrap check: if the filesystem codec is implemented in Python, we |
| 2873 | cannot use it to encode and decode filenames before it is loaded. Load |
| 2874 | the Python codec requires to encode at least its own filename. Use the C |
| 2875 | version of the locale codec until the codec registry is initialized and |
| 2876 | the Python codec is loaded. |
| 2877 | |
| 2878 | Py_FileSystemDefaultEncoding is shared between all interpreters, we |
| 2879 | cannot only rely on it: check also interp->fscodec_initialized for |
| 2880 | subinterpreters. */ |
| 2881 | if (Py_FileSystemDefaultEncoding && interp->fscodec_initialized) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2882 | return PyUnicode_Decode(s, size, |
| 2883 | Py_FileSystemDefaultEncoding, |
Victor Stinner | b9a20ad | 2010-04-30 16:37:52 +0000 | [diff] [blame] | 2884 | "surrogateescape"); |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2885 | } |
| 2886 | else { |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2887 | /* locale encoding with surrogateescape */ |
| 2888 | wchar_t *wchar; |
| 2889 | PyObject *unicode; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2890 | size_t len; |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2891 | |
| 2892 | if (s[size] != '\0' || size != strlen(s)) { |
| 2893 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2894 | return NULL; |
| 2895 | } |
| 2896 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2897 | wchar = _Py_char2wchar(s, &len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2898 | if (wchar == NULL) |
Victor Stinner | d5af0a5 | 2010-11-08 23:34:29 +0000 | [diff] [blame] | 2899 | return PyErr_NoMemory(); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2900 | |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 2901 | unicode = PyUnicode_FromWideChar(wchar, len); |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 2902 | PyMem_Free(wchar); |
| 2903 | return unicode; |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2904 | } |
Victor Stinner | ad15872 | 2010-10-27 00:25:46 +0000 | [diff] [blame] | 2905 | #endif |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2908 | |
| 2909 | int |
| 2910 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 2911 | { |
| 2912 | PyObject *output = NULL; |
| 2913 | Py_ssize_t size; |
| 2914 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2915 | if (arg == NULL) { |
| 2916 | Py_DECREF(*(PyObject**)addr); |
| 2917 | return 1; |
| 2918 | } |
Victor Stinner | dcb2403 | 2010-04-22 12:08:36 +0000 | [diff] [blame] | 2919 | if (PyBytes_Check(arg)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2920 | output = arg; |
| 2921 | Py_INCREF(output); |
| 2922 | } |
| 2923 | else { |
| 2924 | arg = PyUnicode_FromObject(arg); |
| 2925 | if (!arg) |
| 2926 | return 0; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 2927 | output = PyUnicode_EncodeFSDefault(arg); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2928 | Py_DECREF(arg); |
| 2929 | if (!output) |
| 2930 | return 0; |
| 2931 | if (!PyBytes_Check(output)) { |
| 2932 | Py_DECREF(output); |
| 2933 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 2934 | return 0; |
| 2935 | } |
| 2936 | } |
Victor Stinner | 0ea2a46 | 2010-04-30 00:22:08 +0000 | [diff] [blame] | 2937 | size = PyBytes_GET_SIZE(output); |
| 2938 | data = PyBytes_AS_STRING(output); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2939 | if (size != strlen(data)) { |
Benjamin Peterson | 7a6b44a | 2011-08-18 13:51:47 -0500 | [diff] [blame] | 2940 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2941 | Py_DECREF(output); |
| 2942 | return 0; |
| 2943 | } |
| 2944 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2945 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2949 | int |
| 2950 | PyUnicode_FSDecoder(PyObject* arg, void* addr) |
| 2951 | { |
| 2952 | PyObject *output = NULL; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2953 | if (arg == NULL) { |
| 2954 | Py_DECREF(*(PyObject**)addr); |
| 2955 | return 1; |
| 2956 | } |
| 2957 | if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2958 | if (PyUnicode_READY(arg)) |
| 2959 | return 0; |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2960 | output = arg; |
| 2961 | Py_INCREF(output); |
| 2962 | } |
| 2963 | else { |
| 2964 | arg = PyBytes_FromObject(arg); |
| 2965 | if (!arg) |
| 2966 | return 0; |
| 2967 | output = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AS_STRING(arg), |
| 2968 | PyBytes_GET_SIZE(arg)); |
| 2969 | Py_DECREF(arg); |
| 2970 | if (!output) |
| 2971 | return 0; |
| 2972 | if (!PyUnicode_Check(output)) { |
| 2973 | Py_DECREF(output); |
| 2974 | PyErr_SetString(PyExc_TypeError, "decoder failed to return unicode"); |
| 2975 | return 0; |
| 2976 | } |
| 2977 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2978 | if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), |
| 2979 | PyUnicode_GET_LENGTH(output), 0, 1)) { |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 2980 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 2981 | Py_DECREF(output); |
| 2982 | return 0; |
| 2983 | } |
| 2984 | *(PyObject**)addr = output; |
| 2985 | return Py_CLEANUP_SUPPORTED; |
| 2986 | } |
| 2987 | |
| 2988 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2989 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2990 | PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 2991 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2992 | PyObject *bytes; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2993 | PyUnicodeObject *u = (PyUnicodeObject *)unicode; |
| 2994 | |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 2995 | if (!PyUnicode_Check(unicode)) { |
| 2996 | PyErr_BadArgument(); |
| 2997 | return NULL; |
| 2998 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2999 | if (PyUnicode_READY(u) == -1) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3000 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3001 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3002 | if (PyUnicode_UTF8(unicode) == NULL) { |
| 3003 | assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3004 | bytes = _PyUnicode_AsUTF8String(unicode, "strict"); |
| 3005 | if (bytes == NULL) |
| 3006 | return NULL; |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3007 | _PyUnicode_UTF8(u) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); |
| 3008 | if (_PyUnicode_UTF8(u) == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3009 | Py_DECREF(bytes); |
| 3010 | return NULL; |
| 3011 | } |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3012 | _PyUnicode_UTF8_LENGTH(u) = PyBytes_GET_SIZE(bytes); |
| 3013 | 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] | 3014 | Py_DECREF(bytes); |
| 3015 | } |
| 3016 | |
| 3017 | if (psize) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 3018 | *psize = PyUnicode_UTF8_LENGTH(unicode); |
| 3019 | return PyUnicode_UTF8(unicode); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | char* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3023 | PyUnicode_AsUTF8(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 3024 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3025 | return PyUnicode_AsUTF8AndSize(unicode, NULL); |
| 3026 | } |
| 3027 | |
| 3028 | #ifdef Py_DEBUG |
| 3029 | int unicode_as_unicode_calls = 0; |
| 3030 | #endif |
| 3031 | |
| 3032 | |
| 3033 | Py_UNICODE * |
| 3034 | PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size) |
| 3035 | { |
| 3036 | PyUnicodeObject *u; |
| 3037 | const unsigned char *one_byte; |
| 3038 | #if SIZEOF_WCHAR_T == 4 |
| 3039 | const Py_UCS2 *two_bytes; |
| 3040 | #else |
| 3041 | const Py_UCS4 *four_bytes; |
| 3042 | const Py_UCS4 *ucs4_end; |
| 3043 | Py_ssize_t num_surrogates; |
| 3044 | #endif |
| 3045 | wchar_t *w; |
| 3046 | wchar_t *wchar_end; |
| 3047 | |
| 3048 | if (!PyUnicode_Check(unicode)) { |
| 3049 | PyErr_BadArgument(); |
| 3050 | return NULL; |
| 3051 | } |
| 3052 | u = (PyUnicodeObject*)unicode; |
| 3053 | if (_PyUnicode_WSTR(u) == NULL) { |
| 3054 | /* Non-ASCII compact unicode object */ |
| 3055 | assert(_PyUnicode_KIND(u) != 0); |
| 3056 | assert(PyUnicode_IS_READY(u)); |
| 3057 | |
| 3058 | #ifdef Py_DEBUG |
| 3059 | ++unicode_as_unicode_calls; |
| 3060 | #endif |
| 3061 | |
| 3062 | if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) { |
| 3063 | #if SIZEOF_WCHAR_T == 2 |
| 3064 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3065 | ucs4_end = four_bytes + _PyUnicode_LENGTH(u); |
| 3066 | num_surrogates = 0; |
| 3067 | |
| 3068 | for (; four_bytes < ucs4_end; ++four_bytes) { |
| 3069 | if (*four_bytes > 0xFFFF) |
| 3070 | ++num_surrogates; |
| 3071 | } |
| 3072 | |
| 3073 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC( |
| 3074 | sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates)); |
| 3075 | if (!_PyUnicode_WSTR(u)) { |
| 3076 | PyErr_NoMemory(); |
| 3077 | return NULL; |
| 3078 | } |
| 3079 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates; |
| 3080 | |
| 3081 | w = _PyUnicode_WSTR(u); |
| 3082 | wchar_end = w + _PyUnicode_WSTR_LENGTH(u); |
| 3083 | four_bytes = PyUnicode_4BYTE_DATA(u); |
| 3084 | for (; four_bytes < ucs4_end; ++four_bytes, ++w) { |
| 3085 | if (*four_bytes > 0xFFFF) { |
| 3086 | /* encode surrogate pair in this case */ |
| 3087 | *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10); |
| 3088 | *w = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF); |
| 3089 | } |
| 3090 | else |
| 3091 | *w = *four_bytes; |
| 3092 | |
| 3093 | if (w > wchar_end) { |
| 3094 | assert(0 && "Miscalculated string end"); |
| 3095 | } |
| 3096 | } |
| 3097 | *w = 0; |
| 3098 | #else |
| 3099 | /* sizeof(wchar_t) == 4 */ |
| 3100 | Py_FatalError("Impossible unicode object state, wstr and str " |
| 3101 | "should share memory already."); |
| 3102 | return NULL; |
| 3103 | #endif |
| 3104 | } |
| 3105 | else { |
| 3106 | _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) * |
| 3107 | (_PyUnicode_LENGTH(u) + 1)); |
| 3108 | if (!_PyUnicode_WSTR(u)) { |
| 3109 | PyErr_NoMemory(); |
| 3110 | return NULL; |
| 3111 | } |
| 3112 | if (!PyUnicode_IS_COMPACT_ASCII(u)) |
| 3113 | _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u); |
| 3114 | w = _PyUnicode_WSTR(u); |
| 3115 | wchar_end = w + _PyUnicode_LENGTH(u); |
| 3116 | |
| 3117 | if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) { |
| 3118 | one_byte = PyUnicode_1BYTE_DATA(u); |
| 3119 | for (; w < wchar_end; ++one_byte, ++w) |
| 3120 | *w = *one_byte; |
| 3121 | /* null-terminate the wstr */ |
| 3122 | *w = 0; |
| 3123 | } |
| 3124 | else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) { |
| 3125 | #if SIZEOF_WCHAR_T == 4 |
| 3126 | two_bytes = PyUnicode_2BYTE_DATA(u); |
| 3127 | for (; w < wchar_end; ++two_bytes, ++w) |
| 3128 | *w = *two_bytes; |
| 3129 | /* null-terminate the wstr */ |
| 3130 | *w = 0; |
| 3131 | #else |
| 3132 | /* sizeof(wchar_t) == 2 */ |
| 3133 | PyObject_FREE(_PyUnicode_WSTR(u)); |
| 3134 | _PyUnicode_WSTR(u) = NULL; |
| 3135 | Py_FatalError("Impossible unicode object state, wstr " |
| 3136 | "and str should share memory already."); |
| 3137 | return NULL; |
| 3138 | #endif |
| 3139 | } |
| 3140 | else { |
| 3141 | assert(0 && "This should never happen."); |
| 3142 | } |
| 3143 | } |
| 3144 | } |
| 3145 | if (size != NULL) |
| 3146 | *size = PyUnicode_WSTR_LENGTH(u); |
| 3147 | return _PyUnicode_WSTR(u); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3150 | Py_UNICODE * |
| 3151 | PyUnicode_AsUnicode(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3152 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3153 | return PyUnicode_AsUnicodeAndSize(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3156 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3157 | Py_ssize_t |
| 3158 | PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3159 | { |
| 3160 | if (!PyUnicode_Check(unicode)) { |
| 3161 | PyErr_BadArgument(); |
| 3162 | goto onError; |
| 3163 | } |
| 3164 | return PyUnicode_GET_SIZE(unicode); |
| 3165 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3166 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3167 | return -1; |
| 3168 | } |
| 3169 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3170 | Py_ssize_t |
| 3171 | PyUnicode_GetLength(PyObject *unicode) |
| 3172 | { |
Victor Stinner | 5a706cf | 2011-10-02 00:36:53 +0200 | [diff] [blame] | 3173 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3174 | PyErr_BadArgument(); |
| 3175 | return -1; |
| 3176 | } |
| 3177 | |
| 3178 | return PyUnicode_GET_LENGTH(unicode); |
| 3179 | } |
| 3180 | |
| 3181 | Py_UCS4 |
| 3182 | PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index) |
| 3183 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 3184 | if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { |
| 3185 | PyErr_BadArgument(); |
| 3186 | return (Py_UCS4)-1; |
| 3187 | } |
| 3188 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3189 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3190 | return (Py_UCS4)-1; |
| 3191 | } |
| 3192 | return PyUnicode_READ_CHAR(unicode, index); |
| 3193 | } |
| 3194 | |
| 3195 | int |
| 3196 | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) |
| 3197 | { |
| 3198 | if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3199 | PyErr_BadArgument(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3200 | return -1; |
| 3201 | } |
Victor Stinner | cd9950f | 2011-10-02 00:34:53 +0200 | [diff] [blame] | 3202 | if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { |
| 3203 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 3204 | return -1; |
| 3205 | } |
| 3206 | if (_PyUnicode_Dirty(unicode)) |
| 3207 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3208 | PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |
| 3209 | index, ch); |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3213 | const char * |
| 3214 | PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3215 | { |
Victor Stinner | 42cb462 | 2010-09-01 19:39:01 +0000 | [diff] [blame] | 3216 | return "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3219 | /* create or adjust a UnicodeDecodeError */ |
| 3220 | static void |
| 3221 | make_decode_exception(PyObject **exceptionObject, |
| 3222 | const char *encoding, |
| 3223 | const char *input, Py_ssize_t length, |
| 3224 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3225 | const char *reason) |
| 3226 | { |
| 3227 | if (*exceptionObject == NULL) { |
| 3228 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 3229 | encoding, input, length, startpos, endpos, reason); |
| 3230 | } |
| 3231 | else { |
| 3232 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, startpos)) |
| 3233 | goto onError; |
| 3234 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, endpos)) |
| 3235 | goto onError; |
| 3236 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 3237 | goto onError; |
| 3238 | } |
| 3239 | return; |
| 3240 | |
| 3241 | onError: |
| 3242 | Py_DECREF(*exceptionObject); |
| 3243 | *exceptionObject = NULL; |
| 3244 | } |
| 3245 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3246 | /* error handling callback helper: |
| 3247 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3248 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3249 | and adjust various state variables. |
| 3250 | return 0 on success, -1 on error |
| 3251 | */ |
| 3252 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3253 | static int |
| 3254 | unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3255 | const char *encoding, const char *reason, |
| 3256 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 3257 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 3258 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3259 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3260 | 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] | 3261 | |
| 3262 | PyObject *restuple = NULL; |
| 3263 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3264 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3265 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3266 | Py_ssize_t requiredsize; |
| 3267 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3268 | const Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3269 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3270 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3271 | int res = -1; |
| 3272 | |
| 3273 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3274 | *errorHandler = PyCodec_LookupError(errors); |
| 3275 | if (*errorHandler == NULL) |
| 3276 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 3279 | make_decode_exception(exceptionObject, |
| 3280 | encoding, |
| 3281 | *input, *inend - *input, |
| 3282 | *startinpos, *endinpos, |
| 3283 | reason); |
| 3284 | if (*exceptionObject == NULL) |
| 3285 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3286 | |
| 3287 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 3288 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3289 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3290 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3291 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3292 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3293 | } |
| 3294 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3295 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3296 | |
| 3297 | /* Copy back the bytes variables, which might have been modified by the |
| 3298 | callback */ |
| 3299 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 3300 | if (!inputobj) |
| 3301 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3302 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3303 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3304 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3305 | *input = PyBytes_AS_STRING(inputobj); |
| 3306 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3307 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 3308 | /* we can DECREF safely, as the exception has another reference, |
| 3309 | so the object won't go away. */ |
| 3310 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3311 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3312 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3313 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3314 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3315 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 3316 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3317 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3318 | |
| 3319 | /* need more space? (at least enough for what we |
| 3320 | have+the replacement+the rest of the string (starting |
| 3321 | at the new input position), so we won't have to check space |
| 3322 | when there are no errors in the rest of the string) */ |
| 3323 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 3324 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3325 | requiredsize = *outpos + repsize + insize-newpos; |
| 3326 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3327 | if (requiredsize<2*outsize) |
| 3328 | requiredsize = 2*outsize; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3329 | if (PyUnicode_Resize((PyObject**)output, requiredsize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3330 | goto onError; |
| 3331 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3332 | } |
| 3333 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3334 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3335 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 3336 | *outptr += repsize; |
| 3337 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3338 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3339 | /* we made it! */ |
| 3340 | res = 0; |
| 3341 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3342 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3343 | Py_XDECREF(restuple); |
| 3344 | return res; |
| 3345 | } |
| 3346 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3347 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 3348 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3349 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 3350 | |
| 3351 | /* Three simple macros defining base-64. */ |
| 3352 | |
| 3353 | /* Is c a base-64 character? */ |
| 3354 | |
| 3355 | #define IS_BASE64(c) \ |
| 3356 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 3357 | ((c) >= 'a' && (c) <= 'z') || \ |
| 3358 | ((c) >= '0' && (c) <= '9') || \ |
| 3359 | (c) == '+' || (c) == '/') |
| 3360 | |
| 3361 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 3362 | |
| 3363 | #define FROM_BASE64(c) \ |
| 3364 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 3365 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 3366 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 3367 | (c) == '+' ? 62 : 63) |
| 3368 | |
| 3369 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 3370 | |
| 3371 | #define TO_BASE64(n) \ |
| 3372 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 3373 | |
| 3374 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 3375 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 3376 | * byte not decoding to itself is the + which begins a base64 |
| 3377 | * string. */ |
| 3378 | |
| 3379 | #define DECODE_DIRECT(c) \ |
| 3380 | ((c) <= 127 && (c) != '+') |
| 3381 | |
| 3382 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 3383 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 3384 | * the above). See RFC2152. This array identifies these different |
| 3385 | * sets: |
| 3386 | * 0 : "Set D" |
| 3387 | * alphanumeric and '(),-./:? |
| 3388 | * 1 : "Set O" |
| 3389 | * !"#$%&*;<=>@[]^_`{|} |
| 3390 | * 2 : "whitespace" |
| 3391 | * ht nl cr sp |
| 3392 | * 3 : special (must be base64 encoded) |
| 3393 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 3394 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3395 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3396 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3397 | char utf7_category[128] = { |
| 3398 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 3399 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 3400 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 3401 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 3402 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 3403 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 3404 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 3405 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 3406 | /* @ A B C D E F G H I J K L M N O */ |
| 3407 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3408 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 3409 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 3410 | /* ` a b c d e f g h i j k l m n o */ |
| 3411 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3412 | /* p q r s t u v w x y z { | } ~ del */ |
| 3413 | 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] | 3414 | }; |
| 3415 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3416 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 3417 | * answer depends on whether we are encoding set O as itself, and also |
| 3418 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 3419 | * clear that the answers to these questions vary between |
| 3420 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 3421 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3422 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 3423 | ((c) < 128 && (c) > 0 && \ |
| 3424 | ((utf7_category[(c)] == 0) || \ |
| 3425 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 3426 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3427 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3428 | PyObject * |
| 3429 | PyUnicode_DecodeUTF7(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3430 | Py_ssize_t size, |
| 3431 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3432 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3433 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 3434 | } |
| 3435 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3436 | /* The decoder. The only state we preserve is our read position, |
| 3437 | * i.e. how many characters we have consumed. So if we end in the |
| 3438 | * middle of a shift sequence we have to back off the read position |
| 3439 | * and the output to the beginning of the sequence, otherwise we lose |
| 3440 | * all the shift state (seen bits, number of bits seen, high |
| 3441 | * surrogate). */ |
| 3442 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3443 | PyObject * |
| 3444 | PyUnicode_DecodeUTF7Stateful(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3445 | Py_ssize_t size, |
| 3446 | const char *errors, |
| 3447 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3448 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3449 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3450 | Py_ssize_t startinpos; |
| 3451 | Py_ssize_t endinpos; |
| 3452 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3453 | const char *e; |
| 3454 | PyUnicodeObject *unicode; |
| 3455 | Py_UNICODE *p; |
| 3456 | const char *errmsg = ""; |
| 3457 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3458 | Py_UNICODE *shiftOutStart; |
| 3459 | unsigned int base64bits = 0; |
| 3460 | unsigned long base64buffer = 0; |
| 3461 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3462 | PyObject *errorHandler = NULL; |
| 3463 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3464 | |
| 3465 | unicode = _PyUnicode_New(size); |
| 3466 | if (!unicode) |
| 3467 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3468 | if (size == 0) { |
| 3469 | if (consumed) |
| 3470 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3471 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3472 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3473 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3474 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3475 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3476 | e = s + size; |
| 3477 | |
| 3478 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3479 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3480 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 3481 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3482 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3483 | if (inShift) { /* in a base-64 section */ |
| 3484 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 3485 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 3486 | base64bits += 6; |
| 3487 | s++; |
| 3488 | if (base64bits >= 16) { |
| 3489 | /* we have enough bits for a UTF-16 value */ |
| 3490 | Py_UNICODE outCh = (Py_UNICODE) |
| 3491 | (base64buffer >> (base64bits-16)); |
| 3492 | base64bits -= 16; |
| 3493 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 3494 | if (surrogate) { |
| 3495 | /* expecting a second surrogate */ |
| 3496 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3497 | #ifdef Py_UNICODE_WIDE |
| 3498 | *p++ = (((surrogate & 0x3FF)<<10) |
| 3499 | | (outCh & 0x3FF)) + 0x10000; |
| 3500 | #else |
| 3501 | *p++ = surrogate; |
| 3502 | *p++ = outCh; |
| 3503 | #endif |
| 3504 | surrogate = 0; |
| 3505 | } |
| 3506 | else { |
| 3507 | surrogate = 0; |
| 3508 | errmsg = "second surrogate missing"; |
| 3509 | goto utf7Error; |
| 3510 | } |
| 3511 | } |
| 3512 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 3513 | /* first surrogate */ |
| 3514 | surrogate = outCh; |
| 3515 | } |
| 3516 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 3517 | errmsg = "unexpected second surrogate"; |
| 3518 | goto utf7Error; |
| 3519 | } |
| 3520 | else { |
| 3521 | *p++ = outCh; |
| 3522 | } |
| 3523 | } |
| 3524 | } |
| 3525 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3526 | inShift = 0; |
| 3527 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3528 | if (surrogate) { |
| 3529 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3530 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3531 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3532 | if (base64bits > 0) { /* left-over bits */ |
| 3533 | if (base64bits >= 6) { |
| 3534 | /* We've seen at least one base-64 character */ |
| 3535 | errmsg = "partial character in shift sequence"; |
| 3536 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3537 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3538 | else { |
| 3539 | /* Some bits remain; they should be zero */ |
| 3540 | if (base64buffer != 0) { |
| 3541 | errmsg = "non-zero padding bits in shift sequence"; |
| 3542 | goto utf7Error; |
| 3543 | } |
| 3544 | } |
| 3545 | } |
| 3546 | if (ch != '-') { |
| 3547 | /* '-' is absorbed; other terminating |
| 3548 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3549 | *p++ = ch; |
| 3550 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3551 | } |
| 3552 | } |
| 3553 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3554 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3555 | s++; /* consume '+' */ |
| 3556 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3557 | s++; |
| 3558 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3559 | } |
| 3560 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3561 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3562 | shiftOutStart = p; |
| 3563 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3564 | } |
| 3565 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3566 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3567 | *p++ = ch; |
| 3568 | s++; |
| 3569 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3570 | else { |
| 3571 | startinpos = s-starts; |
| 3572 | s++; |
| 3573 | errmsg = "unexpected special character"; |
| 3574 | goto utf7Error; |
| 3575 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3576 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3577 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3578 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3579 | endinpos = s-starts; |
| 3580 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3581 | errors, &errorHandler, |
| 3582 | "utf7", errmsg, |
| 3583 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3584 | &unicode, &outpos, &p)) |
| 3585 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3588 | /* end of string */ |
| 3589 | |
| 3590 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 3591 | /* if we're in an inconsistent state, that's an error */ |
| 3592 | if (surrogate || |
| 3593 | (base64bits >= 6) || |
| 3594 | (base64bits > 0 && base64buffer != 0)) { |
| 3595 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 3596 | endinpos = size; |
| 3597 | if (unicode_decode_call_errorhandler( |
| 3598 | errors, &errorHandler, |
| 3599 | "utf7", "unterminated shift sequence", |
| 3600 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 3601 | &unicode, &outpos, &p)) |
| 3602 | goto onError; |
| 3603 | if (s < e) |
| 3604 | goto restart; |
| 3605 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3606 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3607 | |
| 3608 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3609 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3610 | if (inShift) { |
| 3611 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3612 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3613 | } |
| 3614 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3615 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3616 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 3617 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3618 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 3619 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3620 | goto onError; |
| 3621 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3622 | Py_XDECREF(errorHandler); |
| 3623 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 3624 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3625 | Py_DECREF(unicode); |
| 3626 | return NULL; |
| 3627 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3628 | return (PyObject *)unicode; |
| 3629 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3630 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3631 | Py_XDECREF(errorHandler); |
| 3632 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3633 | Py_DECREF(unicode); |
| 3634 | return NULL; |
| 3635 | } |
| 3636 | |
| 3637 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3638 | PyObject * |
| 3639 | PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3640 | Py_ssize_t size, |
| 3641 | int base64SetO, |
| 3642 | int base64WhiteSpace, |
| 3643 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3644 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3645 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3646 | /* It might be possible to tighten this worst case */ |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3647 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3648 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3649 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3650 | unsigned int base64bits = 0; |
| 3651 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3652 | char * out; |
| 3653 | char * start; |
| 3654 | |
| 3655 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3656 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3657 | |
Alexandre Vassalotti | e85bd98 | 2009-07-21 00:39:03 +0000 | [diff] [blame] | 3658 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3659 | return PyErr_NoMemory(); |
| 3660 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3661 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3662 | if (v == NULL) |
| 3663 | return NULL; |
| 3664 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3665 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3666 | for (;i < size; ++i) { |
| 3667 | Py_UNICODE ch = s[i]; |
| 3668 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3669 | if (inShift) { |
| 3670 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3671 | /* shifting out */ |
| 3672 | if (base64bits) { /* output remaining bits */ |
| 3673 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 3674 | base64buffer = 0; |
| 3675 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3676 | } |
| 3677 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3678 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 3679 | so no '-' is required, except if the character is itself a '-' */ |
| 3680 | if (IS_BASE64(ch) || ch == '-') { |
| 3681 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3682 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3683 | *out++ = (char) ch; |
| 3684 | } |
| 3685 | else { |
| 3686 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3687 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3688 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3689 | else { /* not in a shift sequence */ |
| 3690 | if (ch == '+') { |
| 3691 | *out++ = '+'; |
| 3692 | *out++ = '-'; |
| 3693 | } |
| 3694 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 3695 | *out++ = (char) ch; |
| 3696 | } |
| 3697 | else { |
| 3698 | *out++ = '+'; |
| 3699 | inShift = 1; |
| 3700 | goto encode_char; |
| 3701 | } |
| 3702 | } |
| 3703 | continue; |
| 3704 | encode_char: |
| 3705 | #ifdef Py_UNICODE_WIDE |
| 3706 | if (ch >= 0x10000) { |
| 3707 | /* code first surrogate */ |
| 3708 | base64bits += 16; |
| 3709 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 3710 | while (base64bits >= 6) { |
| 3711 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3712 | base64bits -= 6; |
| 3713 | } |
| 3714 | /* prepare second surrogate */ |
| 3715 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3716 | } |
| 3717 | #endif |
| 3718 | base64bits += 16; |
| 3719 | base64buffer = (base64buffer << 16) | ch; |
| 3720 | while (base64bits >= 6) { |
| 3721 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 3722 | base64bits -= 6; |
| 3723 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 3724 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3725 | if (base64bits) |
| 3726 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 3727 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3728 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3729 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 3730 | return NULL; |
| 3731 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 3734 | #undef IS_BASE64 |
| 3735 | #undef FROM_BASE64 |
| 3736 | #undef TO_BASE64 |
| 3737 | #undef DECODE_DIRECT |
| 3738 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 3739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3740 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 3741 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3742 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3743 | char utf8_code_length[256] = { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3744 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 3745 | illegal prefix. See RFC 3629 for details */ |
| 3746 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 3747 | 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] | 3748 | 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] | 3749 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3750 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3751 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 3752 | 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] | 3753 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 3754 | 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] | 3755 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 3756 | 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] | 3757 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 3758 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 3759 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 3760 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 3761 | 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] | 3762 | }; |
| 3763 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3764 | PyObject * |
| 3765 | PyUnicode_DecodeUTF8(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 3766 | Py_ssize_t size, |
| 3767 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3768 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3769 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 3770 | } |
| 3771 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3772 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 3773 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 3774 | |
| 3775 | /* Mask to quickly check whether a C 'long' contains a |
| 3776 | non-ASCII, UTF8-encoded char. */ |
| 3777 | #if (SIZEOF_LONG == 8) |
| 3778 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 3779 | #elif (SIZEOF_LONG == 4) |
| 3780 | # define ASCII_CHAR_MASK 0x80808080L |
| 3781 | #else |
| 3782 | # error C 'long' size should be either 4 or 8! |
| 3783 | #endif |
| 3784 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3785 | /* Scans a UTF-8 string and returns the maximum character to be expected, |
| 3786 | the size of the decoded unicode string and if any major errors were |
| 3787 | encountered. |
| 3788 | |
| 3789 | This function does check basic UTF-8 sanity, it does however NOT CHECK |
| 3790 | if the string contains surrogates, and if all continuation bytes are |
| 3791 | within the correct ranges, these checks are performed in |
| 3792 | PyUnicode_DecodeUTF8Stateful. |
| 3793 | |
| 3794 | If it sets has_errors to 1, it means the value of unicode_size and max_char |
| 3795 | will be bogus and you should not rely on useful information in them. |
| 3796 | */ |
| 3797 | static Py_UCS4 |
| 3798 | utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size, |
| 3799 | Py_ssize_t *unicode_size, Py_ssize_t* consumed, |
| 3800 | int *has_errors) |
| 3801 | { |
| 3802 | Py_ssize_t n; |
| 3803 | Py_ssize_t char_count = 0; |
| 3804 | Py_UCS4 max_char = 127, new_max; |
| 3805 | Py_UCS4 upper_bound; |
| 3806 | const unsigned char *p = (const unsigned char *)s; |
| 3807 | const unsigned char *end = p + string_size; |
| 3808 | const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK); |
| 3809 | int err = 0; |
| 3810 | |
| 3811 | for (; p < end && !err; ++p, ++char_count) { |
| 3812 | /* Only check value if it's not a ASCII char... */ |
| 3813 | if (*p < 0x80) { |
| 3814 | /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for |
| 3815 | an explanation. */ |
| 3816 | if (!((size_t) p & LONG_PTR_MASK)) { |
| 3817 | /* Help register allocation */ |
| 3818 | register const unsigned char *_p = p; |
| 3819 | while (_p < aligned_end) { |
| 3820 | unsigned long value = *(unsigned long *) _p; |
| 3821 | if (value & ASCII_CHAR_MASK) |
| 3822 | break; |
| 3823 | _p += SIZEOF_LONG; |
| 3824 | char_count += SIZEOF_LONG; |
| 3825 | } |
| 3826 | p = _p; |
| 3827 | if (p == end) |
| 3828 | break; |
| 3829 | } |
| 3830 | } |
| 3831 | if (*p >= 0x80) { |
| 3832 | n = utf8_code_length[*p]; |
| 3833 | new_max = max_char; |
| 3834 | switch (n) { |
| 3835 | /* invalid start byte */ |
| 3836 | case 0: |
| 3837 | err = 1; |
| 3838 | break; |
| 3839 | case 2: |
| 3840 | /* Code points between 0x00FF and 0x07FF inclusive. |
| 3841 | Approximate the upper bound of the code point, |
| 3842 | if this flips over 255 we can be sure it will be more |
| 3843 | than 255 and the string will need 2 bytes per code coint, |
| 3844 | if it stays under or equal to 255, we can be sure 1 byte |
| 3845 | is enough. |
| 3846 | ((*p & 0b00011111) << 6) | 0b00111111 */ |
| 3847 | upper_bound = ((*p & 0x1F) << 6) | 0x3F; |
| 3848 | if (max_char < upper_bound) |
| 3849 | new_max = upper_bound; |
| 3850 | /* Ensure we track at least that we left ASCII space. */ |
| 3851 | if (new_max < 128) |
| 3852 | new_max = 128; |
| 3853 | break; |
| 3854 | case 3: |
| 3855 | /* Between 0x0FFF and 0xFFFF inclusive, so values are |
| 3856 | always > 255 and <= 65535 and will always need 2 bytes. */ |
| 3857 | if (max_char < 65535) |
| 3858 | new_max = 65535; |
| 3859 | break; |
| 3860 | case 4: |
| 3861 | /* Code point will be above 0xFFFF for sure in this case. */ |
| 3862 | new_max = 65537; |
| 3863 | break; |
| 3864 | /* Internal error, this should be caught by the first if */ |
| 3865 | case 1: |
| 3866 | default: |
| 3867 | assert(0 && "Impossible case in utf8_max_char_and_size"); |
| 3868 | err = 1; |
| 3869 | } |
| 3870 | /* Instead of number of overall bytes for this code point, |
| 3871 | n containts the number of following bytes: */ |
| 3872 | --n; |
| 3873 | /* Check if the follow up chars are all valid continuation bytes */ |
| 3874 | if (n >= 1) { |
| 3875 | const unsigned char *cont; |
| 3876 | if ((p + n) >= end) { |
| 3877 | if (consumed == 0) |
| 3878 | /* incomplete data, non-incremental decoding */ |
| 3879 | err = 1; |
| 3880 | break; |
| 3881 | } |
| 3882 | for (cont = p + 1; cont < (p + n); ++cont) { |
| 3883 | if ((*cont & 0xc0) != 0x80) { |
| 3884 | err = 1; |
| 3885 | break; |
| 3886 | } |
| 3887 | } |
| 3888 | p += n; |
| 3889 | } |
| 3890 | else |
| 3891 | err = 1; |
| 3892 | max_char = new_max; |
| 3893 | } |
| 3894 | } |
| 3895 | |
| 3896 | if (unicode_size) |
| 3897 | *unicode_size = char_count; |
| 3898 | if (has_errors) |
| 3899 | *has_errors = err; |
| 3900 | return max_char; |
| 3901 | } |
| 3902 | |
| 3903 | /* Similar to PyUnicode_WRITE but can also write into wstr field |
| 3904 | of the legacy unicode representation */ |
| 3905 | #define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \ |
| 3906 | do { \ |
| 3907 | const int k_ = (kind); \ |
| 3908 | if (k_ == PyUnicode_WCHAR_KIND) \ |
| 3909 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 3910 | else if (k_ == PyUnicode_1BYTE_KIND) \ |
| 3911 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 3912 | else if (k_ == PyUnicode_2BYTE_KIND) \ |
| 3913 | ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \ |
| 3914 | else \ |
| 3915 | ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \ |
| 3916 | } while (0) |
| 3917 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 3918 | PyObject * |
| 3919 | PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3920 | Py_ssize_t size, |
| 3921 | const char *errors, |
| 3922 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3923 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3924 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3925 | int n; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 3926 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3927 | Py_ssize_t startinpos; |
| 3928 | Py_ssize_t endinpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3929 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3930 | PyUnicodeObject *unicode; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 3931 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3932 | PyObject *errorHandler = NULL; |
| 3933 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3934 | Py_UCS4 maxchar = 0; |
| 3935 | Py_ssize_t unicode_size; |
| 3936 | Py_ssize_t i; |
| 3937 | int kind; |
| 3938 | void *data; |
| 3939 | int has_errors; |
| 3940 | Py_UNICODE *error_outptr; |
| 3941 | #if SIZEOF_WCHAR_T == 2 |
| 3942 | Py_ssize_t wchar_offset = 0; |
| 3943 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3944 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3945 | if (size == 0) { |
| 3946 | if (consumed) |
| 3947 | *consumed = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3948 | return (PyObject *)PyUnicode_New(0, 0); |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3949 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3950 | maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size, |
| 3951 | consumed, &has_errors); |
| 3952 | if (has_errors) { |
| 3953 | unicode = _PyUnicode_New(size); |
| 3954 | if (!unicode) |
| 3955 | return NULL; |
| 3956 | kind = PyUnicode_WCHAR_KIND; |
| 3957 | data = PyUnicode_AS_UNICODE(unicode); |
| 3958 | assert(data != NULL); |
| 3959 | } |
| 3960 | else { |
| 3961 | unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar); |
| 3962 | if (!unicode) |
| 3963 | return NULL; |
| 3964 | /* When the string is ASCII only, just use memcpy and return. |
| 3965 | unicode_size may be != size if there is an incomplete UTF-8 |
| 3966 | sequence at the end of the ASCII block. */ |
| 3967 | if (maxchar < 128 && size == unicode_size) { |
| 3968 | Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size); |
| 3969 | return (PyObject *)unicode; |
| 3970 | } |
| 3971 | kind = PyUnicode_KIND(unicode); |
| 3972 | data = PyUnicode_DATA(unicode); |
| 3973 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3974 | /* Unpack UTF-8 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3975 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3976 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3977 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3978 | |
| 3979 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 3980 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3981 | |
| 3982 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3983 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 3984 | input will consist of an overwhelming majority of ASCII |
| 3985 | characters, we try to optimize for this case by checking |
| 3986 | as many characters as a C 'long' can contain. |
| 3987 | First, check if we can do an aligned read, as most CPUs have |
| 3988 | a penalty for unaligned reads. |
| 3989 | */ |
| 3990 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 3991 | /* Help register allocation */ |
| 3992 | register const char *_s = s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3993 | register Py_ssize_t _i = i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3994 | while (_s < aligned_end) { |
| 3995 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 3996 | and do a fast unrolled copy if it only contains ASCII |
| 3997 | characters. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3998 | unsigned long value = *(unsigned long *) _s; |
| 3999 | if (value & ASCII_CHAR_MASK) |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4000 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4001 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]); |
| 4002 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]); |
| 4003 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]); |
| 4004 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4005 | #if (SIZEOF_LONG == 8) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4006 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]); |
| 4007 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]); |
| 4008 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]); |
| 4009 | WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4010 | #endif |
| 4011 | _s += SIZEOF_LONG; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4012 | _i += SIZEOF_LONG; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4013 | } |
| 4014 | s = _s; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4015 | i = _i; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4016 | if (s == e) |
| 4017 | break; |
| 4018 | ch = (unsigned char)*s; |
| 4019 | } |
| 4020 | } |
| 4021 | |
| 4022 | if (ch < 0x80) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4023 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4024 | s++; |
| 4025 | continue; |
| 4026 | } |
| 4027 | |
| 4028 | n = utf8_code_length[ch]; |
| 4029 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4030 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4031 | if (consumed) |
| 4032 | break; |
| 4033 | else { |
| 4034 | errmsg = "unexpected end of data"; |
| 4035 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4036 | endinpos = startinpos+1; |
| 4037 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 4038 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4039 | goto utf8Error; |
| 4040 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4041 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4042 | |
| 4043 | switch (n) { |
| 4044 | |
| 4045 | case 0: |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4046 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4047 | startinpos = s-starts; |
| 4048 | endinpos = startinpos+1; |
| 4049 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4050 | |
| 4051 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4052 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4053 | startinpos = s-starts; |
| 4054 | endinpos = startinpos+1; |
| 4055 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4056 | |
| 4057 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4058 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4059 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4060 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4061 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4062 | goto utf8Error; |
| 4063 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4064 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4065 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4066 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4067 | break; |
| 4068 | |
| 4069 | case 3: |
Ezio Melotti | 9bf2b3a | 2010-07-03 04:52:19 +0000 | [diff] [blame] | 4070 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4071 | will result in surrogates in range d800-dfff. Surrogates are |
| 4072 | not valid UTF-8 so they are rejected. |
| 4073 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4074 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4075 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4076 | (s[2] & 0xc0) != 0x80 || |
| 4077 | ((unsigned char)s[0] == 0xE0 && |
| 4078 | (unsigned char)s[1] < 0xA0) || |
| 4079 | ((unsigned char)s[0] == 0xED && |
| 4080 | (unsigned char)s[1] > 0x9F)) { |
| 4081 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4082 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4083 | endinpos = startinpos + 1; |
| 4084 | |
| 4085 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 4086 | continuation byte is s[2], so increment endinpos by 1, |
| 4087 | if not, s[1] is invalid and endinpos doesn't need to |
| 4088 | be incremented. */ |
| 4089 | if ((s[1] & 0xC0) == 0x80) |
| 4090 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4091 | goto utf8Error; |
| 4092 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4093 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4094 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4095 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4096 | break; |
| 4097 | |
| 4098 | case 4: |
| 4099 | if ((s[1] & 0xc0) != 0x80 || |
| 4100 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4101 | (s[3] & 0xc0) != 0x80 || |
| 4102 | ((unsigned char)s[0] == 0xF0 && |
| 4103 | (unsigned char)s[1] < 0x90) || |
| 4104 | ((unsigned char)s[0] == 0xF4 && |
| 4105 | (unsigned char)s[1] > 0x8F)) { |
| 4106 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4107 | startinpos = s-starts; |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4108 | endinpos = startinpos + 1; |
| 4109 | if ((s[1] & 0xC0) == 0x80) { |
| 4110 | endinpos++; |
| 4111 | if ((s[2] & 0xC0) == 0x80) |
| 4112 | endinpos++; |
| 4113 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4114 | goto utf8Error; |
| 4115 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4116 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 57221d0 | 2010-07-01 07:32:02 +0000 | [diff] [blame] | 4117 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4118 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4119 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4120 | /* If the string is flexible or we have native UCS-4, write |
| 4121 | directly.. */ |
| 4122 | if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND) |
| 4123 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4124 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4125 | else { |
| 4126 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4127 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4128 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4129 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4130 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4131 | /* high surrogate = top 10 bits added to D800 */ |
| 4132 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4133 | (Py_UNICODE)(0xD800 + (ch >> 10))); |
| 4134 | |
| 4135 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4136 | WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, |
| 4137 | (Py_UNICODE)(0xDC00 + (ch & 0x03FF))); |
| 4138 | } |
| 4139 | #if SIZEOF_WCHAR_T == 2 |
| 4140 | wchar_offset++; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 4141 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4142 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4143 | } |
| 4144 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4145 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4146 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4147 | utf8Error: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4148 | /* If this is not yet a resizable string, make it one.. */ |
| 4149 | if (kind != PyUnicode_WCHAR_KIND) { |
| 4150 | const Py_UNICODE *u; |
| 4151 | PyUnicodeObject *new_unicode = _PyUnicode_New(size); |
| 4152 | if (!new_unicode) |
| 4153 | goto onError; |
| 4154 | u = PyUnicode_AsUnicode((PyObject *)unicode); |
| 4155 | if (!u) |
| 4156 | goto onError; |
| 4157 | #if SIZEOF_WCHAR_T == 2 |
| 4158 | i += wchar_offset; |
| 4159 | #endif |
| 4160 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i); |
| 4161 | Py_DECREF(unicode); |
| 4162 | unicode = new_unicode; |
| 4163 | kind = 0; |
| 4164 | data = PyUnicode_AS_UNICODE(new_unicode); |
| 4165 | assert(data != NULL); |
| 4166 | } |
| 4167 | error_outptr = PyUnicode_AS_UNICODE(unicode) + i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4168 | if (unicode_decode_call_errorhandler( |
| 4169 | errors, &errorHandler, |
| 4170 | "utf8", errmsg, |
| 4171 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4172 | &unicode, &i, &error_outptr)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4173 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4174 | /* Update data because unicode_decode_call_errorhandler might have |
| 4175 | re-created or resized the unicode object. */ |
| 4176 | data = PyUnicode_AS_UNICODE(unicode); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4177 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4178 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4179 | /* Ensure the unicode_size calculation above was correct: */ |
| 4180 | assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size); |
| 4181 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4182 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4183 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4184 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4185 | /* Adjust length and ready string when it contained errors and |
| 4186 | is of the old resizable kind. */ |
| 4187 | if (kind == PyUnicode_WCHAR_KIND) { |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4188 | if (PyUnicode_Resize((PyObject**)&unicode, i) < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4189 | goto onError; |
| 4190 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4191 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4192 | Py_XDECREF(errorHandler); |
| 4193 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4194 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4195 | Py_DECREF(unicode); |
| 4196 | return NULL; |
| 4197 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4198 | return (PyObject *)unicode; |
| 4199 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4200 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4201 | Py_XDECREF(errorHandler); |
| 4202 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4203 | Py_DECREF(unicode); |
| 4204 | return NULL; |
| 4205 | } |
| 4206 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4207 | #undef WRITE_FLEXIBLE_OR_WSTR |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4208 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4209 | #ifdef __APPLE__ |
| 4210 | |
| 4211 | /* Simplified UTF-8 decoder using surrogateescape error handler, |
| 4212 | used to decode the command line arguments on Mac OS X. */ |
| 4213 | |
| 4214 | wchar_t* |
| 4215 | _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size) |
| 4216 | { |
| 4217 | int n; |
| 4218 | const char *e; |
| 4219 | wchar_t *unicode, *p; |
| 4220 | |
| 4221 | /* Note: size will always be longer than the resulting Unicode |
| 4222 | character count */ |
| 4223 | if (PY_SSIZE_T_MAX / sizeof(wchar_t) < (size + 1)) { |
| 4224 | PyErr_NoMemory(); |
| 4225 | return NULL; |
| 4226 | } |
| 4227 | unicode = PyMem_Malloc((size + 1) * sizeof(wchar_t)); |
| 4228 | if (!unicode) |
| 4229 | return NULL; |
| 4230 | |
| 4231 | /* Unpack UTF-8 encoded data */ |
| 4232 | p = unicode; |
| 4233 | e = s + size; |
| 4234 | while (s < e) { |
| 4235 | Py_UCS4 ch = (unsigned char)*s; |
| 4236 | |
| 4237 | if (ch < 0x80) { |
| 4238 | *p++ = (wchar_t)ch; |
| 4239 | s++; |
| 4240 | continue; |
| 4241 | } |
| 4242 | |
| 4243 | n = utf8_code_length[ch]; |
| 4244 | if (s + n > e) { |
| 4245 | goto surrogateescape; |
| 4246 | } |
| 4247 | |
| 4248 | switch (n) { |
| 4249 | case 0: |
| 4250 | case 1: |
| 4251 | goto surrogateescape; |
| 4252 | |
| 4253 | case 2: |
| 4254 | if ((s[1] & 0xc0) != 0x80) |
| 4255 | goto surrogateescape; |
| 4256 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
| 4257 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 4258 | *p++ = (wchar_t)ch; |
| 4259 | break; |
| 4260 | |
| 4261 | case 3: |
| 4262 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 4263 | will result in surrogates in range d800-dfff. Surrogates are |
| 4264 | not valid UTF-8 so they are rejected. |
| 4265 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 4266 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
| 4267 | if ((s[1] & 0xc0) != 0x80 || |
| 4268 | (s[2] & 0xc0) != 0x80 || |
| 4269 | ((unsigned char)s[0] == 0xE0 && |
| 4270 | (unsigned char)s[1] < 0xA0) || |
| 4271 | ((unsigned char)s[0] == 0xED && |
| 4272 | (unsigned char)s[1] > 0x9F)) { |
| 4273 | |
| 4274 | goto surrogateescape; |
| 4275 | } |
| 4276 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
| 4277 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4278 | *p++ = (wchar_t)ch; |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 4279 | break; |
| 4280 | |
| 4281 | case 4: |
| 4282 | if ((s[1] & 0xc0) != 0x80 || |
| 4283 | (s[2] & 0xc0) != 0x80 || |
| 4284 | (s[3] & 0xc0) != 0x80 || |
| 4285 | ((unsigned char)s[0] == 0xF0 && |
| 4286 | (unsigned char)s[1] < 0x90) || |
| 4287 | ((unsigned char)s[0] == 0xF4 && |
| 4288 | (unsigned char)s[1] > 0x8F)) { |
| 4289 | goto surrogateescape; |
| 4290 | } |
| 4291 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 4292 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 4293 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 4294 | |
| 4295 | #if SIZEOF_WCHAR_T == 4 |
| 4296 | *p++ = (wchar_t)ch; |
| 4297 | #else |
| 4298 | /* compute and append the two surrogates: */ |
| 4299 | |
| 4300 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 4301 | ch -= 0x10000; |
| 4302 | |
| 4303 | /* high surrogate = top 10 bits added to D800 */ |
| 4304 | *p++ = (wchar_t)(0xD800 + (ch >> 10)); |
| 4305 | |
| 4306 | /* low surrogate = bottom 10 bits added to DC00 */ |
| 4307 | *p++ = (wchar_t)(0xDC00 + (ch & 0x03FF)); |
| 4308 | #endif |
| 4309 | break; |
| 4310 | } |
| 4311 | s += n; |
| 4312 | continue; |
| 4313 | |
| 4314 | surrogateescape: |
| 4315 | *p++ = 0xDC00 + ch; |
| 4316 | s++; |
| 4317 | } |
| 4318 | *p = L'\0'; |
| 4319 | return unicode; |
| 4320 | } |
| 4321 | |
| 4322 | #endif /* __APPLE__ */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4323 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4324 | /* Primary internal function which creates utf8 encoded bytes objects. |
| 4325 | |
| 4326 | Allocation strategy: if the string is short, convert into a stack buffer |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4327 | and allocate exactly as much space needed at the end. Else allocate the |
| 4328 | maximum possible needed (4 result bytes per Unicode character), and return |
| 4329 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4330 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 4331 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4332 | _PyUnicode_AsUTF8String(PyObject *obj, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4333 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4334 | #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] | 4335 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4336 | Py_ssize_t i; /* index into s of next input byte */ |
| 4337 | PyObject *result; /* result string object */ |
| 4338 | char *p; /* next free byte in output buffer */ |
| 4339 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 4340 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4341 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4342 | PyObject *errorHandler = NULL; |
| 4343 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4344 | int kind; |
| 4345 | void *data; |
| 4346 | Py_ssize_t size; |
| 4347 | PyUnicodeObject *unicode = (PyUnicodeObject *)obj; |
| 4348 | #if SIZEOF_WCHAR_T == 2 |
| 4349 | Py_ssize_t wchar_offset = 0; |
| 4350 | #endif |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 4351 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4352 | if (!PyUnicode_Check(unicode)) { |
| 4353 | PyErr_BadArgument(); |
| 4354 | return NULL; |
| 4355 | } |
| 4356 | |
| 4357 | if (PyUnicode_READY(unicode) == -1) |
| 4358 | return NULL; |
| 4359 | |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 4360 | if (PyUnicode_UTF8(unicode)) |
| 4361 | return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), |
| 4362 | PyUnicode_UTF8_LENGTH(unicode)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4363 | |
| 4364 | kind = PyUnicode_KIND(unicode); |
| 4365 | data = PyUnicode_DATA(unicode); |
| 4366 | size = PyUnicode_GET_LENGTH(unicode); |
| 4367 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4368 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4369 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4370 | if (size <= MAX_SHORT_UNICHARS) { |
| 4371 | /* Write into the stack buffer; nallocated can't overflow. |
| 4372 | * At the end, we'll allocate exactly as much heap space as it |
| 4373 | * turns out we need. |
| 4374 | */ |
| 4375 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4376 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4377 | p = stackbuf; |
| 4378 | } |
| 4379 | else { |
| 4380 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 4381 | nallocated = size * 4; |
| 4382 | if (nallocated / 4 != size) /* overflow! */ |
| 4383 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4384 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4385 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4386 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4387 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4388 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4389 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4390 | for (i = 0; i < size;) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4391 | Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4392 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4393 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4394 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4395 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 4396 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4397 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4398 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 4399 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 4400 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4401 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4402 | Py_ssize_t newpos; |
| 4403 | PyObject *rep; |
| 4404 | Py_ssize_t repsize, k, startpos; |
| 4405 | startpos = i-1; |
| 4406 | #if SIZEOF_WCHAR_T == 2 |
| 4407 | startpos += wchar_offset; |
Victor Stinner | 445a623 | 2010-04-22 20:01:57 +0000 | [diff] [blame] | 4408 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4409 | rep = unicode_encode_call_errorhandler( |
| 4410 | errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 4411 | PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), |
| 4412 | &exc, startpos, startpos+1, &newpos); |
| 4413 | if (!rep) |
| 4414 | goto error; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4415 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4416 | if (PyBytes_Check(rep)) |
| 4417 | repsize = PyBytes_GET_SIZE(rep); |
| 4418 | else |
| 4419 | repsize = PyUnicode_GET_SIZE(rep); |
| 4420 | |
| 4421 | if (repsize > 4) { |
| 4422 | Py_ssize_t offset; |
| 4423 | |
| 4424 | if (result == NULL) |
| 4425 | offset = p - stackbuf; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4426 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4427 | offset = p - PyBytes_AS_STRING(result); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4428 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4429 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 4430 | /* integer overflow */ |
| 4431 | PyErr_NoMemory(); |
| 4432 | goto error; |
| 4433 | } |
| 4434 | nallocated += repsize - 4; |
| 4435 | if (result != NULL) { |
| 4436 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 4437 | goto error; |
| 4438 | } else { |
| 4439 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4440 | if (result == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4441 | goto error; |
| 4442 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 4443 | } |
| 4444 | p = PyBytes_AS_STRING(result) + offset; |
| 4445 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4446 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4447 | if (PyBytes_Check(rep)) { |
| 4448 | char *prep = PyBytes_AS_STRING(rep); |
| 4449 | for(k = repsize; k > 0; k--) |
| 4450 | *p++ = *prep++; |
| 4451 | } else /* rep is unicode */ { |
| 4452 | const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 4453 | Py_UNICODE c; |
| 4454 | |
| 4455 | for(k=0; k<repsize; k++) { |
| 4456 | c = prep[k]; |
| 4457 | if (0x80 <= c) { |
| 4458 | raise_encode_exception(&exc, "utf-8", |
| 4459 | PyUnicode_AS_UNICODE(unicode), |
| 4460 | size, i-1, i, |
| 4461 | "surrogates not allowed"); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4462 | goto error; |
| 4463 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4464 | *p++ = (char)prep[k]; |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4465 | } |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4466 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4467 | Py_DECREF(rep); |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 4468 | } else if (ch < 0x10000) { |
| 4469 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 4470 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4471 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 4472 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4473 | /* Encode UCS4 Unicode ordinals */ |
| 4474 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 4475 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 4476 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 4477 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4478 | #if SIZEOF_WCHAR_T == 2 |
| 4479 | wchar_offset++; |
| 4480 | #endif |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4481 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4482 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 4483 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4484 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4485 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4486 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4487 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4488 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4489 | } |
| 4490 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4491 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4492 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4493 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4494 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4495 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4496 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4497 | Py_XDECREF(errorHandler); |
| 4498 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4499 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4500 | error: |
| 4501 | Py_XDECREF(errorHandler); |
| 4502 | Py_XDECREF(exc); |
| 4503 | Py_XDECREF(result); |
| 4504 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 4505 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 4506 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4509 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4510 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
| 4511 | Py_ssize_t size, |
| 4512 | const char *errors) |
| 4513 | { |
| 4514 | PyObject *v, *unicode; |
| 4515 | |
| 4516 | unicode = PyUnicode_FromUnicode(s, size); |
| 4517 | if (unicode == NULL) |
| 4518 | return NULL; |
| 4519 | v = _PyUnicode_AsUTF8String(unicode, errors); |
| 4520 | Py_DECREF(unicode); |
| 4521 | return v; |
| 4522 | } |
| 4523 | |
| 4524 | PyObject * |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4525 | PyUnicode_AsUTF8String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4526 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4527 | return _PyUnicode_AsUTF8String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4530 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 4531 | |
| 4532 | PyObject * |
| 4533 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4534 | Py_ssize_t size, |
| 4535 | const char *errors, |
| 4536 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4537 | { |
| 4538 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 4539 | } |
| 4540 | |
| 4541 | PyObject * |
| 4542 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4543 | Py_ssize_t size, |
| 4544 | const char *errors, |
| 4545 | int *byteorder, |
| 4546 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4547 | { |
| 4548 | const char *starts = s; |
| 4549 | Py_ssize_t startinpos; |
| 4550 | Py_ssize_t endinpos; |
| 4551 | Py_ssize_t outpos; |
| 4552 | PyUnicodeObject *unicode; |
| 4553 | Py_UNICODE *p; |
| 4554 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4555 | int pairs = 0; |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4556 | const unsigned char *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4557 | #else |
| 4558 | const int pairs = 0; |
| 4559 | #endif |
Mark Dickinson | 7db923c | 2010-06-12 09:10:14 +0000 | [diff] [blame] | 4560 | const unsigned char *q, *e; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4561 | int bo = 0; /* assume native ordering by default */ |
| 4562 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4563 | /* Offsets from q for retrieving bytes in the right order. */ |
| 4564 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4565 | int iorder[] = {0, 1, 2, 3}; |
| 4566 | #else |
| 4567 | int iorder[] = {3, 2, 1, 0}; |
| 4568 | #endif |
| 4569 | PyObject *errorHandler = NULL; |
| 4570 | PyObject *exc = NULL; |
Victor Stinner | 313a120 | 2010-06-11 23:56:51 +0000 | [diff] [blame] | 4571 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4572 | q = (unsigned char *)s; |
| 4573 | e = q + size; |
| 4574 | |
| 4575 | if (byteorder) |
| 4576 | bo = *byteorder; |
| 4577 | |
| 4578 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4579 | byte order setting accordingly. In native mode, the leading BOM |
| 4580 | mark is skipped, in all other modes, it is copied to the output |
| 4581 | stream as-is (giving a ZWNBSP character). */ |
| 4582 | if (bo == 0) { |
| 4583 | if (size >= 4) { |
| 4584 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4585 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4586 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4587 | if (bom == 0x0000FEFF) { |
| 4588 | q += 4; |
| 4589 | bo = -1; |
| 4590 | } |
| 4591 | else if (bom == 0xFFFE0000) { |
| 4592 | q += 4; |
| 4593 | bo = 1; |
| 4594 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4595 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4596 | if (bom == 0x0000FEFF) { |
| 4597 | q += 4; |
| 4598 | bo = 1; |
| 4599 | } |
| 4600 | else if (bom == 0xFFFE0000) { |
| 4601 | q += 4; |
| 4602 | bo = -1; |
| 4603 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4604 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4605 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | if (bo == -1) { |
| 4609 | /* force LE */ |
| 4610 | iorder[0] = 0; |
| 4611 | iorder[1] = 1; |
| 4612 | iorder[2] = 2; |
| 4613 | iorder[3] = 3; |
| 4614 | } |
| 4615 | else if (bo == 1) { |
| 4616 | /* force BE */ |
| 4617 | iorder[0] = 3; |
| 4618 | iorder[1] = 2; |
| 4619 | iorder[2] = 1; |
| 4620 | iorder[3] = 0; |
| 4621 | } |
| 4622 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4623 | /* On narrow builds we split characters outside the BMP into two |
| 4624 | codepoints => count how much extra space we need. */ |
| 4625 | #ifndef Py_UNICODE_WIDE |
| 4626 | for (qq = q; qq < e; qq += 4) |
| 4627 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 4628 | pairs++; |
| 4629 | #endif |
| 4630 | |
| 4631 | /* This might be one to much, because of a BOM */ |
| 4632 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 4633 | if (!unicode) |
| 4634 | return NULL; |
| 4635 | if (size == 0) |
| 4636 | return (PyObject *)unicode; |
| 4637 | |
| 4638 | /* Unpack UTF-32 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4639 | p = PyUnicode_AS_UNICODE(unicode); |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 4640 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4641 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4642 | Py_UCS4 ch; |
| 4643 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 4644 | if (e-q<4) { |
| 4645 | if (consumed) |
| 4646 | break; |
| 4647 | errmsg = "truncated data"; |
| 4648 | startinpos = ((const char *)q)-starts; |
| 4649 | endinpos = ((const char *)e)-starts; |
| 4650 | goto utf32Error; |
| 4651 | /* The remaining input chars are ignored if the callback |
| 4652 | chooses to skip the input */ |
| 4653 | } |
| 4654 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 4655 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4656 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4657 | if (ch >= 0x110000) |
| 4658 | { |
| 4659 | errmsg = "codepoint not in range(0x110000)"; |
| 4660 | startinpos = ((const char *)q)-starts; |
| 4661 | endinpos = startinpos+4; |
| 4662 | goto utf32Error; |
| 4663 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4664 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4665 | if (ch >= 0x10000) |
| 4666 | { |
| 4667 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 4668 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 4669 | } |
| 4670 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4671 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4672 | *p++ = ch; |
| 4673 | q += 4; |
| 4674 | continue; |
| 4675 | utf32Error: |
| 4676 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 4677 | if (unicode_decode_call_errorhandler( |
| 4678 | errors, &errorHandler, |
| 4679 | "utf32", errmsg, |
| 4680 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 4681 | &unicode, &outpos, &p)) |
| 4682 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | if (byteorder) |
| 4686 | *byteorder = bo; |
| 4687 | |
| 4688 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4689 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4690 | |
| 4691 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 4692 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4693 | goto onError; |
| 4694 | |
| 4695 | Py_XDECREF(errorHandler); |
| 4696 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 4697 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4698 | Py_DECREF(unicode); |
| 4699 | return NULL; |
| 4700 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4701 | return (PyObject *)unicode; |
| 4702 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4703 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4704 | Py_DECREF(unicode); |
| 4705 | Py_XDECREF(errorHandler); |
| 4706 | Py_XDECREF(exc); |
| 4707 | return NULL; |
| 4708 | } |
| 4709 | |
| 4710 | PyObject * |
| 4711 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4712 | Py_ssize_t size, |
| 4713 | const char *errors, |
| 4714 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4715 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4716 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4717 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4718 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4719 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4720 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4721 | #else |
| 4722 | const int pairs = 0; |
| 4723 | #endif |
| 4724 | /* Offsets from p for storing byte pairs in the right order. */ |
| 4725 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4726 | int iorder[] = {0, 1, 2, 3}; |
| 4727 | #else |
| 4728 | int iorder[] = {3, 2, 1, 0}; |
| 4729 | #endif |
| 4730 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4731 | #define STORECHAR(CH) \ |
| 4732 | do { \ |
| 4733 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 4734 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 4735 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 4736 | p[iorder[0]] = (CH) & 0xff; \ |
| 4737 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4738 | } while(0) |
| 4739 | |
| 4740 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 4741 | so we need less space. */ |
| 4742 | #ifndef Py_UNICODE_WIDE |
| 4743 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4744 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 4745 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 4746 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4747 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 4748 | nsize = (size - pairs + (byteorder == 0)); |
| 4749 | bytesize = nsize * 4; |
| 4750 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4751 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4752 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4753 | if (v == NULL) |
| 4754 | return NULL; |
| 4755 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4756 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4757 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4758 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4759 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4760 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4761 | |
| 4762 | if (byteorder == -1) { |
| 4763 | /* force LE */ |
| 4764 | iorder[0] = 0; |
| 4765 | iorder[1] = 1; |
| 4766 | iorder[2] = 2; |
| 4767 | iorder[3] = 3; |
| 4768 | } |
| 4769 | else if (byteorder == 1) { |
| 4770 | /* force BE */ |
| 4771 | iorder[0] = 3; |
| 4772 | iorder[1] = 2; |
| 4773 | iorder[2] = 1; |
| 4774 | iorder[3] = 0; |
| 4775 | } |
| 4776 | |
| 4777 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4778 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4779 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4780 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 4781 | Py_UCS4 ch2 = *s; |
| 4782 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 4783 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 4784 | s++; |
| 4785 | size--; |
| 4786 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4787 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4788 | #endif |
| 4789 | STORECHAR(ch); |
| 4790 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4791 | |
| 4792 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4793 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4794 | #undef STORECHAR |
| 4795 | } |
| 4796 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 4797 | PyObject * |
| 4798 | PyUnicode_AsUTF32String(PyObject *unicode) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4799 | { |
| 4800 | if (!PyUnicode_Check(unicode)) { |
| 4801 | PyErr_BadArgument(); |
| 4802 | return NULL; |
| 4803 | } |
| 4804 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4805 | PyUnicode_GET_SIZE(unicode), |
| 4806 | NULL, |
| 4807 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 4808 | } |
| 4809 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4810 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 4811 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4812 | PyObject * |
| 4813 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4814 | Py_ssize_t size, |
| 4815 | const char *errors, |
| 4816 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4817 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4818 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 4819 | } |
| 4820 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4821 | /* Two masks for fast checking of whether a C 'long' may contain |
| 4822 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 4823 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 4824 | rare in most input. |
| 4825 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 4826 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4827 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4828 | #if (SIZEOF_LONG == 8) |
| 4829 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 4830 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 4831 | #elif (SIZEOF_LONG == 4) |
| 4832 | # define FAST_CHAR_MASK 0x80008000L |
| 4833 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 4834 | #else |
| 4835 | # error C 'long' size should be either 4 or 8! |
| 4836 | #endif |
| 4837 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4838 | PyObject * |
| 4839 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4840 | Py_ssize_t size, |
| 4841 | const char *errors, |
| 4842 | int *byteorder, |
| 4843 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4844 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4845 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4846 | Py_ssize_t startinpos; |
| 4847 | Py_ssize_t endinpos; |
| 4848 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4849 | PyUnicodeObject *unicode; |
| 4850 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4851 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4852 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4853 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 4854 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4855 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 4856 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4857 | int ihi = 1, ilo = 0; |
| 4858 | #else |
| 4859 | int ihi = 0, ilo = 1; |
| 4860 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4861 | PyObject *errorHandler = NULL; |
| 4862 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4863 | |
| 4864 | /* Note: size will always be longer than the resulting Unicode |
| 4865 | character count */ |
| 4866 | unicode = _PyUnicode_New(size); |
| 4867 | if (!unicode) |
| 4868 | return NULL; |
| 4869 | if (size == 0) |
| 4870 | return (PyObject *)unicode; |
| 4871 | |
| 4872 | /* Unpack UTF-16 encoded data */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 4873 | p = PyUnicode_AS_UNICODE(unicode); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4874 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4875 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4876 | |
| 4877 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4878 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4879 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4880 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 4881 | byte order setting accordingly. In native mode, the leading BOM |
| 4882 | mark is skipped, in all other modes, it is copied to the output |
| 4883 | stream as-is (giving a ZWNBSP character). */ |
| 4884 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 4885 | if (size >= 2) { |
| 4886 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4887 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4888 | if (bom == 0xFEFF) { |
| 4889 | q += 2; |
| 4890 | bo = -1; |
| 4891 | } |
| 4892 | else if (bom == 0xFFFE) { |
| 4893 | q += 2; |
| 4894 | bo = 1; |
| 4895 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4896 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4897 | if (bom == 0xFEFF) { |
| 4898 | q += 2; |
| 4899 | bo = 1; |
| 4900 | } |
| 4901 | else if (bom == 0xFFFE) { |
| 4902 | q += 2; |
| 4903 | bo = -1; |
| 4904 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4905 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4906 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 4907 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4908 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4909 | if (bo == -1) { |
| 4910 | /* force LE */ |
| 4911 | ihi = 1; |
| 4912 | ilo = 0; |
| 4913 | } |
| 4914 | else if (bo == 1) { |
| 4915 | /* force BE */ |
| 4916 | ihi = 0; |
| 4917 | ilo = 1; |
| 4918 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4919 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 4920 | native_ordering = ilo < ihi; |
| 4921 | #else |
| 4922 | native_ordering = ilo > ihi; |
| 4923 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4924 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4925 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 4926 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4927 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4928 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 4929 | reads are more expensive, better to defer to another iteration. */ |
| 4930 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 4931 | /* Fast path for runs of non-surrogate chars. */ |
| 4932 | register const unsigned char *_q = q; |
| 4933 | Py_UNICODE *_p = p; |
| 4934 | if (native_ordering) { |
| 4935 | /* Native ordering is simple: as long as the input cannot |
| 4936 | possibly contain a surrogate char, do an unrolled copy |
| 4937 | of several 16-bit code points to the target object. |
| 4938 | The non-surrogate check is done on several input bytes |
| 4939 | at a time (as many as a C 'long' can contain). */ |
| 4940 | while (_q < aligned_end) { |
| 4941 | unsigned long data = * (unsigned long *) _q; |
| 4942 | if (data & FAST_CHAR_MASK) |
| 4943 | break; |
| 4944 | _p[0] = ((unsigned short *) _q)[0]; |
| 4945 | _p[1] = ((unsigned short *) _q)[1]; |
| 4946 | #if (SIZEOF_LONG == 8) |
| 4947 | _p[2] = ((unsigned short *) _q)[2]; |
| 4948 | _p[3] = ((unsigned short *) _q)[3]; |
| 4949 | #endif |
| 4950 | _q += SIZEOF_LONG; |
| 4951 | _p += SIZEOF_LONG / 2; |
| 4952 | } |
| 4953 | } |
| 4954 | else { |
| 4955 | /* Byteswapped ordering is similar, but we must decompose |
| 4956 | the copy bytewise, and take care of zero'ing out the |
| 4957 | upper bytes if the target object is in 32-bit units |
| 4958 | (that is, in UCS-4 builds). */ |
| 4959 | while (_q < aligned_end) { |
| 4960 | unsigned long data = * (unsigned long *) _q; |
| 4961 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 4962 | break; |
| 4963 | /* Zero upper bytes in UCS-4 builds */ |
| 4964 | #if (Py_UNICODE_SIZE > 2) |
| 4965 | _p[0] = 0; |
| 4966 | _p[1] = 0; |
| 4967 | #if (SIZEOF_LONG == 8) |
| 4968 | _p[2] = 0; |
| 4969 | _p[3] = 0; |
| 4970 | #endif |
| 4971 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4972 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 4973 | fill the two last bytes of each 4-byte unit. */ |
| 4974 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 4975 | # define OFF 2 |
| 4976 | #else |
| 4977 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4978 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 4979 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 4980 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 4981 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 4982 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 4983 | #if (SIZEOF_LONG == 8) |
| 4984 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 4985 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 4986 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 4987 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 4988 | #endif |
| 4989 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 4990 | _q += SIZEOF_LONG; |
| 4991 | _p += SIZEOF_LONG / 2; |
| 4992 | } |
| 4993 | } |
| 4994 | p = _p; |
| 4995 | q = _q; |
| 4996 | if (q >= e) |
| 4997 | break; |
| 4998 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4999 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5000 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5001 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5002 | |
| 5003 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 5004 | *p++ = ch; |
| 5005 | continue; |
| 5006 | } |
| 5007 | |
| 5008 | /* UTF-16 code pair: */ |
| 5009 | if (q > e) { |
| 5010 | errmsg = "unexpected end of data"; |
| 5011 | startinpos = (((const char *)q) - 2) - starts; |
| 5012 | endinpos = ((const char *)e) + 1 - starts; |
| 5013 | goto utf16Error; |
| 5014 | } |
| 5015 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 5016 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 5017 | q += 2; |
| 5018 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5019 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5020 | *p++ = ch; |
| 5021 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5022 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5023 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5024 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5025 | continue; |
| 5026 | } |
| 5027 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5028 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5029 | startinpos = (((const char *)q)-4)-starts; |
| 5030 | endinpos = startinpos+2; |
| 5031 | goto utf16Error; |
| 5032 | } |
| 5033 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5034 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5035 | errmsg = "illegal encoding"; |
| 5036 | startinpos = (((const char *)q)-2)-starts; |
| 5037 | endinpos = startinpos+2; |
| 5038 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5039 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5040 | utf16Error: |
| 5041 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5042 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5043 | errors, |
| 5044 | &errorHandler, |
| 5045 | "utf16", errmsg, |
| 5046 | &starts, |
| 5047 | (const char **)&e, |
| 5048 | &startinpos, |
| 5049 | &endinpos, |
| 5050 | &exc, |
| 5051 | (const char **)&q, |
| 5052 | &unicode, |
| 5053 | &outpos, |
| 5054 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5055 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5056 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5057 | /* remaining byte at the end? (size should be even) */ |
| 5058 | if (e == q) { |
| 5059 | if (!consumed) { |
| 5060 | errmsg = "truncated data"; |
| 5061 | startinpos = ((const char *)q) - starts; |
| 5062 | endinpos = ((const char *)e) + 1 - starts; |
| 5063 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 5064 | if (unicode_decode_call_errorhandler( |
| 5065 | errors, |
| 5066 | &errorHandler, |
| 5067 | "utf16", errmsg, |
| 5068 | &starts, |
| 5069 | (const char **)&e, |
| 5070 | &startinpos, |
| 5071 | &endinpos, |
| 5072 | &exc, |
| 5073 | (const char **)&q, |
| 5074 | &unicode, |
| 5075 | &outpos, |
| 5076 | &p)) |
| 5077 | goto onError; |
| 5078 | /* The remaining input chars are ignored if the callback |
| 5079 | chooses to skip the input */ |
| 5080 | } |
| 5081 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5082 | |
| 5083 | if (byteorder) |
| 5084 | *byteorder = bo; |
| 5085 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5086 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5087 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 5088 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5089 | /* Adjust length */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5090 | if (PyUnicode_Resize((PyObject**)&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5091 | goto onError; |
| 5092 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5093 | Py_XDECREF(errorHandler); |
| 5094 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5095 | if (_PyUnicode_READY_REPLACE(&unicode)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5096 | Py_DECREF(unicode); |
| 5097 | return NULL; |
| 5098 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5099 | return (PyObject *)unicode; |
| 5100 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5101 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5102 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5103 | Py_XDECREF(errorHandler); |
| 5104 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5105 | return NULL; |
| 5106 | } |
| 5107 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 5108 | #undef FAST_CHAR_MASK |
| 5109 | #undef SWAPPED_FAST_CHAR_MASK |
| 5110 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5111 | PyObject * |
| 5112 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5113 | Py_ssize_t size, |
| 5114 | const char *errors, |
| 5115 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5116 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5117 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5118 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5119 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5120 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5121 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5122 | #else |
| 5123 | const int pairs = 0; |
| 5124 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5125 | /* Offsets from p for storing byte pairs in the right order. */ |
| 5126 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 5127 | int ihi = 1, ilo = 0; |
| 5128 | #else |
| 5129 | int ihi = 0, ilo = 1; |
| 5130 | #endif |
| 5131 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5132 | #define STORECHAR(CH) \ |
| 5133 | do { \ |
| 5134 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 5135 | p[ilo] = (CH) & 0xff; \ |
| 5136 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5137 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5138 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5139 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5140 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5141 | if (s[i] >= 0x10000) |
| 5142 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5143 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5144 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 5145 | if (size > PY_SSIZE_T_MAX || |
| 5146 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5147 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5148 | nsize = size + pairs + (byteorder == 0); |
| 5149 | bytesize = nsize * 2; |
| 5150 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5151 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5152 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5153 | if (v == NULL) |
| 5154 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5155 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5156 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5157 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5158 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5159 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5160 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5161 | |
| 5162 | if (byteorder == -1) { |
| 5163 | /* force LE */ |
| 5164 | ihi = 1; |
| 5165 | ilo = 0; |
| 5166 | } |
| 5167 | else if (byteorder == 1) { |
| 5168 | /* force BE */ |
| 5169 | ihi = 0; |
| 5170 | ilo = 1; |
| 5171 | } |
| 5172 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5173 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5174 | Py_UNICODE ch = *s++; |
| 5175 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5176 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5177 | if (ch >= 0x10000) { |
| 5178 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 5179 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 5180 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 5181 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5182 | STORECHAR(ch); |
| 5183 | if (ch2) |
| 5184 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5185 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5186 | |
| 5187 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5188 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 5189 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5190 | } |
| 5191 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5192 | PyObject * |
| 5193 | PyUnicode_AsUTF16String(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5194 | { |
| 5195 | if (!PyUnicode_Check(unicode)) { |
| 5196 | PyErr_BadArgument(); |
| 5197 | return NULL; |
| 5198 | } |
| 5199 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5200 | PyUnicode_GET_SIZE(unicode), |
| 5201 | NULL, |
| 5202 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 5206 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5207 | /* Helper function for PyUnicode_DecodeUnicodeEscape, determines |
| 5208 | if all the escapes in the string make it still a valid ASCII string. |
| 5209 | Returns -1 if any escapes were found which cause the string to |
| 5210 | pop out of ASCII range. Otherwise returns the length of the |
| 5211 | required buffer to hold the string. |
| 5212 | */ |
| 5213 | Py_ssize_t |
| 5214 | length_of_escaped_ascii_string(const char *s, Py_ssize_t size) |
| 5215 | { |
| 5216 | const unsigned char *p = (const unsigned char *)s; |
| 5217 | const unsigned char *end = p + size; |
| 5218 | Py_ssize_t length = 0; |
| 5219 | |
| 5220 | if (size < 0) |
| 5221 | return -1; |
| 5222 | |
| 5223 | for (; p < end; ++p) { |
| 5224 | if (*p > 127) { |
| 5225 | /* Non-ASCII */ |
| 5226 | return -1; |
| 5227 | } |
| 5228 | else if (*p != '\\') { |
| 5229 | /* Normal character */ |
| 5230 | ++length; |
| 5231 | } |
| 5232 | else { |
| 5233 | /* Backslash-escape, check next char */ |
| 5234 | ++p; |
| 5235 | /* Escape sequence reaches till end of string or |
| 5236 | non-ASCII follow-up. */ |
| 5237 | if (p >= end || *p > 127) |
| 5238 | return -1; |
| 5239 | switch (*p) { |
| 5240 | case '\n': |
| 5241 | /* backslash + \n result in zero characters */ |
| 5242 | break; |
| 5243 | case '\\': case '\'': case '\"': |
| 5244 | case 'b': case 'f': case 't': |
| 5245 | case 'n': case 'r': case 'v': case 'a': |
| 5246 | ++length; |
| 5247 | break; |
| 5248 | case '0': case '1': case '2': case '3': |
| 5249 | case '4': case '5': case '6': case '7': |
| 5250 | case 'x': case 'u': case 'U': case 'N': |
| 5251 | /* these do not guarantee ASCII characters */ |
| 5252 | return -1; |
| 5253 | default: |
| 5254 | /* count the backslash + the other character */ |
| 5255 | length += 2; |
| 5256 | } |
| 5257 | } |
| 5258 | } |
| 5259 | return length; |
| 5260 | } |
| 5261 | |
| 5262 | /* Similar to PyUnicode_WRITE but either write into wstr field |
| 5263 | or treat string as ASCII. */ |
| 5264 | #define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \ |
| 5265 | do { \ |
| 5266 | if ((kind) != PyUnicode_WCHAR_KIND) \ |
| 5267 | ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \ |
| 5268 | else \ |
| 5269 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \ |
| 5270 | } while (0) |
| 5271 | |
| 5272 | #define WRITE_WSTR(buf, index, value) \ |
| 5273 | assert(kind == PyUnicode_WCHAR_KIND), \ |
| 5274 | ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value) |
| 5275 | |
| 5276 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5277 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 5278 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5279 | PyObject * |
| 5280 | PyUnicode_DecodeUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5281 | Py_ssize_t size, |
Victor Stinner | c17f540 | 2011-09-29 00:16:58 +0200 | [diff] [blame] | 5282 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5283 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5284 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5285 | Py_ssize_t startinpos; |
| 5286 | Py_ssize_t endinpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5287 | int j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5288 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5289 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5290 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5291 | char* message; |
| 5292 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5293 | PyObject *errorHandler = NULL; |
| 5294 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5295 | Py_ssize_t ascii_length; |
| 5296 | Py_ssize_t i; |
| 5297 | int kind; |
| 5298 | void *data; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5299 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5300 | ascii_length = length_of_escaped_ascii_string(s, size); |
| 5301 | |
| 5302 | /* After length_of_escaped_ascii_string() there are two alternatives, |
| 5303 | either the string is pure ASCII with named escapes like \n, etc. |
| 5304 | and we determined it's exact size (common case) |
| 5305 | or it contains \x, \u, ... escape sequences. then we create a |
| 5306 | legacy wchar string and resize it at the end of this function. */ |
| 5307 | if (ascii_length >= 0) { |
| 5308 | v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127); |
| 5309 | if (!v) |
| 5310 | goto onError; |
| 5311 | assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); |
| 5312 | kind = PyUnicode_1BYTE_KIND; |
| 5313 | data = PyUnicode_DATA(v); |
| 5314 | } |
| 5315 | else { |
| 5316 | /* Escaped strings will always be longer than the resulting |
| 5317 | Unicode string, so we start with size here and then reduce the |
| 5318 | length after conversion to the true value. |
| 5319 | (but if the error callback returns a long replacement string |
| 5320 | we'll have to allocate more space) */ |
| 5321 | v = _PyUnicode_New(size); |
| 5322 | if (!v) |
| 5323 | goto onError; |
| 5324 | kind = PyUnicode_WCHAR_KIND; |
| 5325 | data = PyUnicode_AS_UNICODE(v); |
| 5326 | } |
| 5327 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5328 | if (size == 0) |
| 5329 | return (PyObject *)v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5330 | i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5332 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5333 | while (s < end) { |
| 5334 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 5335 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5336 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5337 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5338 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5339 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5340 | } |
| 5341 | else { |
| 5342 | /* The only case in which i == ascii_length is a backslash |
| 5343 | followed by a newline. */ |
| 5344 | assert(i <= ascii_length); |
| 5345 | } |
| 5346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5347 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5348 | if (*s != '\\') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5349 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5350 | continue; |
| 5351 | } |
| 5352 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5353 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5354 | /* \ - Escapes */ |
| 5355 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5356 | c = *s++; |
| 5357 | if (s > end) |
| 5358 | c = '\0'; /* Invalid after \ */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5359 | |
| 5360 | if (kind == PyUnicode_WCHAR_KIND) { |
| 5361 | assert(i < _PyUnicode_WSTR_LENGTH(v)); |
| 5362 | } |
| 5363 | else { |
| 5364 | /* The only case in which i == ascii_length is a backslash |
| 5365 | followed by a newline. */ |
| 5366 | assert(i < ascii_length || (i == ascii_length && c == '\n')); |
| 5367 | } |
| 5368 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5369 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5370 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5371 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5372 | case '\n': break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5373 | case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break; |
| 5374 | case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break; |
| 5375 | case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break; |
| 5376 | case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break; |
| 5377 | /* FF */ |
| 5378 | case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break; |
| 5379 | case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break; |
| 5380 | case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break; |
| 5381 | case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break; |
| 5382 | /* VT */ |
| 5383 | case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break; |
| 5384 | /* BEL, not classic C */ |
| 5385 | case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5387 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5388 | case '0': case '1': case '2': case '3': |
| 5389 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5390 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5391 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5392 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5393 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 5394 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5395 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5396 | WRITE_WSTR(data, i++, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5397 | break; |
| 5398 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5399 | /* hex escapes */ |
| 5400 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5401 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5402 | digits = 2; |
| 5403 | message = "truncated \\xXX escape"; |
| 5404 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5405 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5406 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5407 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5408 | digits = 4; |
| 5409 | message = "truncated \\uXXXX escape"; |
| 5410 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5411 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5412 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5413 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5414 | digits = 8; |
| 5415 | message = "truncated \\UXXXXXXXX escape"; |
| 5416 | hexescape: |
| 5417 | chr = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5418 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5419 | if (s+digits>end) { |
| 5420 | endinpos = size; |
| 5421 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5422 | errors, &errorHandler, |
| 5423 | "unicodeescape", "end of string in escape sequence", |
| 5424 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5425 | &v, &i, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5426 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5427 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5428 | goto nextByte; |
| 5429 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5430 | for (j = 0; j < digits; ++j) { |
| 5431 | c = (unsigned char) s[j]; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5432 | if (!Py_ISXDIGIT(c)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5433 | endinpos = (s+j+1)-starts; |
| 5434 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5435 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5436 | errors, &errorHandler, |
| 5437 | "unicodeescape", message, |
| 5438 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5439 | &v, &i, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5440 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5441 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5442 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5443 | } |
| 5444 | chr = (chr<<4) & ~0xF; |
| 5445 | if (c >= '0' && c <= '9') |
| 5446 | chr += c - '0'; |
| 5447 | else if (c >= 'a' && c <= 'f') |
| 5448 | chr += 10 + c - 'a'; |
| 5449 | else |
| 5450 | chr += 10 + c - 'A'; |
| 5451 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5452 | s += j; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 5453 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5454 | /* _decoding_error will have already written into the |
| 5455 | target buffer. */ |
| 5456 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5457 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5458 | /* when we get here, chr is a 32-bit unicode character */ |
| 5459 | if (chr <= 0xffff) |
| 5460 | /* UCS-2 character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5461 | WRITE_WSTR(data, i++, chr); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5462 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5463 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5464 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 5465 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5466 | WRITE_WSTR(data, i++, chr); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5467 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5468 | chr -= 0x10000L; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5469 | WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10)); |
| 5470 | WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5471 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5472 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5473 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 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", "illegal Unicode character", |
| 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); |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 5482 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5483 | break; |
| 5484 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5485 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5486 | case 'N': |
| 5487 | message = "malformed \\N character escape"; |
| 5488 | if (ucnhash_CAPI == NULL) { |
| 5489 | /* load the unicode data module */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5490 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import( |
| 5491 | PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5492 | if (ucnhash_CAPI == NULL) |
| 5493 | goto ucnhashError; |
| 5494 | } |
| 5495 | if (*s == '{') { |
| 5496 | const char *start = s+1; |
| 5497 | /* look for the closing brace */ |
| 5498 | while (*s != '}' && s < end) |
| 5499 | s++; |
| 5500 | if (s > start && s < end && *s == '}') { |
| 5501 | /* found a name. look it up in the unicode database */ |
| 5502 | message = "unknown Unicode character name"; |
| 5503 | s++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5504 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), |
| 5505 | &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5506 | goto store; |
| 5507 | } |
| 5508 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5509 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5510 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5511 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5512 | errors, &errorHandler, |
| 5513 | "unicodeescape", message, |
| 5514 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5515 | &v, &i, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5516 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5517 | data = PyUnicode_AS_UNICODE(v); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5518 | break; |
| 5519 | |
| 5520 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5521 | if (s > end) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5522 | assert(kind == PyUnicode_WCHAR_KIND); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5523 | message = "\\ at end of string"; |
| 5524 | s--; |
| 5525 | endinpos = s-starts; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5526 | p = PyUnicode_AS_UNICODE(v) + i; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5527 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5528 | errors, &errorHandler, |
| 5529 | "unicodeescape", message, |
| 5530 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5531 | &v, &i, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5532 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5533 | data = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5534 | } |
| 5535 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5536 | WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); |
| 5537 | WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]); |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5538 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 5539 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5540 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5541 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5542 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5543 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5544 | /* Ensure the length prediction worked in case of ASCII strings */ |
| 5545 | assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length); |
| 5546 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5547 | if (kind == PyUnicode_WCHAR_KIND) |
| 5548 | { |
| 5549 | if (PyUnicode_Resize((PyObject**)&v, i) < 0) |
| 5550 | goto onError; |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5551 | } |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 5552 | Py_XDECREF(errorHandler); |
| 5553 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5554 | if (_PyUnicode_READY_REPLACE(&v)) { |
| 5555 | Py_DECREF(v); |
| 5556 | return NULL; |
| 5557 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5558 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 5559 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5560 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 5561 | PyErr_SetString( |
| 5562 | PyExc_UnicodeError, |
| 5563 | "\\N escapes not supported (can't load unicodedata module)" |
| 5564 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5565 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5566 | Py_XDECREF(errorHandler); |
| 5567 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 5568 | return NULL; |
| 5569 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5570 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5571 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5572 | Py_XDECREF(errorHandler); |
| 5573 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | return NULL; |
| 5575 | } |
| 5576 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5577 | #undef WRITE_ASCII_OR_WSTR |
| 5578 | #undef WRITE_WSTR |
| 5579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5580 | /* Return a Unicode-Escape string version of the Unicode object. |
| 5581 | |
| 5582 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 5583 | appropriate. |
| 5584 | |
| 5585 | */ |
| 5586 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5587 | static const char *hexdigits = "0123456789abcdef"; |
| 5588 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5589 | PyObject * |
| 5590 | PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5591 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5593 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5594 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5595 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5596 | #ifdef Py_UNICODE_WIDE |
| 5597 | const Py_ssize_t expandsize = 10; |
| 5598 | #else |
| 5599 | const Py_ssize_t expandsize = 6; |
| 5600 | #endif |
| 5601 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5602 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 5603 | better to choose a different scheme. Perhaps scan the |
| 5604 | first N-chars of the string and allocate based on that size. |
| 5605 | */ |
| 5606 | /* Initial allocation is based on the longest-possible unichr |
| 5607 | escape. |
| 5608 | |
| 5609 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 5610 | unichr, so in this case it's the longest unichr escape. In |
| 5611 | narrow (UTF-16) builds this is five chars per source unichr |
| 5612 | since there are two unichrs in the surrogate pair, so in narrow |
| 5613 | (UTF-16) builds it's not the longest unichr escape. |
| 5614 | |
| 5615 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 5616 | so in the narrow (UTF-16) build case it's the longest unichr |
| 5617 | escape. |
| 5618 | */ |
| 5619 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5620 | if (size == 0) |
| 5621 | return PyBytes_FromStringAndSize(NULL, 0); |
| 5622 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5623 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5624 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5625 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5626 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5627 | 2 |
| 5628 | + expandsize*size |
| 5629 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5630 | if (repr == NULL) |
| 5631 | return NULL; |
| 5632 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5633 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5634 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5635 | while (size-- > 0) { |
| 5636 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5637 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5638 | /* Escape backslashes */ |
| 5639 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5640 | *p++ = '\\'; |
| 5641 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5642 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5643 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5644 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 5645 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5646 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 5647 | else if (ch >= 0x10000) { |
| 5648 | *p++ = '\\'; |
| 5649 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5650 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 5651 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 5652 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 5653 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 5654 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 5655 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 5656 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 5657 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5658 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 5659 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5660 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5661 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5662 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 5663 | Py_UNICODE ch2; |
| 5664 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5665 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5666 | ch2 = *s++; |
| 5667 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5668 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5669 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5670 | *p++ = '\\'; |
| 5671 | *p++ = 'U'; |
| 5672 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 5673 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 5674 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 5675 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 5676 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 5677 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 5678 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 5679 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 5680 | continue; |
| 5681 | } |
| 5682 | /* Fall through: isolated surrogates are copied as-is */ |
| 5683 | s--; |
| 5684 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5685 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5686 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5688 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 5689 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | *p++ = '\\'; |
| 5691 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5692 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 5693 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 5694 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5695 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5696 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5697 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5698 | /* Map special whitespace to '\t', \n', '\r' */ |
| 5699 | else if (ch == '\t') { |
| 5700 | *p++ = '\\'; |
| 5701 | *p++ = 't'; |
| 5702 | } |
| 5703 | else if (ch == '\n') { |
| 5704 | *p++ = '\\'; |
| 5705 | *p++ = 'n'; |
| 5706 | } |
| 5707 | else if (ch == '\r') { |
| 5708 | *p++ = '\\'; |
| 5709 | *p++ = 'r'; |
| 5710 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5711 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5712 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 5713 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5714 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 5715 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5716 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 5717 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5718 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 5719 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5720 | /* Copy everything else as-is */ |
| 5721 | else |
| 5722 | *p++ = (char) ch; |
| 5723 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5724 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5725 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 5726 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 5727 | return NULL; |
| 5728 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | } |
| 5730 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5731 | PyObject * |
| 5732 | PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5734 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | if (!PyUnicode_Check(unicode)) { |
| 5736 | PyErr_BadArgument(); |
| 5737 | return NULL; |
| 5738 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 5739 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5740 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5741 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5742 | } |
| 5743 | |
| 5744 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 5745 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5746 | PyObject * |
| 5747 | PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5748 | Py_ssize_t size, |
| 5749 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5750 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5751 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5752 | Py_ssize_t startinpos; |
| 5753 | Py_ssize_t endinpos; |
| 5754 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5755 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5756 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5757 | const char *end; |
| 5758 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5759 | PyObject *errorHandler = NULL; |
| 5760 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5761 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5762 | /* Escaped strings will always be longer than the resulting |
| 5763 | 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] | 5764 | length after conversion to the true value. (But decoding error |
| 5765 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5766 | v = _PyUnicode_New(size); |
| 5767 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5768 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5769 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5770 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5771 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5772 | end = s + size; |
| 5773 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5774 | unsigned char c; |
| 5775 | Py_UCS4 x; |
| 5776 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5777 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5778 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5779 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 5780 | if (*s != '\\') { |
| 5781 | *p++ = (unsigned char)*s++; |
| 5782 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5783 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5784 | startinpos = s-starts; |
| 5785 | |
| 5786 | /* \u-escapes are only interpreted iff the number of leading |
| 5787 | backslashes if odd */ |
| 5788 | bs = s; |
| 5789 | for (;s < end;) { |
| 5790 | if (*s != '\\') |
| 5791 | break; |
| 5792 | *p++ = (unsigned char)*s++; |
| 5793 | } |
| 5794 | if (((s - bs) & 1) == 0 || |
| 5795 | s >= end || |
| 5796 | (*s != 'u' && *s != 'U')) { |
| 5797 | continue; |
| 5798 | } |
| 5799 | p--; |
| 5800 | count = *s=='u' ? 4 : 8; |
| 5801 | s++; |
| 5802 | |
| 5803 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 5804 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 5805 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 5806 | c = (unsigned char)*s; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 5807 | if (!Py_ISXDIGIT(c)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5808 | endinpos = s-starts; |
| 5809 | if (unicode_decode_call_errorhandler( |
| 5810 | errors, &errorHandler, |
| 5811 | "rawunicodeescape", "truncated \\uXXXX", |
| 5812 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5813 | &v, &outpos, &p)) |
| 5814 | goto onError; |
| 5815 | goto nextByte; |
| 5816 | } |
| 5817 | x = (x<<4) & ~0xF; |
| 5818 | if (c >= '0' && c <= '9') |
| 5819 | x += c - '0'; |
| 5820 | else if (c >= 'a' && c <= 'f') |
| 5821 | x += 10 + c - 'a'; |
| 5822 | else |
| 5823 | x += 10 + c - 'A'; |
| 5824 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5825 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5826 | /* UCS-2 character */ |
| 5827 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5828 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5829 | /* UCS-4 character. Either store directly, or as |
| 5830 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5831 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5832 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5833 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5834 | x -= 0x10000L; |
| 5835 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 5836 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5837 | #endif |
| 5838 | } else { |
| 5839 | endinpos = s-starts; |
| 5840 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5841 | if (unicode_decode_call_errorhandler( |
| 5842 | errors, &errorHandler, |
| 5843 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5844 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 5845 | &v, &outpos, &p)) |
| 5846 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5847 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5848 | nextByte: |
| 5849 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5850 | } |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 5851 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5852 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5853 | Py_XDECREF(errorHandler); |
| 5854 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 5855 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5856 | Py_DECREF(v); |
| 5857 | return NULL; |
| 5858 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5859 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5860 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5861 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5862 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5863 | Py_XDECREF(errorHandler); |
| 5864 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5865 | return NULL; |
| 5866 | } |
| 5867 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5868 | PyObject * |
| 5869 | PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5870 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5871 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5872 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5873 | char *p; |
| 5874 | char *q; |
| 5875 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5876 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5877 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5878 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5879 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5880 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5881 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5882 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5883 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5884 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5885 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5886 | if (repr == NULL) |
| 5887 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5888 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5889 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5891 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5892 | while (size-- > 0) { |
| 5893 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5894 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5895 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 5896 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5897 | *p++ = '\\'; |
| 5898 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5899 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 5900 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 5901 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 5902 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 5903 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5904 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5905 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5906 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5907 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5908 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5909 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5910 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 5911 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 5912 | Py_UNICODE ch2; |
| 5913 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 5914 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5915 | ch2 = *s++; |
| 5916 | size--; |
Georg Brandl | 78eef3de | 2010-08-01 20:51:02 +0000 | [diff] [blame] | 5917 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5918 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 5919 | *p++ = '\\'; |
| 5920 | *p++ = 'U'; |
| 5921 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 5922 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 5923 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 5924 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 5925 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 5926 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 5927 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 5928 | *p++ = hexdigits[ucs & 0xf]; |
| 5929 | continue; |
| 5930 | } |
| 5931 | /* Fall through: isolated surrogates are copied as-is */ |
| 5932 | s--; |
| 5933 | size++; |
| 5934 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 5935 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5936 | /* Map 16-bit characters to '\uxxxx' */ |
| 5937 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | *p++ = '\\'; |
| 5939 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 5940 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 5941 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 5942 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 5943 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5945 | /* Copy everything else as-is */ |
| 5946 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5947 | *p++ = (char) ch; |
| 5948 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5949 | size = p - q; |
| 5950 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5951 | assert(size > 0); |
| 5952 | if (_PyBytes_Resize(&repr, size) < 0) |
| 5953 | return NULL; |
| 5954 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5955 | } |
| 5956 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5957 | PyObject * |
| 5958 | PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5960 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5961 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5962 | PyErr_BadArgument(); |
| 5963 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5964 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 5965 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 5966 | PyUnicode_GET_SIZE(unicode)); |
| 5967 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 5968 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5969 | } |
| 5970 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5971 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 5972 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 5973 | PyObject * |
| 5974 | _PyUnicode_DecodeUnicodeInternal(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 5975 | Py_ssize_t size, |
| 5976 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5977 | { |
| 5978 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5979 | Py_ssize_t startinpos; |
| 5980 | Py_ssize_t endinpos; |
| 5981 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5982 | PyUnicodeObject *v; |
| 5983 | Py_UNICODE *p; |
| 5984 | const char *end; |
| 5985 | const char *reason; |
| 5986 | PyObject *errorHandler = NULL; |
| 5987 | PyObject *exc = NULL; |
| 5988 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 5989 | #ifdef Py_UNICODE_WIDE |
| 5990 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 5991 | #endif |
| 5992 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5993 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 5994 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 5995 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5996 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 5997 | /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH |
| 5998 | as string was created with the old API. */ |
| 5999 | if (PyUnicode_GET_SIZE(v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6000 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6001 | p = PyUnicode_AS_UNICODE(v); |
| 6002 | end = s + size; |
| 6003 | |
| 6004 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6005 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6006 | /* We have to sanity check the raw data, otherwise doom looms for |
| 6007 | some malformed UCS-4 data. */ |
| 6008 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6009 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6010 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6011 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6012 | end-s < Py_UNICODE_SIZE |
| 6013 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6014 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6015 | startinpos = s - starts; |
| 6016 | if (end-s < Py_UNICODE_SIZE) { |
| 6017 | endinpos = end-starts; |
| 6018 | reason = "truncated input"; |
| 6019 | } |
| 6020 | else { |
| 6021 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 6022 | reason = "illegal code point (> 0x10FFFF)"; |
| 6023 | } |
| 6024 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 6025 | if (unicode_decode_call_errorhandler( |
| 6026 | errors, &errorHandler, |
| 6027 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 6028 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 6029 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6030 | goto onError; |
| 6031 | } |
| 6032 | } |
| 6033 | else { |
| 6034 | p++; |
| 6035 | s += Py_UNICODE_SIZE; |
| 6036 | } |
| 6037 | } |
| 6038 | |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6039 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6040 | goto onError; |
| 6041 | Py_XDECREF(errorHandler); |
| 6042 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6043 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6044 | Py_DECREF(v); |
| 6045 | return NULL; |
| 6046 | } |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6047 | return (PyObject *)v; |
| 6048 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6049 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 6050 | Py_XDECREF(v); |
| 6051 | Py_XDECREF(errorHandler); |
| 6052 | Py_XDECREF(exc); |
| 6053 | return NULL; |
| 6054 | } |
| 6055 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 6057 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6058 | PyObject * |
| 6059 | PyUnicode_DecodeLatin1(const char *s, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6060 | Py_ssize_t size, |
| 6061 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6062 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Victor Stinner | e57b1c0 | 2011-09-28 22:20:48 +0200 | [diff] [blame] | 6064 | return _PyUnicode_FromUCS1((unsigned char*)s, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6065 | } |
| 6066 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6067 | /* create or adjust a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6068 | static void |
| 6069 | make_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6070 | const char *encoding, |
| 6071 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6072 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6073 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6075 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6076 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 6077 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | } |
| 6079 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6080 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 6081 | goto onError; |
| 6082 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 6083 | goto onError; |
| 6084 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 6085 | goto onError; |
| 6086 | return; |
| 6087 | onError: |
| 6088 | Py_DECREF(*exceptionObject); |
| 6089 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6090 | } |
| 6091 | } |
| 6092 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6093 | /* raises a UnicodeEncodeError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6094 | static void |
| 6095 | raise_encode_exception(PyObject **exceptionObject, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6096 | const char *encoding, |
| 6097 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 6098 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6099 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6100 | { |
| 6101 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6102 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6103 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6104 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6105 | } |
| 6106 | |
| 6107 | /* error handling callback helper: |
| 6108 | build arguments, call the callback and check the arguments, |
| 6109 | put the result into newpos and return the replacement string, which |
| 6110 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6111 | static PyObject * |
| 6112 | unicode_encode_call_errorhandler(const char *errors, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6113 | PyObject **errorHandler, |
| 6114 | const char *encoding, const char *reason, |
| 6115 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 6116 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 6117 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6118 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6119 | 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] | 6120 | |
| 6121 | PyObject *restuple; |
| 6122 | PyObject *resunicode; |
| 6123 | |
| 6124 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6125 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6126 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6127 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6128 | } |
| 6129 | |
| 6130 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6131 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6132 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6133 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6134 | |
| 6135 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6136 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6137 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6138 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6139 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6140 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6141 | Py_DECREF(restuple); |
| 6142 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6143 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6144 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6145 | &resunicode, newpos)) { |
| 6146 | Py_DECREF(restuple); |
| 6147 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6148 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6149 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 6150 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 6151 | Py_DECREF(restuple); |
| 6152 | return NULL; |
| 6153 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6154 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6155 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6156 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6157 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 6158 | Py_DECREF(restuple); |
| 6159 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 6160 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6161 | Py_INCREF(resunicode); |
| 6162 | Py_DECREF(restuple); |
| 6163 | return resunicode; |
| 6164 | } |
| 6165 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6166 | static PyObject * |
| 6167 | unicode_encode_ucs1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6168 | Py_ssize_t size, |
| 6169 | const char *errors, |
| 6170 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6171 | { |
| 6172 | /* output object */ |
| 6173 | PyObject *res; |
| 6174 | /* pointers to the beginning and end+1 of input */ |
| 6175 | const Py_UNICODE *startp = p; |
| 6176 | const Py_UNICODE *endp = p + size; |
| 6177 | /* pointer to the beginning of the unencodable characters */ |
| 6178 | /* const Py_UNICODE *badp = NULL; */ |
| 6179 | /* pointer into the output */ |
| 6180 | char *str; |
| 6181 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6182 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6183 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 6184 | 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] | 6185 | PyObject *errorHandler = NULL; |
| 6186 | PyObject *exc = NULL; |
| 6187 | /* the following variable is used for caching string comparisons |
| 6188 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 6189 | int known_errorHandler = -1; |
| 6190 | |
| 6191 | /* allocate enough for a simple encoding without |
| 6192 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6193 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6194 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6195 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6196 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6197 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6198 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6199 | ressize = size; |
| 6200 | |
| 6201 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6202 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6203 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6204 | /* can we encode this? */ |
| 6205 | if (c<limit) { |
| 6206 | /* no overflow check, because we know that the space is enough */ |
| 6207 | *str++ = (char)c; |
| 6208 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6209 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6210 | else { |
| 6211 | Py_ssize_t unicodepos = p-startp; |
| 6212 | Py_ssize_t requiredsize; |
| 6213 | PyObject *repunicode; |
| 6214 | Py_ssize_t repsize; |
| 6215 | Py_ssize_t newpos; |
| 6216 | Py_ssize_t respos; |
| 6217 | Py_UNICODE *uni2; |
| 6218 | /* startpos for collecting unencodable chars */ |
| 6219 | const Py_UNICODE *collstart = p; |
| 6220 | const Py_UNICODE *collend = p; |
| 6221 | /* find all unecodable characters */ |
| 6222 | while ((collend < endp) && ((*collend)>=limit)) |
| 6223 | ++collend; |
| 6224 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 6225 | if (known_errorHandler==-1) { |
| 6226 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 6227 | known_errorHandler = 1; |
| 6228 | else if (!strcmp(errors, "replace")) |
| 6229 | known_errorHandler = 2; |
| 6230 | else if (!strcmp(errors, "ignore")) |
| 6231 | known_errorHandler = 3; |
| 6232 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 6233 | known_errorHandler = 4; |
| 6234 | else |
| 6235 | known_errorHandler = 0; |
| 6236 | } |
| 6237 | switch (known_errorHandler) { |
| 6238 | case 1: /* strict */ |
| 6239 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 6240 | goto onError; |
| 6241 | case 2: /* replace */ |
| 6242 | while (collstart++<collend) |
| 6243 | *str++ = '?'; /* fall through */ |
| 6244 | case 3: /* ignore */ |
| 6245 | p = collend; |
| 6246 | break; |
| 6247 | case 4: /* xmlcharrefreplace */ |
| 6248 | respos = str - PyBytes_AS_STRING(res); |
| 6249 | /* determine replacement size (temporarily (mis)uses p) */ |
| 6250 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 6251 | if (*p<10) |
| 6252 | repsize += 2+1+1; |
| 6253 | else if (*p<100) |
| 6254 | repsize += 2+2+1; |
| 6255 | else if (*p<1000) |
| 6256 | repsize += 2+3+1; |
| 6257 | else if (*p<10000) |
| 6258 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6259 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6260 | else |
| 6261 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 6262 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6263 | else if (*p<100000) |
| 6264 | repsize += 2+5+1; |
| 6265 | else if (*p<1000000) |
| 6266 | repsize += 2+6+1; |
| 6267 | else |
| 6268 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 6269 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6270 | } |
| 6271 | requiredsize = respos+repsize+(endp-collend); |
| 6272 | if (requiredsize > ressize) { |
| 6273 | if (requiredsize<2*ressize) |
| 6274 | requiredsize = 2*ressize; |
| 6275 | if (_PyBytes_Resize(&res, requiredsize)) |
| 6276 | goto onError; |
| 6277 | str = PyBytes_AS_STRING(res) + respos; |
| 6278 | ressize = requiredsize; |
| 6279 | } |
| 6280 | /* generate replacement (temporarily (mis)uses p) */ |
| 6281 | for (p = collstart; p < collend; ++p) { |
| 6282 | str += sprintf(str, "&#%d;", (int)*p); |
| 6283 | } |
| 6284 | p = collend; |
| 6285 | break; |
| 6286 | default: |
| 6287 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 6288 | encoding, reason, startp, size, &exc, |
| 6289 | collstart-startp, collend-startp, &newpos); |
| 6290 | if (repunicode == NULL) |
| 6291 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6292 | if (PyBytes_Check(repunicode)) { |
| 6293 | /* Directly copy bytes result to output. */ |
| 6294 | repsize = PyBytes_Size(repunicode); |
| 6295 | if (repsize > 1) { |
| 6296 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6297 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6298 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 6299 | Py_DECREF(repunicode); |
| 6300 | goto onError; |
| 6301 | } |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 6302 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6303 | ressize += repsize-1; |
| 6304 | } |
| 6305 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 6306 | str += repsize; |
| 6307 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6308 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6309 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 6310 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6311 | /* need more space? (at least enough for what we |
| 6312 | have+the replacement+the rest of the string, so |
| 6313 | we won't have to check space for encodable characters) */ |
| 6314 | respos = str - PyBytes_AS_STRING(res); |
| 6315 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 6316 | requiredsize = respos+repsize+(endp-collend); |
| 6317 | if (requiredsize > ressize) { |
| 6318 | if (requiredsize<2*ressize) |
| 6319 | requiredsize = 2*ressize; |
| 6320 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 6321 | Py_DECREF(repunicode); |
| 6322 | goto onError; |
| 6323 | } |
| 6324 | str = PyBytes_AS_STRING(res) + respos; |
| 6325 | ressize = requiredsize; |
| 6326 | } |
| 6327 | /* check if there is anything unencodable in the replacement |
| 6328 | and copy it to the output */ |
| 6329 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 6330 | c = *uni2; |
| 6331 | if (c >= limit) { |
| 6332 | raise_encode_exception(&exc, encoding, startp, size, |
| 6333 | unicodepos, unicodepos+1, reason); |
| 6334 | Py_DECREF(repunicode); |
| 6335 | goto onError; |
| 6336 | } |
| 6337 | *str = (char)c; |
| 6338 | } |
| 6339 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6340 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6341 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6342 | } |
| 6343 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6344 | /* Resize if we allocated to much */ |
| 6345 | size = str - PyBytes_AS_STRING(res); |
| 6346 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 6347 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6348 | if (_PyBytes_Resize(&res, size) < 0) |
| 6349 | goto onError; |
| 6350 | } |
| 6351 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6352 | Py_XDECREF(errorHandler); |
| 6353 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 6354 | return res; |
| 6355 | |
| 6356 | onError: |
| 6357 | Py_XDECREF(res); |
| 6358 | Py_XDECREF(errorHandler); |
| 6359 | Py_XDECREF(exc); |
| 6360 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6361 | } |
| 6362 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6363 | PyObject * |
| 6364 | PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 6365 | Py_ssize_t size, |
| 6366 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6367 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6368 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6369 | } |
| 6370 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6371 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6372 | _PyUnicode_AsLatin1String(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6373 | { |
| 6374 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6375 | PyErr_BadArgument(); |
| 6376 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6377 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6378 | if (PyUnicode_READY(unicode) == -1) |
| 6379 | return NULL; |
| 6380 | /* Fast path: if it is a one-byte string, construct |
| 6381 | bytes object directly. */ |
| 6382 | if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND) |
| 6383 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6384 | PyUnicode_GET_LENGTH(unicode)); |
| 6385 | /* Non-Latin-1 characters present. Defer to above function to |
| 6386 | raise the exception. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6387 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6388 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6389 | errors); |
| 6390 | } |
| 6391 | |
| 6392 | PyObject* |
| 6393 | PyUnicode_AsLatin1String(PyObject *unicode) |
| 6394 | { |
| 6395 | return _PyUnicode_AsLatin1String(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6396 | } |
| 6397 | |
| 6398 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 6399 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6400 | PyObject * |
| 6401 | PyUnicode_DecodeASCII(const char *s, |
| 6402 | Py_ssize_t size, |
| 6403 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6405 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6406 | PyUnicodeObject *v; |
| 6407 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6408 | Py_ssize_t startinpos; |
| 6409 | Py_ssize_t endinpos; |
| 6410 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6411 | const char *e; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6412 | unsigned char* d; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6413 | PyObject *errorHandler = NULL; |
| 6414 | PyObject *exc = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6415 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6416 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6417 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6418 | if (size == 1 && *(unsigned char*)s < 128) |
| 6419 | return PyUnicode_FromOrdinal(*(unsigned char*)s); |
| 6420 | |
| 6421 | /* Fast path. Assume the input actually *is* ASCII, and allocate |
| 6422 | a single-block Unicode object with that assumption. If there is |
| 6423 | an error, drop the object and start over. */ |
| 6424 | v = (PyUnicodeObject*)PyUnicode_New(size, 127); |
| 6425 | if (v == NULL) |
| 6426 | goto onError; |
| 6427 | d = PyUnicode_1BYTE_DATA(v); |
| 6428 | for (i = 0; i < size; i++) { |
| 6429 | unsigned char ch = ((unsigned char*)s)[i]; |
| 6430 | if (ch < 128) |
| 6431 | d[i] = ch; |
| 6432 | else |
| 6433 | break; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 6434 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6435 | if (i == size) |
| 6436 | return (PyObject*)v; |
| 6437 | Py_DECREF(v); /* start over */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6438 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6439 | v = _PyUnicode_New(size); |
| 6440 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6441 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6442 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6443 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6445 | e = s + size; |
| 6446 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6447 | register unsigned char c = (unsigned char)*s; |
| 6448 | if (c < 128) { |
| 6449 | *p++ = c; |
| 6450 | ++s; |
| 6451 | } |
| 6452 | else { |
| 6453 | startinpos = s-starts; |
| 6454 | endinpos = startinpos + 1; |
| 6455 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 6456 | if (unicode_decode_call_errorhandler( |
| 6457 | errors, &errorHandler, |
| 6458 | "ascii", "ordinal not in range(128)", |
| 6459 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6460 | &v, &outpos, &p)) |
| 6461 | goto onError; |
| 6462 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6463 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6464 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6465 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6466 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6467 | Py_XDECREF(errorHandler); |
| 6468 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6469 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6470 | Py_DECREF(v); |
| 6471 | return NULL; |
| 6472 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6473 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6474 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6475 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6476 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6477 | Py_XDECREF(errorHandler); |
| 6478 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | return NULL; |
| 6480 | } |
| 6481 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6482 | PyObject * |
| 6483 | PyUnicode_EncodeASCII(const Py_UNICODE *p, |
| 6484 | Py_ssize_t size, |
| 6485 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6486 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6487 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6488 | } |
| 6489 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6490 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6491 | _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6492 | { |
| 6493 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6494 | PyErr_BadArgument(); |
| 6495 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6496 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6497 | if (PyUnicode_READY(unicode) == -1) |
| 6498 | return NULL; |
| 6499 | /* Fast path: if it is an ASCII-only string, construct bytes object |
| 6500 | directly. Else defer to above function to raise the exception. */ |
| 6501 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 6502 | return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode), |
| 6503 | PyUnicode_GET_LENGTH(unicode)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6504 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6505 | PyUnicode_GET_SIZE(unicode), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6506 | errors); |
| 6507 | } |
| 6508 | |
| 6509 | PyObject * |
| 6510 | PyUnicode_AsASCIIString(PyObject *unicode) |
| 6511 | { |
| 6512 | return _PyUnicode_AsASCIIString(unicode, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6513 | } |
| 6514 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6515 | #ifdef HAVE_MBCS |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6516 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6517 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6518 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 6519 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6520 | #define NEED_RETRY |
| 6521 | #endif |
| 6522 | |
| 6523 | /* XXX This code is limited to "true" double-byte encodings, as |
| 6524 | a) it assumes an incomplete character consists of a single byte, and |
| 6525 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6526 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6527 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6528 | static int |
| 6529 | is_dbcs_lead_byte(const char *s, int offset) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6530 | { |
| 6531 | const char *curr = s + offset; |
| 6532 | |
| 6533 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6534 | const char *prev = CharPrev(s, curr); |
| 6535 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6536 | } |
| 6537 | return 0; |
| 6538 | } |
| 6539 | |
| 6540 | /* |
| 6541 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 6542 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 6543 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6544 | static int |
| 6545 | decode_mbcs(PyUnicodeObject **v, |
| 6546 | const char *s, /* MBCS string */ |
| 6547 | int size, /* sizeof MBCS string */ |
| 6548 | int final, |
| 6549 | const char *errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6550 | { |
| 6551 | Py_UNICODE *p; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6552 | Py_ssize_t n; |
| 6553 | DWORD usize; |
| 6554 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6555 | |
| 6556 | assert(size >= 0); |
| 6557 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6558 | /* check and handle 'errors' arg */ |
| 6559 | if (errors==NULL || strcmp(errors, "strict")==0) |
| 6560 | flags = MB_ERR_INVALID_CHARS; |
| 6561 | else if (strcmp(errors, "ignore")==0) |
| 6562 | flags = 0; |
| 6563 | else { |
| 6564 | PyErr_Format(PyExc_ValueError, |
| 6565 | "mbcs encoding does not support errors='%s'", |
| 6566 | errors); |
| 6567 | return -1; |
| 6568 | } |
| 6569 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6570 | /* Skip trailing lead-byte unless 'final' is set */ |
| 6571 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6572 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6573 | |
| 6574 | /* First get the size of the result */ |
| 6575 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6576 | usize = MultiByteToWideChar(CP_ACP, flags, s, size, NULL, 0); |
| 6577 | if (usize==0) |
| 6578 | goto mbcs_decode_error; |
| 6579 | } else |
| 6580 | usize = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6581 | |
| 6582 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6583 | /* Create unicode object */ |
| 6584 | *v = _PyUnicode_New(usize); |
| 6585 | if (*v == NULL) |
| 6586 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6587 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6588 | } |
| 6589 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6590 | /* Extend unicode object */ |
| 6591 | n = PyUnicode_GET_SIZE(*v); |
Victor Stinner | 2fd8227 | 2011-10-03 04:06:05 +0200 | [diff] [blame] | 6592 | if (PyUnicode_Resize((PyObject**)v, n + usize) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6593 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | /* Do the conversion */ |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6597 | if (usize > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6598 | p = PyUnicode_AS_UNICODE(*v) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6599 | if (0 == MultiByteToWideChar(CP_ACP, flags, s, size, p, usize)) { |
| 6600 | goto mbcs_decode_error; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6601 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6602 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6603 | return size; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6604 | |
| 6605 | mbcs_decode_error: |
| 6606 | /* If the last error was ERROR_NO_UNICODE_TRANSLATION, then |
| 6607 | we raise a UnicodeDecodeError - else it is a 'generic' |
| 6608 | windows error |
| 6609 | */ |
| 6610 | if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) { |
| 6611 | /* Ideally, we should get reason from FormatMessage - this |
| 6612 | is the Windows 2000 English version of the message |
| 6613 | */ |
| 6614 | PyObject *exc = NULL; |
| 6615 | const char *reason = "No mapping for the Unicode character exists " |
| 6616 | "in the target multi-byte code page."; |
| 6617 | make_decode_exception(&exc, "mbcs", s, size, 0, 0, reason); |
| 6618 | if (exc != NULL) { |
| 6619 | PyCodec_StrictErrors(exc); |
| 6620 | Py_DECREF(exc); |
| 6621 | } |
| 6622 | } else { |
| 6623 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6624 | } |
| 6625 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6626 | } |
| 6627 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6628 | PyObject * |
| 6629 | PyUnicode_DecodeMBCSStateful(const char *s, |
| 6630 | Py_ssize_t size, |
| 6631 | const char *errors, |
| 6632 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6633 | { |
| 6634 | PyUnicodeObject *v = NULL; |
| 6635 | int done; |
| 6636 | |
| 6637 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6638 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6639 | |
| 6640 | #ifdef NEED_RETRY |
| 6641 | retry: |
| 6642 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6643 | done = decode_mbcs(&v, s, INT_MAX, 0, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6644 | else |
| 6645 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6646 | done = decode_mbcs(&v, s, (int)size, !consumed, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6647 | |
| 6648 | if (done < 0) { |
| 6649 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6650 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6651 | } |
| 6652 | |
| 6653 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6654 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6655 | |
| 6656 | #ifdef NEED_RETRY |
| 6657 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6658 | s += done; |
| 6659 | size -= done; |
| 6660 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6661 | } |
| 6662 | #endif |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6663 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6664 | Py_DECREF(v); |
| 6665 | return NULL; |
| 6666 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6667 | return (PyObject *)v; |
| 6668 | } |
| 6669 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6670 | PyObject * |
| 6671 | PyUnicode_DecodeMBCS(const char *s, |
| 6672 | Py_ssize_t size, |
| 6673 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6674 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6675 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 6676 | } |
| 6677 | |
| 6678 | /* |
| 6679 | * Convert unicode into string object (MBCS). |
| 6680 | * Returns 0 if succeed, -1 otherwise. |
| 6681 | */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6682 | static int |
| 6683 | encode_mbcs(PyObject **repr, |
| 6684 | const Py_UNICODE *p, /* unicode */ |
| 6685 | int size, /* size of unicode */ |
| 6686 | const char* errors) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6687 | { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6688 | BOOL usedDefaultChar = FALSE; |
| 6689 | BOOL *pusedDefaultChar; |
| 6690 | int mbcssize; |
| 6691 | Py_ssize_t n; |
| 6692 | PyObject *exc = NULL; |
| 6693 | DWORD flags; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6694 | |
| 6695 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6696 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6697 | /* check and handle 'errors' arg */ |
| 6698 | if (errors==NULL || strcmp(errors, "strict")==0) { |
| 6699 | flags = WC_NO_BEST_FIT_CHARS; |
| 6700 | pusedDefaultChar = &usedDefaultChar; |
| 6701 | } else if (strcmp(errors, "replace")==0) { |
| 6702 | flags = 0; |
| 6703 | pusedDefaultChar = NULL; |
| 6704 | } else { |
| 6705 | PyErr_Format(PyExc_ValueError, |
| 6706 | "mbcs encoding does not support errors='%s'", |
| 6707 | errors); |
| 6708 | return -1; |
| 6709 | } |
| 6710 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6711 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6712 | if (size > 0) { |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6713 | mbcssize = WideCharToMultiByte(CP_ACP, flags, p, size, NULL, 0, |
| 6714 | NULL, pusedDefaultChar); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6715 | if (mbcssize == 0) { |
| 6716 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6717 | return -1; |
| 6718 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6719 | /* If we used a default char, then we failed! */ |
| 6720 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6721 | goto mbcs_encode_error; |
| 6722 | } else { |
| 6723 | mbcssize = 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6724 | } |
| 6725 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6726 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6727 | /* Create string object */ |
| 6728 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 6729 | if (*repr == NULL) |
| 6730 | return -1; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6731 | n = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6732 | } |
| 6733 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6734 | /* Extend string object */ |
| 6735 | n = PyBytes_Size(*repr); |
| 6736 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 6737 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6738 | } |
| 6739 | |
| 6740 | /* Do the conversion */ |
| 6741 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6742 | char *s = PyBytes_AS_STRING(*repr) + n; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6743 | if (0 == WideCharToMultiByte(CP_ACP, flags, p, size, s, mbcssize, |
| 6744 | NULL, pusedDefaultChar)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6745 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 6746 | return -1; |
| 6747 | } |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6748 | if (pusedDefaultChar && *pusedDefaultChar) |
| 6749 | goto mbcs_encode_error; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6750 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6751 | return 0; |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6752 | |
| 6753 | mbcs_encode_error: |
| 6754 | raise_encode_exception(&exc, "mbcs", p, size, 0, 0, "invalid character"); |
| 6755 | Py_XDECREF(exc); |
| 6756 | return -1; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6757 | } |
| 6758 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6759 | PyObject * |
| 6760 | PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
| 6761 | Py_ssize_t size, |
| 6762 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6763 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6764 | PyObject *repr = NULL; |
| 6765 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 6766 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6767 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6768 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6769 | if (size > INT_MAX) |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6770 | ret = encode_mbcs(&repr, p, INT_MAX, errors); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6771 | else |
| 6772 | #endif |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 6773 | ret = encode_mbcs(&repr, p, (int)size, errors); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6774 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6775 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6776 | Py_XDECREF(repr); |
| 6777 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6778 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6779 | |
| 6780 | #ifdef NEED_RETRY |
| 6781 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6782 | p += INT_MAX; |
| 6783 | size -= INT_MAX; |
| 6784 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6785 | } |
| 6786 | #endif |
| 6787 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6788 | return repr; |
| 6789 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 6790 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6791 | PyObject * |
| 6792 | PyUnicode_AsMBCSString(PyObject *unicode) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6793 | { |
| 6794 | if (!PyUnicode_Check(unicode)) { |
| 6795 | PyErr_BadArgument(); |
| 6796 | return NULL; |
| 6797 | } |
| 6798 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6799 | PyUnicode_GET_SIZE(unicode), |
| 6800 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 6801 | } |
| 6802 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6803 | #undef NEED_RETRY |
| 6804 | |
Victor Stinner | 99b9538 | 2011-07-04 14:23:54 +0200 | [diff] [blame] | 6805 | #endif /* HAVE_MBCS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 6806 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6807 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 6808 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6809 | PyObject * |
| 6810 | PyUnicode_DecodeCharmap(const char *s, |
| 6811 | Py_ssize_t size, |
| 6812 | PyObject *mapping, |
| 6813 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6814 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6815 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6816 | Py_ssize_t startinpos; |
| 6817 | Py_ssize_t endinpos; |
| 6818 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6819 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6820 | PyUnicodeObject *v; |
| 6821 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6822 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6823 | PyObject *errorHandler = NULL; |
| 6824 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6825 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6826 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6827 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6828 | /* Default to Latin-1 */ |
| 6829 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6830 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6831 | |
| 6832 | v = _PyUnicode_New(size); |
| 6833 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6834 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6836 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6837 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6838 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6839 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6840 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 6841 | maplen = PyUnicode_GET_SIZE(mapping); |
| 6842 | while (s < e) { |
| 6843 | unsigned char ch = *s; |
| 6844 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6845 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6846 | if (ch < maplen) |
| 6847 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6848 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6849 | if (x == 0xfffe) { |
| 6850 | /* undefined mapping */ |
| 6851 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6852 | startinpos = s-starts; |
| 6853 | endinpos = startinpos+1; |
| 6854 | if (unicode_decode_call_errorhandler( |
| 6855 | errors, &errorHandler, |
| 6856 | "charmap", "character maps to <undefined>", |
| 6857 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6858 | &v, &outpos, &p)) { |
| 6859 | goto onError; |
| 6860 | } |
| 6861 | continue; |
| 6862 | } |
| 6863 | *p++ = x; |
| 6864 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6865 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6866 | } |
| 6867 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6868 | while (s < e) { |
| 6869 | unsigned char ch = *s; |
| 6870 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 6871 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6872 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 6873 | w = PyLong_FromLong((long)ch); |
| 6874 | if (w == NULL) |
| 6875 | goto onError; |
| 6876 | x = PyObject_GetItem(mapping, w); |
| 6877 | Py_DECREF(w); |
| 6878 | if (x == NULL) { |
| 6879 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 6880 | /* No mapping found means: mapping is undefined. */ |
| 6881 | PyErr_Clear(); |
| 6882 | x = Py_None; |
| 6883 | Py_INCREF(x); |
| 6884 | } else |
| 6885 | goto onError; |
| 6886 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6887 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6888 | /* Apply mapping */ |
| 6889 | if (PyLong_Check(x)) { |
| 6890 | long value = PyLong_AS_LONG(x); |
| 6891 | if (value < 0 || value > 65535) { |
| 6892 | PyErr_SetString(PyExc_TypeError, |
| 6893 | "character mapping must be in range(65536)"); |
| 6894 | Py_DECREF(x); |
| 6895 | goto onError; |
| 6896 | } |
| 6897 | *p++ = (Py_UNICODE)value; |
| 6898 | } |
| 6899 | else if (x == Py_None) { |
| 6900 | /* undefined mapping */ |
| 6901 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 6902 | startinpos = s-starts; |
| 6903 | endinpos = startinpos+1; |
| 6904 | if (unicode_decode_call_errorhandler( |
| 6905 | errors, &errorHandler, |
| 6906 | "charmap", "character maps to <undefined>", |
| 6907 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 6908 | &v, &outpos, &p)) { |
| 6909 | Py_DECREF(x); |
| 6910 | goto onError; |
| 6911 | } |
| 6912 | Py_DECREF(x); |
| 6913 | continue; |
| 6914 | } |
| 6915 | else if (PyUnicode_Check(x)) { |
| 6916 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6917 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6918 | if (targetsize == 1) |
| 6919 | /* 1-1 mapping */ |
| 6920 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6921 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6922 | else if (targetsize > 1) { |
| 6923 | /* 1-n mapping */ |
| 6924 | if (targetsize > extrachars) { |
| 6925 | /* resize first */ |
| 6926 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 6927 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 6928 | (targetsize << 2); |
| 6929 | extrachars += needed; |
| 6930 | /* XXX overflow detection missing */ |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6931 | if (PyUnicode_Resize((PyObject**)&v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6932 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 6933 | Py_DECREF(x); |
| 6934 | goto onError; |
| 6935 | } |
| 6936 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 6937 | } |
| 6938 | Py_UNICODE_COPY(p, |
| 6939 | PyUnicode_AS_UNICODE(x), |
| 6940 | targetsize); |
| 6941 | p += targetsize; |
| 6942 | extrachars -= targetsize; |
| 6943 | } |
| 6944 | /* 1-0 mapping: skip the character */ |
| 6945 | } |
| 6946 | else { |
| 6947 | /* wrong return value */ |
| 6948 | PyErr_SetString(PyExc_TypeError, |
| 6949 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6950 | Py_DECREF(x); |
| 6951 | goto onError; |
| 6952 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6953 | Py_DECREF(x); |
| 6954 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6955 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6956 | } |
| 6957 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Victor Stinner | fe226c0 | 2011-10-03 03:52:20 +0200 | [diff] [blame] | 6958 | if (PyUnicode_Resize((PyObject**)&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6959 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6960 | Py_XDECREF(errorHandler); |
| 6961 | Py_XDECREF(exc); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 6962 | if (_PyUnicode_READY_REPLACE(&v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6963 | Py_DECREF(v); |
| 6964 | return NULL; |
| 6965 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6966 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6967 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6968 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6969 | Py_XDECREF(errorHandler); |
| 6970 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6971 | Py_XDECREF(v); |
| 6972 | return NULL; |
| 6973 | } |
| 6974 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6975 | /* Charmap encoding: the lookup table */ |
| 6976 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 6977 | struct encoding_map { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6978 | PyObject_HEAD |
| 6979 | unsigned char level1[32]; |
| 6980 | int count2, count3; |
| 6981 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6982 | }; |
| 6983 | |
| 6984 | static PyObject* |
| 6985 | encoding_map_size(PyObject *obj, PyObject* args) |
| 6986 | { |
| 6987 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6988 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6989 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6990 | } |
| 6991 | |
| 6992 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6993 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6994 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 6995 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6996 | }; |
| 6997 | |
| 6998 | static void |
| 6999 | encoding_map_dealloc(PyObject* o) |
| 7000 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7001 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7002 | } |
| 7003 | |
| 7004 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7005 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7006 | "EncodingMap", /*tp_name*/ |
| 7007 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 7008 | 0, /*tp_itemsize*/ |
| 7009 | /* methods */ |
| 7010 | encoding_map_dealloc, /*tp_dealloc*/ |
| 7011 | 0, /*tp_print*/ |
| 7012 | 0, /*tp_getattr*/ |
| 7013 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7014 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7015 | 0, /*tp_repr*/ |
| 7016 | 0, /*tp_as_number*/ |
| 7017 | 0, /*tp_as_sequence*/ |
| 7018 | 0, /*tp_as_mapping*/ |
| 7019 | 0, /*tp_hash*/ |
| 7020 | 0, /*tp_call*/ |
| 7021 | 0, /*tp_str*/ |
| 7022 | 0, /*tp_getattro*/ |
| 7023 | 0, /*tp_setattro*/ |
| 7024 | 0, /*tp_as_buffer*/ |
| 7025 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 7026 | 0, /*tp_doc*/ |
| 7027 | 0, /*tp_traverse*/ |
| 7028 | 0, /*tp_clear*/ |
| 7029 | 0, /*tp_richcompare*/ |
| 7030 | 0, /*tp_weaklistoffset*/ |
| 7031 | 0, /*tp_iter*/ |
| 7032 | 0, /*tp_iternext*/ |
| 7033 | encoding_map_methods, /*tp_methods*/ |
| 7034 | 0, /*tp_members*/ |
| 7035 | 0, /*tp_getset*/ |
| 7036 | 0, /*tp_base*/ |
| 7037 | 0, /*tp_dict*/ |
| 7038 | 0, /*tp_descr_get*/ |
| 7039 | 0, /*tp_descr_set*/ |
| 7040 | 0, /*tp_dictoffset*/ |
| 7041 | 0, /*tp_init*/ |
| 7042 | 0, /*tp_alloc*/ |
| 7043 | 0, /*tp_new*/ |
| 7044 | 0, /*tp_free*/ |
| 7045 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7046 | }; |
| 7047 | |
| 7048 | PyObject* |
| 7049 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 7050 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7051 | PyObject *result; |
| 7052 | struct encoding_map *mresult; |
| 7053 | int i; |
| 7054 | int need_dict = 0; |
| 7055 | unsigned char level1[32]; |
| 7056 | unsigned char level2[512]; |
| 7057 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 7058 | int count2 = 0, count3 = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7059 | int kind; |
| 7060 | void *data; |
| 7061 | Py_UCS4 ch; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7062 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7063 | if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7064 | PyErr_BadArgument(); |
| 7065 | return NULL; |
| 7066 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7067 | kind = PyUnicode_KIND(string); |
| 7068 | data = PyUnicode_DATA(string); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7069 | memset(level1, 0xFF, sizeof level1); |
| 7070 | memset(level2, 0xFF, sizeof level2); |
| 7071 | |
| 7072 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 7073 | or if there are non-BMP characters, we need to use |
| 7074 | a mapping dictionary. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7075 | if (PyUnicode_READ(kind, data, 0) != 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7076 | need_dict = 1; |
| 7077 | for (i = 1; i < 256; i++) { |
| 7078 | int l1, l2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7079 | ch = PyUnicode_READ(kind, data, i); |
| 7080 | if (ch == 0 || ch > 0xFFFF) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7081 | need_dict = 1; |
| 7082 | break; |
| 7083 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7084 | if (ch == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7085 | /* unmapped character */ |
| 7086 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7087 | l1 = ch >> 11; |
| 7088 | l2 = ch >> 7; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7089 | if (level1[l1] == 0xFF) |
| 7090 | level1[l1] = count2++; |
| 7091 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7092 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7093 | } |
| 7094 | |
| 7095 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 7096 | need_dict = 1; |
| 7097 | |
| 7098 | if (need_dict) { |
| 7099 | PyObject *result = PyDict_New(); |
| 7100 | PyObject *key, *value; |
| 7101 | if (!result) |
| 7102 | return NULL; |
| 7103 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7104 | key = PyLong_FromLong(PyUnicode_READ(kind, data, i)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7105 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7106 | if (!key || !value) |
| 7107 | goto failed1; |
| 7108 | if (PyDict_SetItem(result, key, value) == -1) |
| 7109 | goto failed1; |
| 7110 | Py_DECREF(key); |
| 7111 | Py_DECREF(value); |
| 7112 | } |
| 7113 | return result; |
| 7114 | failed1: |
| 7115 | Py_XDECREF(key); |
| 7116 | Py_XDECREF(value); |
| 7117 | Py_DECREF(result); |
| 7118 | return NULL; |
| 7119 | } |
| 7120 | |
| 7121 | /* Create a three-level trie */ |
| 7122 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 7123 | 16*count2 + 128*count3 - 1); |
| 7124 | if (!result) |
| 7125 | return PyErr_NoMemory(); |
| 7126 | PyObject_Init(result, &EncodingMapType); |
| 7127 | mresult = (struct encoding_map*)result; |
| 7128 | mresult->count2 = count2; |
| 7129 | mresult->count3 = count3; |
| 7130 | mlevel1 = mresult->level1; |
| 7131 | mlevel2 = mresult->level23; |
| 7132 | mlevel3 = mresult->level23 + 16*count2; |
| 7133 | memcpy(mlevel1, level1, 32); |
| 7134 | memset(mlevel2, 0xFF, 16*count2); |
| 7135 | memset(mlevel3, 0, 128*count3); |
| 7136 | count3 = 0; |
| 7137 | for (i = 1; i < 256; i++) { |
| 7138 | int o1, o2, o3, i2, i3; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7139 | if (PyUnicode_READ(kind, data, i) == 0xFFFE) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7140 | /* unmapped character */ |
| 7141 | continue; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7142 | o1 = PyUnicode_READ(kind, data, i)>>11; |
| 7143 | o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7144 | i2 = 16*mlevel1[o1] + o2; |
| 7145 | if (mlevel2[i2] == 0xFF) |
| 7146 | mlevel2[i2] = count3++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7147 | o3 = PyUnicode_READ(kind, data, i) & 0x7F; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7148 | i3 = 128*mlevel2[i2] + o3; |
| 7149 | mlevel3[i3] = i; |
| 7150 | } |
| 7151 | return result; |
| 7152 | } |
| 7153 | |
| 7154 | static int |
| 7155 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 7156 | { |
| 7157 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 7158 | int l1 = c>>11; |
| 7159 | int l2 = (c>>7) & 0xF; |
| 7160 | int l3 = c & 0x7F; |
| 7161 | int i; |
| 7162 | |
| 7163 | #ifdef Py_UNICODE_WIDE |
| 7164 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7165 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7166 | } |
| 7167 | #endif |
| 7168 | if (c == 0) |
| 7169 | return 0; |
| 7170 | /* level 1*/ |
| 7171 | i = map->level1[l1]; |
| 7172 | if (i == 0xFF) { |
| 7173 | return -1; |
| 7174 | } |
| 7175 | /* level 2*/ |
| 7176 | i = map->level23[16*i+l2]; |
| 7177 | if (i == 0xFF) { |
| 7178 | return -1; |
| 7179 | } |
| 7180 | /* level 3 */ |
| 7181 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 7182 | if (i == 0) { |
| 7183 | return -1; |
| 7184 | } |
| 7185 | return i; |
| 7186 | } |
| 7187 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7188 | /* Lookup the character ch in the mapping. If the character |
| 7189 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 7190 | error occurred). */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7191 | static PyObject * |
| 7192 | charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7193 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7194 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7195 | PyObject *x; |
| 7196 | |
| 7197 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7198 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7199 | x = PyObject_GetItem(mapping, w); |
| 7200 | Py_DECREF(w); |
| 7201 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7202 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7203 | /* No mapping found means: mapping is undefined. */ |
| 7204 | PyErr_Clear(); |
| 7205 | x = Py_None; |
| 7206 | Py_INCREF(x); |
| 7207 | return x; |
| 7208 | } else |
| 7209 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7210 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 7211 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7212 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7213 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7214 | long value = PyLong_AS_LONG(x); |
| 7215 | if (value < 0 || value > 255) { |
| 7216 | PyErr_SetString(PyExc_TypeError, |
| 7217 | "character mapping must be in range(256)"); |
| 7218 | Py_DECREF(x); |
| 7219 | return NULL; |
| 7220 | } |
| 7221 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7222 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7223 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7224 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7225 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7226 | /* wrong return value */ |
| 7227 | PyErr_Format(PyExc_TypeError, |
| 7228 | "character mapping must return integer, bytes or None, not %.400s", |
| 7229 | x->ob_type->tp_name); |
| 7230 | Py_DECREF(x); |
| 7231 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7232 | } |
| 7233 | } |
| 7234 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7235 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7236 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7237 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7238 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 7239 | /* exponentially overallocate to minimize reallocations */ |
| 7240 | if (requiredsize < 2*outsize) |
| 7241 | requiredsize = 2*outsize; |
| 7242 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 7243 | return -1; |
| 7244 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7245 | } |
| 7246 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7247 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7248 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7249 | } charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7250 | /* 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] | 7251 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7252 | space is available. Return a new reference to the object that |
| 7253 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 7254 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 7255 | reallocation error occurred. The caller must decref the result */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7256 | static charmapencode_result |
| 7257 | charmapencode_output(Py_UNICODE c, PyObject *mapping, |
| 7258 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7259 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7260 | PyObject *rep; |
| 7261 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7262 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7263 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7264 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7265 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7266 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7267 | if (res == -1) |
| 7268 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7269 | if (outsize<requiredsize) |
| 7270 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 7271 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7272 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7273 | outstart[(*outpos)++] = (char)res; |
| 7274 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7275 | } |
| 7276 | |
| 7277 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7278 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7279 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7280 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7281 | Py_DECREF(rep); |
| 7282 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7283 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7284 | if (PyLong_Check(rep)) { |
| 7285 | Py_ssize_t requiredsize = *outpos+1; |
| 7286 | if (outsize<requiredsize) |
| 7287 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7288 | Py_DECREF(rep); |
| 7289 | return enc_EXCEPTION; |
| 7290 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7291 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7292 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7293 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7294 | else { |
| 7295 | const char *repchars = PyBytes_AS_STRING(rep); |
| 7296 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 7297 | Py_ssize_t requiredsize = *outpos+repsize; |
| 7298 | if (outsize<requiredsize) |
| 7299 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 7300 | Py_DECREF(rep); |
| 7301 | return enc_EXCEPTION; |
| 7302 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7303 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7304 | memcpy(outstart + *outpos, repchars, repsize); |
| 7305 | *outpos += repsize; |
| 7306 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7307 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7308 | Py_DECREF(rep); |
| 7309 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7310 | } |
| 7311 | |
| 7312 | /* handle an error in PyUnicode_EncodeCharmap |
| 7313 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7314 | static int |
| 7315 | charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7316 | 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] | 7317 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 7318 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7319 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7320 | { |
| 7321 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7322 | Py_ssize_t repsize; |
| 7323 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7324 | Py_UNICODE *uni2; |
| 7325 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7326 | Py_ssize_t collstartpos = *inpos; |
| 7327 | Py_ssize_t collendpos = *inpos+1; |
| 7328 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7329 | char *encoding = "charmap"; |
| 7330 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7331 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7332 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7333 | /* find all unencodable characters */ |
| 7334 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 7335 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7336 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7337 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 7338 | if (res != -1) |
| 7339 | break; |
| 7340 | ++collendpos; |
| 7341 | continue; |
| 7342 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7343 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7344 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 7345 | if (rep==NULL) |
| 7346 | return -1; |
| 7347 | else if (rep!=Py_None) { |
| 7348 | Py_DECREF(rep); |
| 7349 | break; |
| 7350 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7351 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7352 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7353 | } |
| 7354 | /* cache callback name lookup |
| 7355 | * (if not done yet, i.e. it's the first error) */ |
| 7356 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7357 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7358 | *known_errorHandler = 1; |
| 7359 | else if (!strcmp(errors, "replace")) |
| 7360 | *known_errorHandler = 2; |
| 7361 | else if (!strcmp(errors, "ignore")) |
| 7362 | *known_errorHandler = 3; |
| 7363 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7364 | *known_errorHandler = 4; |
| 7365 | else |
| 7366 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7367 | } |
| 7368 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7369 | case 1: /* strict */ |
| 7370 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7371 | return -1; |
| 7372 | case 2: /* replace */ |
| 7373 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7374 | x = charmapencode_output('?', mapping, res, respos); |
| 7375 | if (x==enc_EXCEPTION) { |
| 7376 | return -1; |
| 7377 | } |
| 7378 | else if (x==enc_FAILED) { |
| 7379 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7380 | return -1; |
| 7381 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7382 | } |
| 7383 | /* fall through */ |
| 7384 | case 3: /* ignore */ |
| 7385 | *inpos = collendpos; |
| 7386 | break; |
| 7387 | case 4: /* xmlcharrefreplace */ |
| 7388 | /* generate replacement (temporarily (mis)uses p) */ |
| 7389 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7390 | char buffer[2+29+1+1]; |
| 7391 | char *cp; |
| 7392 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 7393 | for (cp = buffer; *cp; ++cp) { |
| 7394 | x = charmapencode_output(*cp, mapping, res, respos); |
| 7395 | if (x==enc_EXCEPTION) |
| 7396 | return -1; |
| 7397 | else if (x==enc_FAILED) { |
| 7398 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7399 | return -1; |
| 7400 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7401 | } |
| 7402 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7403 | *inpos = collendpos; |
| 7404 | break; |
| 7405 | default: |
| 7406 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7407 | encoding, reason, p, size, exceptionObject, |
| 7408 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7409 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7410 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7411 | if (PyBytes_Check(repunicode)) { |
| 7412 | /* Directly copy bytes result to output. */ |
| 7413 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 7414 | Py_ssize_t requiredsize; |
| 7415 | repsize = PyBytes_Size(repunicode); |
| 7416 | requiredsize = *respos + repsize; |
| 7417 | if (requiredsize > outsize) |
| 7418 | /* Make room for all additional bytes. */ |
| 7419 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 7420 | Py_DECREF(repunicode); |
| 7421 | return -1; |
| 7422 | } |
| 7423 | memcpy(PyBytes_AsString(*res) + *respos, |
| 7424 | PyBytes_AsString(repunicode), repsize); |
| 7425 | *respos += repsize; |
| 7426 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7427 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 7428 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 7429 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7430 | /* generate replacement */ |
| 7431 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 7432 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7433 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 7434 | if (x==enc_EXCEPTION) { |
| 7435 | return -1; |
| 7436 | } |
| 7437 | else if (x==enc_FAILED) { |
| 7438 | Py_DECREF(repunicode); |
| 7439 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 7440 | return -1; |
| 7441 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7442 | } |
| 7443 | *inpos = newpos; |
| 7444 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7445 | } |
| 7446 | return 0; |
| 7447 | } |
| 7448 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7449 | PyObject * |
| 7450 | PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
| 7451 | Py_ssize_t size, |
| 7452 | PyObject *mapping, |
| 7453 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7454 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7455 | /* output object */ |
| 7456 | PyObject *res = NULL; |
| 7457 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7458 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7459 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7460 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7461 | PyObject *errorHandler = NULL; |
| 7462 | PyObject *exc = NULL; |
| 7463 | /* the following variable is used for caching string comparisons |
| 7464 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7465 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7466 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7467 | |
| 7468 | /* Default to Latin-1 */ |
| 7469 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7470 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7472 | /* allocate enough for a simple encoding without |
| 7473 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7474 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7475 | if (res == NULL) |
| 7476 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 7477 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7478 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7480 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7481 | /* try to encode it */ |
| 7482 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 7483 | if (x==enc_EXCEPTION) /* error */ |
| 7484 | goto onError; |
| 7485 | if (x==enc_FAILED) { /* unencodable character */ |
| 7486 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 7487 | &exc, |
| 7488 | &known_errorHandler, &errorHandler, errors, |
| 7489 | &res, &respos)) { |
| 7490 | goto onError; |
| 7491 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7492 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7493 | else |
| 7494 | /* done with this character => adjust input position */ |
| 7495 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7496 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7497 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7498 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7499 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 7500 | if (_PyBytes_Resize(&res, respos) < 0) |
| 7501 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 7502 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7503 | Py_XDECREF(exc); |
| 7504 | Py_XDECREF(errorHandler); |
| 7505 | return res; |
| 7506 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7507 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7508 | Py_XDECREF(res); |
| 7509 | Py_XDECREF(exc); |
| 7510 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7511 | return NULL; |
| 7512 | } |
| 7513 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7514 | PyObject * |
| 7515 | PyUnicode_AsCharmapString(PyObject *unicode, |
| 7516 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7517 | { |
| 7518 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7519 | PyErr_BadArgument(); |
| 7520 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7521 | } |
| 7522 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7523 | PyUnicode_GET_SIZE(unicode), |
| 7524 | mapping, |
| 7525 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7526 | } |
| 7527 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7528 | /* create or adjust a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7529 | static void |
| 7530 | make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7531 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7532 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7533 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7534 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7535 | if (*exceptionObject == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7536 | *exceptionObject = _PyUnicodeTranslateError_Create( |
| 7537 | unicode, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7538 | } |
| 7539 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7540 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 7541 | goto onError; |
| 7542 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 7543 | goto onError; |
| 7544 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 7545 | goto onError; |
| 7546 | return; |
| 7547 | onError: |
| 7548 | Py_DECREF(*exceptionObject); |
| 7549 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7550 | } |
| 7551 | } |
| 7552 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7553 | /* raises a UnicodeTranslateError */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7554 | static void |
| 7555 | raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7556 | PyObject *unicode, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7557 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7558 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7559 | { |
| 7560 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7561 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7562 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7563 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7564 | } |
| 7565 | |
| 7566 | /* error handling callback helper: |
| 7567 | build arguments, call the callback and check the arguments, |
| 7568 | put the result into newpos and return the replacement string, which |
| 7569 | has to be freed by the caller */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7570 | static PyObject * |
| 7571 | unicode_translate_call_errorhandler(const char *errors, |
| 7572 | PyObject **errorHandler, |
| 7573 | const char *reason, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7574 | PyObject *unicode, PyObject **exceptionObject, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7575 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 7576 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7577 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7578 | 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] | 7579 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7580 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7581 | PyObject *restuple; |
| 7582 | PyObject *resunicode; |
| 7583 | |
| 7584 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7585 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7586 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7587 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7588 | } |
| 7589 | |
| 7590 | make_translate_exception(exceptionObject, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7591 | unicode, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7592 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7593 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7594 | |
| 7595 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7596 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7597 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7598 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7599 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 7600 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7601 | Py_DECREF(restuple); |
| 7602 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7603 | } |
| 7604 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7605 | &resunicode, &i_newpos)) { |
| 7606 | Py_DECREF(restuple); |
| 7607 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7608 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7609 | if (i_newpos<0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7610 | *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7611 | else |
| 7612 | *newpos = i_newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7613 | if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7614 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 7615 | Py_DECREF(restuple); |
| 7616 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 7617 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7618 | Py_INCREF(resunicode); |
| 7619 | Py_DECREF(restuple); |
| 7620 | return resunicode; |
| 7621 | } |
| 7622 | |
| 7623 | /* Lookup the character ch in the mapping and put the result in result, |
| 7624 | which must be decrefed by the caller. |
| 7625 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7626 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7627 | charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7628 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7629 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7630 | PyObject *x; |
| 7631 | |
| 7632 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7633 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7634 | x = PyObject_GetItem(mapping, w); |
| 7635 | Py_DECREF(w); |
| 7636 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7637 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 7638 | /* No mapping found means: use 1:1 mapping. */ |
| 7639 | PyErr_Clear(); |
| 7640 | *result = NULL; |
| 7641 | return 0; |
| 7642 | } else |
| 7643 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7644 | } |
| 7645 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7646 | *result = x; |
| 7647 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7648 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7649 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7650 | long value = PyLong_AS_LONG(x); |
| 7651 | long max = PyUnicode_GetMax(); |
| 7652 | if (value < 0 || value > max) { |
| 7653 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 7654 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7655 | Py_DECREF(x); |
| 7656 | return -1; |
| 7657 | } |
| 7658 | *result = x; |
| 7659 | return 0; |
| 7660 | } |
| 7661 | else if (PyUnicode_Check(x)) { |
| 7662 | *result = x; |
| 7663 | return 0; |
| 7664 | } |
| 7665 | else { |
| 7666 | /* wrong return value */ |
| 7667 | PyErr_SetString(PyExc_TypeError, |
| 7668 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7669 | Py_DECREF(x); |
| 7670 | return -1; |
| 7671 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7672 | } |
| 7673 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7674 | if not reallocate and adjust various state variables. |
| 7675 | Return 0 on success, -1 on error */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7676 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7677 | charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7678 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7679 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7680 | Py_ssize_t oldsize = *psize; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 7681 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7682 | /* exponentially overallocate to minimize reallocations */ |
| 7683 | if (requiredsize < 2 * oldsize) |
| 7684 | requiredsize = 2 * oldsize; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7685 | *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); |
| 7686 | if (*outobj == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7687 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7688 | *psize = requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7689 | } |
| 7690 | return 0; |
| 7691 | } |
| 7692 | /* lookup the character, put the result in the output string and adjust |
| 7693 | various state variables. Return a new reference to the object that |
| 7694 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 7695 | undefined (in which case no character was written). |
| 7696 | The called must decref result. |
| 7697 | Return 0 on success, -1 on error. */ |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7698 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7699 | charmaptranslate_output(PyObject *input, Py_ssize_t ipos, |
| 7700 | PyObject *mapping, Py_UCS4 **output, |
| 7701 | Py_ssize_t *osize, Py_ssize_t *opos, |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7702 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7703 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7704 | Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos); |
| 7705 | if (charmaptranslate_lookup(curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7706 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7707 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7708 | /* not found => default to 1:1 mapping */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7709 | (*output)[(*opos)++] = curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7710 | } |
| 7711 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7712 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7713 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7714 | /* 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] | 7715 | (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7716 | } |
| 7717 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7718 | Py_ssize_t repsize; |
| 7719 | if (PyUnicode_READY(*res) == -1) |
| 7720 | return -1; |
| 7721 | repsize = PyUnicode_GET_LENGTH(*res); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7722 | if (repsize==1) { |
| 7723 | /* 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] | 7724 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7725 | } |
| 7726 | else if (repsize!=0) { |
| 7727 | /* more than one character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7728 | Py_ssize_t requiredsize = *opos + |
| 7729 | (PyUnicode_GET_LENGTH(input) - ipos) + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7730 | repsize - 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7731 | Py_ssize_t i; |
| 7732 | if (charmaptranslate_makespace(output, osize, requiredsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7733 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7734 | for(i = 0; i < repsize; i++) |
| 7735 | (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7736 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7737 | } |
| 7738 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7739 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7740 | return 0; |
| 7741 | } |
| 7742 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7743 | PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7744 | _PyUnicode_TranslateCharmap(PyObject *input, |
| 7745 | PyObject *mapping, |
| 7746 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7747 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7748 | /* input object */ |
| 7749 | char *idata; |
| 7750 | Py_ssize_t size, i; |
| 7751 | int kind; |
| 7752 | /* output buffer */ |
| 7753 | Py_UCS4 *output = NULL; |
| 7754 | Py_ssize_t osize; |
| 7755 | PyObject *res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7756 | /* current output position */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7757 | Py_ssize_t opos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7758 | char *reason = "character maps to <undefined>"; |
| 7759 | PyObject *errorHandler = NULL; |
| 7760 | PyObject *exc = NULL; |
| 7761 | /* the following variable is used for caching string comparisons |
| 7762 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 7763 | * 3=ignore, 4=xmlcharrefreplace */ |
| 7764 | int known_errorHandler = -1; |
| 7765 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7766 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7767 | PyErr_BadArgument(); |
| 7768 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7769 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7770 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7771 | if (PyUnicode_READY(input) == -1) |
| 7772 | return NULL; |
| 7773 | idata = (char*)PyUnicode_DATA(input); |
| 7774 | kind = PyUnicode_KIND(input); |
| 7775 | size = PyUnicode_GET_LENGTH(input); |
| 7776 | i = 0; |
| 7777 | |
| 7778 | if (size == 0) { |
| 7779 | Py_INCREF(input); |
| 7780 | return input; |
| 7781 | } |
| 7782 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7783 | /* allocate enough for a simple 1:1 translation without |
| 7784 | replacements, if we need more, we'll resize */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7785 | osize = size; |
| 7786 | output = PyMem_Malloc(osize * sizeof(Py_UCS4)); |
| 7787 | opos = 0; |
| 7788 | if (output == NULL) { |
| 7789 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7790 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7791 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7792 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7793 | while (i<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7794 | /* try to encode it */ |
| 7795 | PyObject *x = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7796 | if (charmaptranslate_output(input, i, mapping, |
| 7797 | &output, &osize, &opos, &x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7798 | Py_XDECREF(x); |
| 7799 | goto onError; |
| 7800 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7801 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7802 | if (x!=Py_None) /* it worked => adjust input pointer */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7803 | ++i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7804 | else { /* untranslatable character */ |
| 7805 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 7806 | Py_ssize_t repsize; |
| 7807 | Py_ssize_t newpos; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7808 | Py_ssize_t uni2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7809 | /* startpos for collecting untranslatable chars */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7810 | Py_ssize_t collstart = i; |
| 7811 | Py_ssize_t collend = i+1; |
| 7812 | Py_ssize_t coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7813 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7814 | /* find all untranslatable characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7815 | while (collend < size) { |
| 7816 | if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7817 | goto onError; |
| 7818 | Py_XDECREF(x); |
| 7819 | if (x!=Py_None) |
| 7820 | break; |
| 7821 | ++collend; |
| 7822 | } |
| 7823 | /* cache callback name lookup |
| 7824 | * (if not done yet, i.e. it's the first error) */ |
| 7825 | if (known_errorHandler==-1) { |
| 7826 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 7827 | known_errorHandler = 1; |
| 7828 | else if (!strcmp(errors, "replace")) |
| 7829 | known_errorHandler = 2; |
| 7830 | else if (!strcmp(errors, "ignore")) |
| 7831 | known_errorHandler = 3; |
| 7832 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 7833 | known_errorHandler = 4; |
| 7834 | else |
| 7835 | known_errorHandler = 0; |
| 7836 | } |
| 7837 | switch (known_errorHandler) { |
| 7838 | case 1: /* strict */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7839 | raise_translate_exception(&exc, input, collstart, |
| 7840 | collend, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7841 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7842 | case 2: /* replace */ |
| 7843 | /* 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] | 7844 | for (coll = collstart; coll<collend; coll++) |
| 7845 | output[opos++] = '?'; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7846 | /* fall through */ |
| 7847 | case 3: /* ignore */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7848 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7849 | break; |
| 7850 | case 4: /* xmlcharrefreplace */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7851 | /* generate replacement (temporarily (mis)uses i) */ |
| 7852 | for (i = collstart; i < collend; ++i) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7853 | char buffer[2+29+1+1]; |
| 7854 | char *cp; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7855 | sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i)); |
| 7856 | if (charmaptranslate_makespace(&output, &osize, |
| 7857 | opos+strlen(buffer)+(size-collend))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7858 | goto onError; |
| 7859 | for (cp = buffer; *cp; ++cp) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7860 | output[opos++] = *cp; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7861 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7862 | i = collend; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7863 | break; |
| 7864 | default: |
| 7865 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7866 | reason, input, &exc, |
| 7867 | collstart, collend, &newpos); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 7868 | if (repunicode == NULL || _PyUnicode_READY_REPLACE(&repunicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7869 | goto onError; |
| 7870 | /* generate replacement */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7871 | repsize = PyUnicode_GET_LENGTH(repunicode); |
| 7872 | if (charmaptranslate_makespace(&output, &osize, |
| 7873 | opos+repsize+(size-collend))) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7874 | Py_DECREF(repunicode); |
| 7875 | goto onError; |
| 7876 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7877 | for (uni2 = 0; repsize-->0; ++uni2) |
| 7878 | output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2); |
| 7879 | i = newpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7880 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7881 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7882 | } |
| 7883 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7884 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos); |
| 7885 | if (!res) |
| 7886 | goto onError; |
| 7887 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7888 | Py_XDECREF(exc); |
| 7889 | Py_XDECREF(errorHandler); |
| 7890 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7891 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7892 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7893 | PyMem_Free(output); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7894 | Py_XDECREF(exc); |
| 7895 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7896 | return NULL; |
| 7897 | } |
| 7898 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7899 | /* Deprecated. Use PyUnicode_Translate instead. */ |
| 7900 | PyObject * |
| 7901 | PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
| 7902 | Py_ssize_t size, |
| 7903 | PyObject *mapping, |
| 7904 | const char *errors) |
| 7905 | { |
| 7906 | PyObject *unicode = PyUnicode_FromUnicode(p, size); |
| 7907 | if (!unicode) |
| 7908 | return NULL; |
| 7909 | return _PyUnicode_TranslateCharmap(unicode, mapping, errors); |
| 7910 | } |
| 7911 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 7912 | PyObject * |
| 7913 | PyUnicode_Translate(PyObject *str, |
| 7914 | PyObject *mapping, |
| 7915 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7916 | { |
| 7917 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7918 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7919 | str = PyUnicode_FromObject(str); |
| 7920 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7921 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7922 | result = _PyUnicode_TranslateCharmap(str, mapping, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7923 | Py_DECREF(str); |
| 7924 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7925 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7926 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7927 | Py_XDECREF(str); |
| 7928 | return NULL; |
| 7929 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7930 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 7931 | static Py_UCS4 |
| 7932 | fix_decimal_and_space_to_ascii(PyUnicodeObject *self) |
| 7933 | { |
| 7934 | /* No need to call PyUnicode_READY(self) because this function is only |
| 7935 | called as a callback from fixup() which does it already. */ |
| 7936 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 7937 | const int kind = PyUnicode_KIND(self); |
| 7938 | void *data = PyUnicode_DATA(self); |
| 7939 | Py_UCS4 maxchar = 0, ch, fixed; |
| 7940 | Py_ssize_t i; |
| 7941 | |
| 7942 | for (i = 0; i < len; ++i) { |
| 7943 | ch = PyUnicode_READ(kind, data, i); |
| 7944 | fixed = 0; |
| 7945 | if (ch > 127) { |
| 7946 | if (Py_UNICODE_ISSPACE(ch)) |
| 7947 | fixed = ' '; |
| 7948 | else { |
| 7949 | const int decimal = Py_UNICODE_TODECIMAL(ch); |
| 7950 | if (decimal >= 0) |
| 7951 | fixed = '0' + decimal; |
| 7952 | } |
| 7953 | if (fixed != 0) { |
| 7954 | if (fixed > maxchar) |
| 7955 | maxchar = fixed; |
| 7956 | PyUnicode_WRITE(kind, data, i, fixed); |
| 7957 | } |
| 7958 | else if (ch > maxchar) |
| 7959 | maxchar = ch; |
| 7960 | } |
| 7961 | else if (ch > maxchar) |
| 7962 | maxchar = ch; |
| 7963 | } |
| 7964 | |
| 7965 | return maxchar; |
| 7966 | } |
| 7967 | |
| 7968 | PyObject * |
| 7969 | _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode) |
| 7970 | { |
| 7971 | if (!PyUnicode_Check(unicode)) { |
| 7972 | PyErr_BadInternalCall(); |
| 7973 | return NULL; |
| 7974 | } |
| 7975 | if (PyUnicode_READY(unicode) == -1) |
| 7976 | return NULL; |
| 7977 | if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) { |
| 7978 | /* If the string is already ASCII, just return the same string */ |
| 7979 | Py_INCREF(unicode); |
| 7980 | return unicode; |
| 7981 | } |
| 7982 | return fixup((PyUnicodeObject *)unicode, fix_decimal_and_space_to_ascii); |
| 7983 | } |
| 7984 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 7985 | PyObject * |
| 7986 | PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, |
| 7987 | Py_ssize_t length) |
| 7988 | { |
| 7989 | PyObject *result; |
| 7990 | Py_UNICODE *p; /* write pointer into result */ |
| 7991 | Py_ssize_t i; |
| 7992 | /* Copy to a new string */ |
| 7993 | result = (PyObject *)_PyUnicode_New(length); |
| 7994 | Py_UNICODE_COPY(PyUnicode_AS_UNICODE(result), s, length); |
| 7995 | if (result == NULL) |
| 7996 | return result; |
| 7997 | p = PyUnicode_AS_UNICODE(result); |
| 7998 | /* Iterate over code points */ |
| 7999 | for (i = 0; i < length; i++) { |
| 8000 | Py_UNICODE ch =s[i]; |
| 8001 | if (ch > 127) { |
| 8002 | int decimal = Py_UNICODE_TODECIMAL(ch); |
| 8003 | if (decimal >= 0) |
| 8004 | p[i] = '0' + decimal; |
| 8005 | } |
| 8006 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8007 | if (PyUnicode_READY((PyUnicodeObject*)result) == -1) { |
| 8008 | Py_DECREF(result); |
| 8009 | return NULL; |
| 8010 | } |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 8011 | return result; |
| 8012 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8013 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 8014 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8015 | int |
| 8016 | PyUnicode_EncodeDecimal(Py_UNICODE *s, |
| 8017 | Py_ssize_t length, |
| 8018 | char *output, |
| 8019 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8020 | { |
| 8021 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8022 | PyObject *errorHandler = NULL; |
| 8023 | PyObject *exc = NULL; |
| 8024 | const char *encoding = "decimal"; |
| 8025 | const char *reason = "invalid decimal Unicode string"; |
| 8026 | /* the following variable is used for caching string comparisons |
| 8027 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 8028 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8029 | |
| 8030 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8031 | PyErr_BadArgument(); |
| 8032 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8033 | } |
| 8034 | |
| 8035 | p = s; |
| 8036 | end = s + length; |
| 8037 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8038 | register Py_UNICODE ch = *p; |
| 8039 | int decimal; |
| 8040 | PyObject *repunicode; |
| 8041 | Py_ssize_t repsize; |
| 8042 | Py_ssize_t newpos; |
| 8043 | Py_UNICODE *uni2; |
| 8044 | Py_UNICODE *collstart; |
| 8045 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8046 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8047 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8048 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8049 | ++p; |
| 8050 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8051 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8052 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8053 | if (decimal >= 0) { |
| 8054 | *output++ = '0' + decimal; |
| 8055 | ++p; |
| 8056 | continue; |
| 8057 | } |
| 8058 | if (0 < ch && ch < 256) { |
| 8059 | *output++ = (char)ch; |
| 8060 | ++p; |
| 8061 | continue; |
| 8062 | } |
| 8063 | /* All other characters are considered unencodable */ |
| 8064 | collstart = p; |
| 8065 | collend = p+1; |
| 8066 | while (collend < end) { |
| 8067 | if ((0 < *collend && *collend < 256) || |
| 8068 | !Py_UNICODE_ISSPACE(*collend) || |
| 8069 | Py_UNICODE_TODECIMAL(*collend)) |
| 8070 | break; |
| 8071 | } |
| 8072 | /* cache callback name lookup |
| 8073 | * (if not done yet, i.e. it's the first error) */ |
| 8074 | if (known_errorHandler==-1) { |
| 8075 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 8076 | known_errorHandler = 1; |
| 8077 | else if (!strcmp(errors, "replace")) |
| 8078 | known_errorHandler = 2; |
| 8079 | else if (!strcmp(errors, "ignore")) |
| 8080 | known_errorHandler = 3; |
| 8081 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 8082 | known_errorHandler = 4; |
| 8083 | else |
| 8084 | known_errorHandler = 0; |
| 8085 | } |
| 8086 | switch (known_errorHandler) { |
| 8087 | case 1: /* strict */ |
| 8088 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 8089 | goto onError; |
| 8090 | case 2: /* replace */ |
| 8091 | for (p = collstart; p < collend; ++p) |
| 8092 | *output++ = '?'; |
| 8093 | /* fall through */ |
| 8094 | case 3: /* ignore */ |
| 8095 | p = collend; |
| 8096 | break; |
| 8097 | case 4: /* xmlcharrefreplace */ |
| 8098 | /* generate replacement (temporarily (mis)uses p) */ |
| 8099 | for (p = collstart; p < collend; ++p) |
| 8100 | output += sprintf(output, "&#%d;", (int)*p); |
| 8101 | p = collend; |
| 8102 | break; |
| 8103 | default: |
| 8104 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 8105 | encoding, reason, s, length, &exc, |
| 8106 | collstart-s, collend-s, &newpos); |
| 8107 | if (repunicode == NULL) |
| 8108 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8109 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 8110 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 8111 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 8112 | Py_DECREF(repunicode); |
| 8113 | goto onError; |
| 8114 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8115 | /* generate replacement */ |
| 8116 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 8117 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 8118 | Py_UNICODE ch = *uni2; |
| 8119 | if (Py_UNICODE_ISSPACE(ch)) |
| 8120 | *output++ = ' '; |
| 8121 | else { |
| 8122 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 8123 | if (decimal >= 0) |
| 8124 | *output++ = '0' + decimal; |
| 8125 | else if (0 < ch && ch < 256) |
| 8126 | *output++ = (char)ch; |
| 8127 | else { |
| 8128 | Py_DECREF(repunicode); |
| 8129 | raise_encode_exception(&exc, encoding, |
| 8130 | s, length, collstart-s, collend-s, reason); |
| 8131 | goto onError; |
| 8132 | } |
| 8133 | } |
| 8134 | } |
| 8135 | p = s + newpos; |
| 8136 | Py_DECREF(repunicode); |
| 8137 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8138 | } |
| 8139 | /* 0-terminate the output string */ |
| 8140 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8141 | Py_XDECREF(exc); |
| 8142 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8143 | return 0; |
| 8144 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8145 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 8146 | Py_XDECREF(exc); |
| 8147 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 8148 | return -1; |
| 8149 | } |
| 8150 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8151 | /* --- Helpers ------------------------------------------------------------ */ |
| 8152 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8153 | #include "stringlib/ucs1lib.h" |
| 8154 | #include "stringlib/fastsearch.h" |
| 8155 | #include "stringlib/partition.h" |
| 8156 | #include "stringlib/split.h" |
| 8157 | #include "stringlib/count.h" |
| 8158 | #include "stringlib/find.h" |
| 8159 | #include "stringlib/localeutil.h" |
| 8160 | #include "stringlib/undef.h" |
| 8161 | |
| 8162 | #include "stringlib/ucs2lib.h" |
| 8163 | #include "stringlib/fastsearch.h" |
| 8164 | #include "stringlib/partition.h" |
| 8165 | #include "stringlib/split.h" |
| 8166 | #include "stringlib/count.h" |
| 8167 | #include "stringlib/find.h" |
| 8168 | #include "stringlib/localeutil.h" |
| 8169 | #include "stringlib/undef.h" |
| 8170 | |
| 8171 | #include "stringlib/ucs4lib.h" |
| 8172 | #include "stringlib/fastsearch.h" |
| 8173 | #include "stringlib/partition.h" |
| 8174 | #include "stringlib/split.h" |
| 8175 | #include "stringlib/count.h" |
| 8176 | #include "stringlib/find.h" |
| 8177 | #include "stringlib/localeutil.h" |
| 8178 | #include "stringlib/undef.h" |
| 8179 | |
| 8180 | static Py_ssize_t |
| 8181 | any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t, |
| 8182 | const Py_UCS1*, Py_ssize_t, |
| 8183 | Py_ssize_t, Py_ssize_t), |
| 8184 | Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t, |
| 8185 | const Py_UCS2*, Py_ssize_t, |
| 8186 | Py_ssize_t, Py_ssize_t), |
| 8187 | Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t, |
| 8188 | const Py_UCS4*, Py_ssize_t, |
| 8189 | Py_ssize_t, Py_ssize_t), |
| 8190 | PyObject* s1, PyObject* s2, |
| 8191 | Py_ssize_t start, |
| 8192 | Py_ssize_t end) |
| 8193 | { |
| 8194 | int kind1, kind2, kind; |
| 8195 | void *buf1, *buf2; |
| 8196 | Py_ssize_t len1, len2, result; |
| 8197 | |
| 8198 | kind1 = PyUnicode_KIND(s1); |
| 8199 | kind2 = PyUnicode_KIND(s2); |
| 8200 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8201 | buf1 = PyUnicode_DATA(s1); |
| 8202 | buf2 = PyUnicode_DATA(s2); |
| 8203 | if (kind1 != kind) |
| 8204 | buf1 = _PyUnicode_AsKind(s1, kind); |
| 8205 | if (!buf1) |
| 8206 | return -2; |
| 8207 | if (kind2 != kind) |
| 8208 | buf2 = _PyUnicode_AsKind(s2, kind); |
| 8209 | if (!buf2) { |
| 8210 | if (kind1 != kind) PyMem_Free(buf1); |
| 8211 | return -2; |
| 8212 | } |
| 8213 | len1 = PyUnicode_GET_LENGTH(s1); |
| 8214 | len2 = PyUnicode_GET_LENGTH(s2); |
| 8215 | |
| 8216 | switch(kind) { |
| 8217 | case PyUnicode_1BYTE_KIND: |
| 8218 | result = ucs1(buf1, len1, buf2, len2, start, end); |
| 8219 | break; |
| 8220 | case PyUnicode_2BYTE_KIND: |
| 8221 | result = ucs2(buf1, len1, buf2, len2, start, end); |
| 8222 | break; |
| 8223 | case PyUnicode_4BYTE_KIND: |
| 8224 | result = ucs4(buf1, len1, buf2, len2, start, end); |
| 8225 | break; |
| 8226 | default: |
| 8227 | assert(0); result = -2; |
| 8228 | } |
| 8229 | |
| 8230 | if (kind1 != kind) |
| 8231 | PyMem_Free(buf1); |
| 8232 | if (kind2 != kind) |
| 8233 | PyMem_Free(buf2); |
| 8234 | |
| 8235 | return result; |
| 8236 | } |
| 8237 | |
| 8238 | Py_ssize_t |
| 8239 | _PyUnicode_InsertThousandsGrouping(int kind, void *data, |
| 8240 | Py_ssize_t n_buffer, |
| 8241 | void *digits, Py_ssize_t n_digits, |
| 8242 | Py_ssize_t min_width, |
| 8243 | const char *grouping, |
| 8244 | const char *thousands_sep) |
| 8245 | { |
| 8246 | switch(kind) { |
| 8247 | case PyUnicode_1BYTE_KIND: |
| 8248 | return _PyUnicode_ucs1_InsertThousandsGrouping( |
| 8249 | (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits, |
| 8250 | min_width, grouping, thousands_sep); |
| 8251 | case PyUnicode_2BYTE_KIND: |
| 8252 | return _PyUnicode_ucs2_InsertThousandsGrouping( |
| 8253 | (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits, |
| 8254 | min_width, grouping, thousands_sep); |
| 8255 | case PyUnicode_4BYTE_KIND: |
| 8256 | return _PyUnicode_ucs4_InsertThousandsGrouping( |
| 8257 | (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits, |
| 8258 | min_width, grouping, thousands_sep); |
| 8259 | } |
| 8260 | assert(0); |
| 8261 | return -1; |
| 8262 | } |
| 8263 | |
| 8264 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8265 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8266 | #include "stringlib/fastsearch.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8267 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8268 | #include "stringlib/count.h" |
| 8269 | #include "stringlib/find.h" |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 8270 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8271 | /* helper macro to fixup start/end slice values */ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 8272 | #define ADJUST_INDICES(start, end, len) \ |
| 8273 | if (end > len) \ |
| 8274 | end = len; \ |
| 8275 | else if (end < 0) { \ |
| 8276 | end += len; \ |
| 8277 | if (end < 0) \ |
| 8278 | end = 0; \ |
| 8279 | } \ |
| 8280 | if (start < 0) { \ |
| 8281 | start += len; \ |
| 8282 | if (start < 0) \ |
| 8283 | start = 0; \ |
| 8284 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8285 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8286 | Py_ssize_t |
| 8287 | PyUnicode_Count(PyObject *str, |
| 8288 | PyObject *substr, |
| 8289 | Py_ssize_t start, |
| 8290 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8291 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8292 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8293 | PyUnicodeObject* str_obj; |
| 8294 | PyUnicodeObject* sub_obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8295 | int kind1, kind2, kind; |
| 8296 | void *buf1 = NULL, *buf2 = NULL; |
| 8297 | Py_ssize_t len1, len2; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8298 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8299 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8300 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8301 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8302 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8303 | if (!sub_obj || PyUnicode_READY(sub_obj) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8304 | Py_DECREF(str_obj); |
| 8305 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8306 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8307 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8308 | kind1 = PyUnicode_KIND(str_obj); |
| 8309 | kind2 = PyUnicode_KIND(sub_obj); |
| 8310 | kind = kind1 > kind2 ? kind1 : kind2; |
| 8311 | buf1 = PyUnicode_DATA(str_obj); |
| 8312 | if (kind1 != kind) |
| 8313 | buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind); |
| 8314 | if (!buf1) |
| 8315 | goto onError; |
| 8316 | buf2 = PyUnicode_DATA(sub_obj); |
| 8317 | if (kind2 != kind) |
| 8318 | buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind); |
| 8319 | if (!buf2) |
| 8320 | goto onError; |
| 8321 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 8322 | len2 = PyUnicode_GET_LENGTH(sub_obj); |
| 8323 | |
| 8324 | ADJUST_INDICES(start, end, len1); |
| 8325 | switch(kind) { |
| 8326 | case PyUnicode_1BYTE_KIND: |
| 8327 | result = ucs1lib_count( |
| 8328 | ((Py_UCS1*)buf1) + start, end - start, |
| 8329 | buf2, len2, PY_SSIZE_T_MAX |
| 8330 | ); |
| 8331 | break; |
| 8332 | case PyUnicode_2BYTE_KIND: |
| 8333 | result = ucs2lib_count( |
| 8334 | ((Py_UCS2*)buf1) + start, end - start, |
| 8335 | buf2, len2, PY_SSIZE_T_MAX |
| 8336 | ); |
| 8337 | break; |
| 8338 | case PyUnicode_4BYTE_KIND: |
| 8339 | result = ucs4lib_count( |
| 8340 | ((Py_UCS4*)buf1) + start, end - start, |
| 8341 | buf2, len2, PY_SSIZE_T_MAX |
| 8342 | ); |
| 8343 | break; |
| 8344 | default: |
| 8345 | assert(0); result = 0; |
| 8346 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8347 | |
| 8348 | Py_DECREF(sub_obj); |
| 8349 | Py_DECREF(str_obj); |
| 8350 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8351 | if (kind1 != kind) |
| 8352 | PyMem_Free(buf1); |
| 8353 | if (kind2 != kind) |
| 8354 | PyMem_Free(buf2); |
| 8355 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8356 | return result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8357 | onError: |
| 8358 | Py_DECREF(sub_obj); |
| 8359 | Py_DECREF(str_obj); |
| 8360 | if (kind1 != kind && buf1) |
| 8361 | PyMem_Free(buf1); |
| 8362 | if (kind2 != kind && buf2) |
| 8363 | PyMem_Free(buf2); |
| 8364 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8365 | } |
| 8366 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8367 | Py_ssize_t |
| 8368 | PyUnicode_Find(PyObject *str, |
| 8369 | PyObject *sub, |
| 8370 | Py_ssize_t start, |
| 8371 | Py_ssize_t end, |
| 8372 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8373 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8374 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8375 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8376 | str = PyUnicode_FromObject(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8377 | if (!str || PyUnicode_READY(str) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8378 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8379 | sub = PyUnicode_FromObject(sub); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8380 | if (!sub || PyUnicode_READY(sub) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8381 | Py_DECREF(str); |
| 8382 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8383 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8384 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8385 | if (direction > 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8386 | result = any_find_slice( |
| 8387 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 8388 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8389 | ); |
| 8390 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8391 | result = any_find_slice( |
| 8392 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 8393 | str, sub, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8394 | ); |
| 8395 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8396 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8397 | Py_DECREF(sub); |
| 8398 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8399 | return result; |
| 8400 | } |
| 8401 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8402 | Py_ssize_t |
| 8403 | PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, |
| 8404 | Py_ssize_t start, Py_ssize_t end, |
| 8405 | int direction) |
| 8406 | { |
| 8407 | char *result; |
| 8408 | int kind; |
| 8409 | if (PyUnicode_READY(str) == -1) |
| 8410 | return -2; |
Victor Stinner | 267aa24 | 2011-10-02 01:08:37 +0200 | [diff] [blame] | 8411 | if (start < 0 || end < 0) { |
| 8412 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 8413 | return -2; |
| 8414 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8415 | if (end > PyUnicode_GET_LENGTH(str)) |
| 8416 | end = PyUnicode_GET_LENGTH(str); |
| 8417 | kind = PyUnicode_KIND(str); |
| 8418 | result = findchar(PyUnicode_1BYTE_DATA(str) |
| 8419 | + PyUnicode_KIND_SIZE(kind, start), |
| 8420 | kind, |
| 8421 | end-start, ch, direction); |
| 8422 | if (!result) |
| 8423 | return -1; |
| 8424 | return (result-(char*)PyUnicode_DATA(str)) >> (kind-1); |
| 8425 | } |
| 8426 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8427 | static int |
| 8428 | tailmatch(PyUnicodeObject *self, |
| 8429 | PyUnicodeObject *substring, |
| 8430 | Py_ssize_t start, |
| 8431 | Py_ssize_t end, |
| 8432 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8433 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8434 | int kind_self; |
| 8435 | int kind_sub; |
| 8436 | void *data_self; |
| 8437 | void *data_sub; |
| 8438 | Py_ssize_t offset; |
| 8439 | Py_ssize_t i; |
| 8440 | Py_ssize_t end_sub; |
| 8441 | |
| 8442 | if (PyUnicode_READY(self) == -1 || |
| 8443 | PyUnicode_READY(substring) == -1) |
| 8444 | return 0; |
| 8445 | |
| 8446 | if (PyUnicode_GET_LENGTH(substring) == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8447 | return 1; |
| 8448 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8449 | ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self)); |
| 8450 | end -= PyUnicode_GET_LENGTH(substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8451 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8452 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8453 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8454 | kind_self = PyUnicode_KIND(self); |
| 8455 | data_self = PyUnicode_DATA(self); |
| 8456 | kind_sub = PyUnicode_KIND(substring); |
| 8457 | data_sub = PyUnicode_DATA(substring); |
| 8458 | end_sub = PyUnicode_GET_LENGTH(substring) - 1; |
| 8459 | |
| 8460 | if (direction > 0) |
| 8461 | offset = end; |
| 8462 | else |
| 8463 | offset = start; |
| 8464 | |
| 8465 | if (PyUnicode_READ(kind_self, data_self, offset) == |
| 8466 | PyUnicode_READ(kind_sub, data_sub, 0) && |
| 8467 | PyUnicode_READ(kind_self, data_self, offset + end_sub) == |
| 8468 | PyUnicode_READ(kind_sub, data_sub, end_sub)) { |
| 8469 | /* If both are of the same kind, memcmp is sufficient */ |
| 8470 | if (kind_self == kind_sub) { |
| 8471 | return ! memcmp((char *)data_self + |
| 8472 | (offset * PyUnicode_CHARACTER_SIZE(substring)), |
| 8473 | data_sub, |
| 8474 | PyUnicode_GET_LENGTH(substring) * |
| 8475 | PyUnicode_CHARACTER_SIZE(substring)); |
| 8476 | } |
| 8477 | /* otherwise we have to compare each character by first accesing it */ |
| 8478 | else { |
| 8479 | /* We do not need to compare 0 and len(substring)-1 because |
| 8480 | the if statement above ensured already that they are equal |
| 8481 | when we end up here. */ |
| 8482 | // TODO: honor direction and do a forward or backwards search |
| 8483 | for (i = 1; i < end_sub; ++i) { |
| 8484 | if (PyUnicode_READ(kind_self, data_self, offset + i) != |
| 8485 | PyUnicode_READ(kind_sub, data_sub, i)) |
| 8486 | return 0; |
| 8487 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8488 | return 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8489 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8490 | } |
| 8491 | |
| 8492 | return 0; |
| 8493 | } |
| 8494 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8495 | Py_ssize_t |
| 8496 | PyUnicode_Tailmatch(PyObject *str, |
| 8497 | PyObject *substr, |
| 8498 | Py_ssize_t start, |
| 8499 | Py_ssize_t end, |
| 8500 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8501 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8502 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8503 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8504 | str = PyUnicode_FromObject(str); |
| 8505 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8506 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8507 | substr = PyUnicode_FromObject(substr); |
| 8508 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8509 | Py_DECREF(str); |
| 8510 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8511 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8512 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8513 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8514 | (PyUnicodeObject *)substr, |
| 8515 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8516 | Py_DECREF(str); |
| 8517 | Py_DECREF(substr); |
| 8518 | return result; |
| 8519 | } |
| 8520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8521 | /* Apply fixfct filter to the Unicode object self and return a |
| 8522 | reference to the modified object */ |
| 8523 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8524 | static PyObject * |
| 8525 | fixup(PyUnicodeObject *self, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8526 | Py_UCS4 (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8527 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8528 | PyObject *u; |
| 8529 | Py_UCS4 maxchar_old, maxchar_new = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8530 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8531 | if (PyUnicode_READY(self) == -1) |
| 8532 | return NULL; |
| 8533 | maxchar_old = PyUnicode_MAX_CHAR_VALUE(self); |
| 8534 | u = PyUnicode_New(PyUnicode_GET_LENGTH(self), |
| 8535 | maxchar_old); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8536 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8537 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8538 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8539 | Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self), |
| 8540 | PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u)); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8541 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8542 | /* fix functions return the new maximum character in a string, |
| 8543 | if the kind of the resulting unicode object does not change, |
| 8544 | everything is fine. Otherwise we need to change the string kind |
| 8545 | and re-run the fix function. */ |
| 8546 | maxchar_new = fixfct((PyUnicodeObject*)u); |
| 8547 | if (maxchar_new == 0) |
| 8548 | /* do nothing, keep maxchar_new at 0 which means no changes. */; |
| 8549 | else if (maxchar_new <= 127) |
| 8550 | maxchar_new = 127; |
| 8551 | else if (maxchar_new <= 255) |
| 8552 | maxchar_new = 255; |
| 8553 | else if (maxchar_new <= 65535) |
| 8554 | maxchar_new = 65535; |
| 8555 | else |
| 8556 | maxchar_new = 1114111; /* 0x10ffff */ |
| 8557 | |
| 8558 | if (!maxchar_new && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8559 | /* fixfct should return TRUE if it modified the buffer. If |
| 8560 | FALSE, return a reference to the original buffer instead |
| 8561 | (to save space, not time) */ |
| 8562 | Py_INCREF(self); |
| 8563 | Py_DECREF(u); |
| 8564 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8565 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8566 | else if (maxchar_new == maxchar_old) { |
| 8567 | return u; |
| 8568 | } |
| 8569 | else { |
| 8570 | /* In case the maximum character changed, we need to |
| 8571 | convert the string to the new category. */ |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8572 | PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8573 | if (v == NULL) { |
| 8574 | Py_DECREF(u); |
| 8575 | return NULL; |
| 8576 | } |
| 8577 | if (maxchar_new > maxchar_old) { |
| 8578 | /* If the maxchar increased so that the kind changed, not all |
| 8579 | characters are representable anymore and we need to fix the |
| 8580 | string again. This only happens in very few cases. */ |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8581 | if (PyUnicode_CopyCharacters(v, 0, |
| 8582 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8583 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8584 | { |
| 8585 | Py_DECREF(u); |
| 8586 | return NULL; |
| 8587 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8588 | maxchar_old = fixfct((PyUnicodeObject*)v); |
| 8589 | assert(maxchar_old > 0 && maxchar_old <= maxchar_new); |
| 8590 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8591 | else { |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8592 | if (PyUnicode_CopyCharacters(v, 0, |
| 8593 | u, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8594 | PyUnicode_GET_LENGTH(self)) < 0) |
| 8595 | { |
| 8596 | Py_DECREF(u); |
| 8597 | return NULL; |
| 8598 | } |
| 8599 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8600 | |
| 8601 | Py_DECREF(u); |
| 8602 | return v; |
| 8603 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8604 | } |
| 8605 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8606 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8607 | fixupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8608 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8609 | /* No need to call PyUnicode_READY(self) because this function is only |
| 8610 | called as a callback from fixup() which does it already. */ |
| 8611 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8612 | const int kind = PyUnicode_KIND(self); |
| 8613 | void *data = PyUnicode_DATA(self); |
| 8614 | int touched = 0; |
| 8615 | Py_UCS4 maxchar = 0; |
| 8616 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8617 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8618 | for (i = 0; i < len; ++i) { |
| 8619 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8620 | const Py_UCS4 up = Py_UNICODE_TOUPPER(ch); |
| 8621 | if (up != ch) { |
| 8622 | if (up > maxchar) |
| 8623 | maxchar = up; |
| 8624 | PyUnicode_WRITE(kind, data, i, up); |
| 8625 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8626 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8627 | else if (ch > maxchar) |
| 8628 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8629 | } |
| 8630 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8631 | if (touched) |
| 8632 | return maxchar; |
| 8633 | else |
| 8634 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8635 | } |
| 8636 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8637 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8638 | fixlower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8639 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8640 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8641 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8642 | const int kind = PyUnicode_KIND(self); |
| 8643 | void *data = PyUnicode_DATA(self); |
| 8644 | int touched = 0; |
| 8645 | Py_UCS4 maxchar = 0; |
| 8646 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8647 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8648 | for(i = 0; i < len; ++i) { |
| 8649 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8650 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8651 | if (lo != ch) { |
| 8652 | if (lo > maxchar) |
| 8653 | maxchar = lo; |
| 8654 | PyUnicode_WRITE(kind, data, i, lo); |
| 8655 | touched = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8656 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8657 | else if (ch > maxchar) |
| 8658 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8659 | } |
| 8660 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8661 | if (touched) |
| 8662 | return maxchar; |
| 8663 | else |
| 8664 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8665 | } |
| 8666 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8667 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8668 | fixswapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8669 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8670 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8671 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8672 | const int kind = PyUnicode_KIND(self); |
| 8673 | void *data = PyUnicode_DATA(self); |
| 8674 | int touched = 0; |
| 8675 | Py_UCS4 maxchar = 0; |
| 8676 | Py_ssize_t i; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8677 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8678 | for(i = 0; i < len; ++i) { |
| 8679 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8680 | Py_UCS4 nu = 0; |
| 8681 | |
| 8682 | if (Py_UNICODE_ISUPPER(ch)) |
| 8683 | nu = Py_UNICODE_TOLOWER(ch); |
| 8684 | else if (Py_UNICODE_ISLOWER(ch)) |
| 8685 | nu = Py_UNICODE_TOUPPER(ch); |
| 8686 | |
| 8687 | if (nu != 0) { |
| 8688 | if (nu > maxchar) |
| 8689 | maxchar = nu; |
| 8690 | PyUnicode_WRITE(kind, data, i, nu); |
| 8691 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8692 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8693 | else if (ch > maxchar) |
| 8694 | maxchar = ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8695 | } |
| 8696 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8697 | if (touched) |
| 8698 | return maxchar; |
| 8699 | else |
| 8700 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8701 | } |
| 8702 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8703 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8704 | fixcapitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8705 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8706 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8707 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8708 | const int kind = PyUnicode_KIND(self); |
| 8709 | void *data = PyUnicode_DATA(self); |
| 8710 | int touched = 0; |
| 8711 | Py_UCS4 maxchar = 0; |
| 8712 | Py_ssize_t i = 0; |
| 8713 | Py_UCS4 ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8714 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8715 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8716 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8717 | |
| 8718 | ch = PyUnicode_READ(kind, data, i); |
| 8719 | if (!Py_UNICODE_ISUPPER(ch)) { |
| 8720 | maxchar = Py_UNICODE_TOUPPER(ch); |
| 8721 | PyUnicode_WRITE(kind, data, i, maxchar); |
| 8722 | touched = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8723 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8724 | ++i; |
| 8725 | for(; i < len; ++i) { |
| 8726 | ch = PyUnicode_READ(kind, data, i); |
| 8727 | if (!Py_UNICODE_ISLOWER(ch)) { |
| 8728 | const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch); |
| 8729 | if (lo > maxchar) |
| 8730 | maxchar = lo; |
| 8731 | PyUnicode_WRITE(kind, data, i, lo); |
| 8732 | touched = 1; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8733 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8734 | else if (ch > maxchar) |
| 8735 | maxchar = ch; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 8736 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8737 | |
| 8738 | if (touched) |
| 8739 | return maxchar; |
| 8740 | else |
| 8741 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8742 | } |
| 8743 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8744 | static Py_UCS4 |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8745 | fixtitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8746 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8747 | /* No need to call PyUnicode_READY(self) because fixup() which does it. */ |
| 8748 | const Py_ssize_t len = PyUnicode_GET_LENGTH(self); |
| 8749 | const int kind = PyUnicode_KIND(self); |
| 8750 | void *data = PyUnicode_DATA(self); |
| 8751 | Py_UCS4 maxchar = 0; |
| 8752 | Py_ssize_t i = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8753 | int previous_is_cased; |
| 8754 | |
| 8755 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8756 | if (len == 1) { |
| 8757 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8758 | const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch); |
| 8759 | if (ti != ch) { |
| 8760 | PyUnicode_WRITE(kind, data, i, ti); |
| 8761 | return ti; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8762 | } |
| 8763 | else |
| 8764 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8765 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8766 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8767 | for(; i < len; ++i) { |
| 8768 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 8769 | Py_UCS4 nu; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8770 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8771 | if (previous_is_cased) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8772 | nu = Py_UNICODE_TOLOWER(ch); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8773 | else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8774 | nu = Py_UNICODE_TOTITLE(ch); |
| 8775 | |
| 8776 | if (nu > maxchar) |
| 8777 | maxchar = nu; |
| 8778 | PyUnicode_WRITE(kind, data, i, nu); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8779 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8780 | if (Py_UNICODE_ISLOWER(ch) || |
| 8781 | Py_UNICODE_ISUPPER(ch) || |
| 8782 | Py_UNICODE_ISTITLE(ch)) |
| 8783 | previous_is_cased = 1; |
| 8784 | else |
| 8785 | previous_is_cased = 0; |
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 | return maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8788 | } |
| 8789 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8790 | PyObject * |
| 8791 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8792 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8793 | PyObject *sep = NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8794 | Py_ssize_t seplen = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8795 | PyObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8796 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8797 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 8798 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8799 | PyObject *item; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8800 | Py_ssize_t sz, i, res_offset; |
| 8801 | Py_UCS4 maxchar = 0; |
| 8802 | Py_UCS4 item_maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8803 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8804 | fseq = PySequence_Fast(seq, ""); |
| 8805 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8806 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8807 | } |
| 8808 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8809 | /* NOTE: the following code can't call back into Python code, |
| 8810 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8811 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8812 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8813 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 8814 | /* If empty sequence, return u"". */ |
| 8815 | if (seqlen == 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8816 | res = PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8817 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8818 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8819 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8820 | /* If singleton sequence with an exact Unicode, return that. */ |
| 8821 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8822 | item = items[0]; |
| 8823 | if (PyUnicode_CheckExact(item)) { |
| 8824 | Py_INCREF(item); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8825 | res = item; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8826 | goto Done; |
| 8827 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8828 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8829 | else { |
| 8830 | /* Set up sep and seplen */ |
| 8831 | if (separator == NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8832 | /* fall back to a blank space separator */ |
| 8833 | sep = PyUnicode_FromOrdinal(' '); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 8834 | if (!sep) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8835 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8836 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8837 | else { |
| 8838 | if (!PyUnicode_Check(separator)) { |
| 8839 | PyErr_Format(PyExc_TypeError, |
| 8840 | "separator: expected str instance," |
| 8841 | " %.80s found", |
| 8842 | Py_TYPE(separator)->tp_name); |
| 8843 | goto onError; |
| 8844 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 8845 | if (PyUnicode_READY(separator)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8846 | goto onError; |
| 8847 | sep = separator; |
| 8848 | seplen = PyUnicode_GET_LENGTH(separator); |
| 8849 | maxchar = PyUnicode_MAX_CHAR_VALUE(separator); |
| 8850 | /* inc refcount to keep this code path symetric with the |
| 8851 | above case of a blank separator */ |
| 8852 | Py_INCREF(sep); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8853 | } |
| 8854 | } |
| 8855 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8856 | /* There are at least two things to join, or else we have a subclass |
| 8857 | * of str in the sequence. |
| 8858 | * Do a pre-pass to figure out the total amount of space we'll |
| 8859 | * need (sz), and see whether all argument are strings. |
| 8860 | */ |
| 8861 | sz = 0; |
| 8862 | for (i = 0; i < seqlen; i++) { |
| 8863 | const Py_ssize_t old_sz = sz; |
| 8864 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8865 | if (!PyUnicode_Check(item)) { |
| 8866 | PyErr_Format(PyExc_TypeError, |
| 8867 | "sequence item %zd: expected str instance," |
| 8868 | " %.80s found", |
| 8869 | i, Py_TYPE(item)->tp_name); |
| 8870 | goto onError; |
| 8871 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8872 | if (PyUnicode_READY(item) == -1) |
| 8873 | goto onError; |
| 8874 | sz += PyUnicode_GET_LENGTH(item); |
| 8875 | item_maxchar = PyUnicode_MAX_CHAR_VALUE(item); |
| 8876 | if (item_maxchar > maxchar) |
| 8877 | maxchar = item_maxchar; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8878 | if (i != 0) |
| 8879 | sz += seplen; |
| 8880 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 8881 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8882 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8883 | goto onError; |
| 8884 | } |
| 8885 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8886 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8887 | res = PyUnicode_New(sz, maxchar); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8888 | if (res == NULL) |
| 8889 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 8890 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8891 | /* Catenate everything. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8892 | for (i = 0, res_offset = 0; i < seqlen; ++i) { |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 8893 | Py_ssize_t itemlen; |
| 8894 | item = items[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8895 | itemlen = PyUnicode_GET_LENGTH(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8896 | /* Copy item, and maybe the separator. */ |
| 8897 | if (i) { |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8898 | if (PyUnicode_CopyCharacters(res, res_offset, |
| 8899 | sep, 0, seplen) < 0) |
| 8900 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8901 | res_offset += seplen; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8902 | } |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8903 | if (PyUnicode_CopyCharacters(res, res_offset, |
| 8904 | item, 0, itemlen) < 0) |
| 8905 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8906 | res_offset += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8907 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8908 | assert(res_offset == PyUnicode_GET_LENGTH(res)); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8909 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8910 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8911 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8912 | Py_XDECREF(sep); |
| 8913 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8914 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8915 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 8916 | Py_DECREF(fseq); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8917 | Py_XDECREF(sep); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 8918 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8919 | return NULL; |
| 8920 | } |
| 8921 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8922 | #define FILL(kind, data, value, start, length) \ |
| 8923 | do { \ |
| 8924 | Py_ssize_t i_ = 0; \ |
| 8925 | assert(kind != PyUnicode_WCHAR_KIND); \ |
| 8926 | switch ((kind)) { \ |
| 8927 | case PyUnicode_1BYTE_KIND: { \ |
| 8928 | unsigned char * to_ = (unsigned char *)((data)) + (start); \ |
| 8929 | memset(to_, (unsigned char)value, length); \ |
| 8930 | break; \ |
| 8931 | } \ |
| 8932 | case PyUnicode_2BYTE_KIND: { \ |
| 8933 | Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \ |
| 8934 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8935 | break; \ |
| 8936 | } \ |
| 8937 | default: { \ |
| 8938 | Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \ |
| 8939 | for (; i_ < (length); ++i_, ++to_) *to_ = (value); \ |
| 8940 | break; \ |
| 8941 | } \ |
| 8942 | } \ |
| 8943 | } while (0) |
| 8944 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8945 | static PyUnicodeObject * |
| 8946 | pad(PyUnicodeObject *self, |
| 8947 | Py_ssize_t left, |
| 8948 | Py_ssize_t right, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8949 | Py_UCS4 fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8950 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8951 | PyObject *u; |
| 8952 | Py_UCS4 maxchar; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8953 | int kind; |
| 8954 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8955 | |
| 8956 | if (left < 0) |
| 8957 | left = 0; |
| 8958 | if (right < 0) |
| 8959 | right = 0; |
| 8960 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8961 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8962 | Py_INCREF(self); |
| 8963 | return self; |
| 8964 | } |
| 8965 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8966 | if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) || |
| 8967 | right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) { |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 8968 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 8969 | return NULL; |
| 8970 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8971 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 8972 | if (fill > maxchar) |
| 8973 | maxchar = fill; |
| 8974 | u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8975 | if (!u) |
| 8976 | return NULL; |
| 8977 | |
| 8978 | kind = PyUnicode_KIND(u); |
| 8979 | data = PyUnicode_DATA(u); |
| 8980 | if (left) |
| 8981 | FILL(kind, data, fill, 0, left); |
| 8982 | if (right) |
| 8983 | FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 8984 | if (PyUnicode_CopyCharacters(u, left, |
| 8985 | (PyObject*)self, 0, |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 8986 | _PyUnicode_LENGTH(self)) < 0) |
| 8987 | { |
| 8988 | Py_DECREF(u); |
| 8989 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8990 | } |
| 8991 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8992 | return (PyUnicodeObject*)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8993 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 8994 | #undef FILL |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8995 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 8996 | PyObject * |
| 8997 | PyUnicode_Splitlines(PyObject *string, int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8998 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8999 | PyObject *list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9000 | |
| 9001 | string = PyUnicode_FromObject(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9002 | if (string == NULL || PyUnicode_READY(string) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9003 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9004 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9005 | switch(PyUnicode_KIND(string)) { |
| 9006 | case PyUnicode_1BYTE_KIND: |
| 9007 | list = ucs1lib_splitlines( |
| 9008 | (PyObject*) string, PyUnicode_1BYTE_DATA(string), |
| 9009 | PyUnicode_GET_LENGTH(string), keepends); |
| 9010 | break; |
| 9011 | case PyUnicode_2BYTE_KIND: |
| 9012 | list = ucs2lib_splitlines( |
| 9013 | (PyObject*) string, PyUnicode_2BYTE_DATA(string), |
| 9014 | PyUnicode_GET_LENGTH(string), keepends); |
| 9015 | break; |
| 9016 | case PyUnicode_4BYTE_KIND: |
| 9017 | list = ucs4lib_splitlines( |
| 9018 | (PyObject*) string, PyUnicode_4BYTE_DATA(string), |
| 9019 | PyUnicode_GET_LENGTH(string), keepends); |
| 9020 | break; |
| 9021 | default: |
| 9022 | assert(0); |
| 9023 | list = 0; |
| 9024 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9025 | Py_DECREF(string); |
| 9026 | return list; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9027 | } |
| 9028 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9029 | static PyObject * |
| 9030 | split(PyUnicodeObject *self, |
| 9031 | PyUnicodeObject *substring, |
| 9032 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9033 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9034 | int kind1, kind2, kind; |
| 9035 | void *buf1, *buf2; |
| 9036 | Py_ssize_t len1, len2; |
| 9037 | PyObject* out; |
| 9038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9039 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9040 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9041 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9042 | if (PyUnicode_READY(self) == -1) |
| 9043 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9044 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9045 | if (substring == NULL) |
| 9046 | switch(PyUnicode_KIND(self)) { |
| 9047 | case PyUnicode_1BYTE_KIND: |
| 9048 | return ucs1lib_split_whitespace( |
| 9049 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9050 | PyUnicode_GET_LENGTH(self), maxcount |
| 9051 | ); |
| 9052 | case PyUnicode_2BYTE_KIND: |
| 9053 | return ucs2lib_split_whitespace( |
| 9054 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9055 | PyUnicode_GET_LENGTH(self), maxcount |
| 9056 | ); |
| 9057 | case PyUnicode_4BYTE_KIND: |
| 9058 | return ucs4lib_split_whitespace( |
| 9059 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9060 | PyUnicode_GET_LENGTH(self), maxcount |
| 9061 | ); |
| 9062 | default: |
| 9063 | assert(0); |
| 9064 | return NULL; |
| 9065 | } |
| 9066 | |
| 9067 | if (PyUnicode_READY(substring) == -1) |
| 9068 | return NULL; |
| 9069 | |
| 9070 | kind1 = PyUnicode_KIND(self); |
| 9071 | kind2 = PyUnicode_KIND(substring); |
| 9072 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9073 | buf1 = PyUnicode_DATA(self); |
| 9074 | buf2 = PyUnicode_DATA(substring); |
| 9075 | if (kind1 != kind) |
| 9076 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9077 | if (!buf1) |
| 9078 | return NULL; |
| 9079 | if (kind2 != kind) |
| 9080 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9081 | if (!buf2) { |
| 9082 | if (kind1 != kind) PyMem_Free(buf1); |
| 9083 | return NULL; |
| 9084 | } |
| 9085 | len1 = PyUnicode_GET_LENGTH(self); |
| 9086 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9087 | |
| 9088 | switch(kind) { |
| 9089 | case PyUnicode_1BYTE_KIND: |
| 9090 | out = ucs1lib_split( |
| 9091 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9092 | break; |
| 9093 | case PyUnicode_2BYTE_KIND: |
| 9094 | out = ucs2lib_split( |
| 9095 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9096 | break; |
| 9097 | case PyUnicode_4BYTE_KIND: |
| 9098 | out = ucs4lib_split( |
| 9099 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9100 | break; |
| 9101 | default: |
| 9102 | out = NULL; |
| 9103 | } |
| 9104 | if (kind1 != kind) |
| 9105 | PyMem_Free(buf1); |
| 9106 | if (kind2 != kind) |
| 9107 | PyMem_Free(buf2); |
| 9108 | return out; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9109 | } |
| 9110 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9111 | static PyObject * |
| 9112 | rsplit(PyUnicodeObject *self, |
| 9113 | PyUnicodeObject *substring, |
| 9114 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9115 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9116 | int kind1, kind2, kind; |
| 9117 | void *buf1, *buf2; |
| 9118 | Py_ssize_t len1, len2; |
| 9119 | PyObject* out; |
| 9120 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9121 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9122 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9123 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9124 | if (PyUnicode_READY(self) == -1) |
| 9125 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9126 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9127 | if (substring == NULL) |
| 9128 | switch(PyUnicode_KIND(self)) { |
| 9129 | case PyUnicode_1BYTE_KIND: |
| 9130 | return ucs1lib_rsplit_whitespace( |
| 9131 | (PyObject*) self, PyUnicode_1BYTE_DATA(self), |
| 9132 | PyUnicode_GET_LENGTH(self), maxcount |
| 9133 | ); |
| 9134 | case PyUnicode_2BYTE_KIND: |
| 9135 | return ucs2lib_rsplit_whitespace( |
| 9136 | (PyObject*) self, PyUnicode_2BYTE_DATA(self), |
| 9137 | PyUnicode_GET_LENGTH(self), maxcount |
| 9138 | ); |
| 9139 | case PyUnicode_4BYTE_KIND: |
| 9140 | return ucs4lib_rsplit_whitespace( |
| 9141 | (PyObject*) self, PyUnicode_4BYTE_DATA(self), |
| 9142 | PyUnicode_GET_LENGTH(self), maxcount |
| 9143 | ); |
| 9144 | default: |
| 9145 | assert(0); |
| 9146 | return NULL; |
| 9147 | } |
| 9148 | |
| 9149 | if (PyUnicode_READY(substring) == -1) |
| 9150 | return NULL; |
| 9151 | |
| 9152 | kind1 = PyUnicode_KIND(self); |
| 9153 | kind2 = PyUnicode_KIND(substring); |
| 9154 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9155 | buf1 = PyUnicode_DATA(self); |
| 9156 | buf2 = PyUnicode_DATA(substring); |
| 9157 | if (kind1 != kind) |
| 9158 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 9159 | if (!buf1) |
| 9160 | return NULL; |
| 9161 | if (kind2 != kind) |
| 9162 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 9163 | if (!buf2) { |
| 9164 | if (kind1 != kind) PyMem_Free(buf1); |
| 9165 | return NULL; |
| 9166 | } |
| 9167 | len1 = PyUnicode_GET_LENGTH(self); |
| 9168 | len2 = PyUnicode_GET_LENGTH(substring); |
| 9169 | |
| 9170 | switch(kind) { |
| 9171 | case PyUnicode_1BYTE_KIND: |
| 9172 | out = ucs1lib_rsplit( |
| 9173 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9174 | break; |
| 9175 | case PyUnicode_2BYTE_KIND: |
| 9176 | out = ucs2lib_rsplit( |
| 9177 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9178 | break; |
| 9179 | case PyUnicode_4BYTE_KIND: |
| 9180 | out = ucs4lib_rsplit( |
| 9181 | (PyObject*) self, buf1, len1, buf2, len2, maxcount); |
| 9182 | break; |
| 9183 | default: |
| 9184 | out = NULL; |
| 9185 | } |
| 9186 | if (kind1 != kind) |
| 9187 | PyMem_Free(buf1); |
| 9188 | if (kind2 != kind) |
| 9189 | PyMem_Free(buf2); |
| 9190 | return out; |
| 9191 | } |
| 9192 | |
| 9193 | static Py_ssize_t |
| 9194 | anylib_find(int kind, void *buf1, Py_ssize_t len1, |
| 9195 | void *buf2, Py_ssize_t len2, Py_ssize_t offset) |
| 9196 | { |
| 9197 | switch(kind) { |
| 9198 | case PyUnicode_1BYTE_KIND: |
| 9199 | return ucs1lib_find(buf1, len1, buf2, len2, offset); |
| 9200 | case PyUnicode_2BYTE_KIND: |
| 9201 | return ucs2lib_find(buf1, len1, buf2, len2, offset); |
| 9202 | case PyUnicode_4BYTE_KIND: |
| 9203 | return ucs4lib_find(buf1, len1, buf2, len2, offset); |
| 9204 | } |
| 9205 | assert(0); |
| 9206 | return -1; |
| 9207 | } |
| 9208 | |
| 9209 | static Py_ssize_t |
| 9210 | anylib_count(int kind, void* sbuf, Py_ssize_t slen, |
| 9211 | void *buf1, Py_ssize_t len1, Py_ssize_t maxcount) |
| 9212 | { |
| 9213 | switch(kind) { |
| 9214 | case PyUnicode_1BYTE_KIND: |
| 9215 | return ucs1lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9216 | case PyUnicode_2BYTE_KIND: |
| 9217 | return ucs2lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9218 | case PyUnicode_4BYTE_KIND: |
| 9219 | return ucs4lib_count(sbuf, slen, buf1, len1, maxcount); |
| 9220 | } |
| 9221 | assert(0); |
| 9222 | return 0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 9223 | } |
| 9224 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9225 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9226 | replace(PyObject *self, PyObject *str1, |
| 9227 | PyObject *str2, Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9228 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9229 | PyObject *u; |
| 9230 | char *sbuf = PyUnicode_DATA(self); |
| 9231 | char *buf1 = PyUnicode_DATA(str1); |
| 9232 | char *buf2 = PyUnicode_DATA(str2); |
| 9233 | int srelease = 0, release1 = 0, release2 = 0; |
| 9234 | int skind = PyUnicode_KIND(self); |
| 9235 | int kind1 = PyUnicode_KIND(str1); |
| 9236 | int kind2 = PyUnicode_KIND(str2); |
| 9237 | Py_ssize_t slen = PyUnicode_GET_LENGTH(self); |
| 9238 | Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1); |
| 9239 | Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9240 | |
| 9241 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9242 | maxcount = PY_SSIZE_T_MAX; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9243 | else if (maxcount == 0 || slen == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9244 | goto nothing; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9245 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9246 | if (skind < kind1) |
| 9247 | /* substring too wide to be present */ |
| 9248 | goto nothing; |
| 9249 | |
| 9250 | if (len1 == len2) { |
Antoine Pitrou | cbfdee3 | 2010-01-13 08:58:08 +0000 | [diff] [blame] | 9251 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9252 | /* same length */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9253 | if (len1 == 0) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9254 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9255 | if (len1 == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9256 | /* replace characters */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9257 | Py_UCS4 u1, u2, maxchar; |
| 9258 | int mayshrink, rkind; |
| 9259 | u1 = PyUnicode_READ_CHAR(str1, 0); |
| 9260 | if (!findchar(sbuf, PyUnicode_KIND(self), |
| 9261 | slen, u1, 1)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9262 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9263 | u2 = PyUnicode_READ_CHAR(str2, 0); |
| 9264 | maxchar = PyUnicode_MAX_CHAR_VALUE(self); |
| 9265 | /* Replacing u1 with u2 may cause a maxchar reduction in the |
| 9266 | result string. */ |
| 9267 | mayshrink = maxchar > 127; |
| 9268 | if (u2 > maxchar) { |
| 9269 | maxchar = u2; |
| 9270 | mayshrink = 0; |
| 9271 | } |
| 9272 | u = PyUnicode_New(slen, maxchar); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9273 | if (!u) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9274 | goto error; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9275 | if (PyUnicode_CopyCharacters(u, 0, |
| 9276 | (PyObject*)self, 0, slen) < 0) |
| 9277 | { |
| 9278 | Py_DECREF(u); |
| 9279 | return NULL; |
| 9280 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9281 | rkind = PyUnicode_KIND(u); |
| 9282 | for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) |
| 9283 | if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9284 | if (--maxcount < 0) |
| 9285 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9286 | PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9287 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9288 | if (mayshrink) { |
| 9289 | PyObject *tmp = u; |
| 9290 | u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp), |
| 9291 | PyUnicode_GET_LENGTH(tmp)); |
| 9292 | Py_DECREF(tmp); |
| 9293 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9294 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9295 | int rkind = skind; |
| 9296 | char *res; |
| 9297 | if (kind1 < rkind) { |
| 9298 | /* widen substring */ |
| 9299 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9300 | if (!buf1) goto error; |
| 9301 | release1 = 1; |
| 9302 | } |
| 9303 | i = anylib_find(rkind, sbuf, slen, buf1, len1, 0); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9304 | if (i < 0) |
| 9305 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9306 | if (rkind > kind2) { |
| 9307 | /* widen replacement */ |
| 9308 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9309 | if (!buf2) goto error; |
| 9310 | release2 = 1; |
| 9311 | } |
| 9312 | else if (rkind < kind2) { |
| 9313 | /* widen self and buf1 */ |
| 9314 | rkind = kind2; |
| 9315 | if (release1) PyMem_Free(buf1); |
| 9316 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9317 | if (!sbuf) goto error; |
| 9318 | srelease = 1; |
| 9319 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9320 | if (!buf1) goto error; |
| 9321 | release1 = 1; |
| 9322 | } |
| 9323 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen)); |
| 9324 | if (!res) { |
| 9325 | PyErr_NoMemory(); |
| 9326 | goto error; |
| 9327 | } |
| 9328 | memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen)); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9329 | /* change everything in-place, starting with this one */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9330 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9331 | buf2, |
| 9332 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9333 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9334 | |
| 9335 | while ( --maxcount > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9336 | i = anylib_find(rkind, sbuf+PyUnicode_KIND_SIZE(rkind, i), |
| 9337 | slen-i, |
| 9338 | buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9339 | if (i == -1) |
| 9340 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9341 | memcpy(res + PyUnicode_KIND_SIZE(rkind, i), |
| 9342 | buf2, |
| 9343 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9344 | i += len1; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9345 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9346 | |
| 9347 | u = PyUnicode_FromKindAndData(rkind, res, slen); |
| 9348 | PyMem_Free(res); |
| 9349 | if (!u) goto error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9350 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9351 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9352 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9353 | Py_ssize_t n, i, j, ires; |
| 9354 | Py_ssize_t product, new_size; |
| 9355 | int rkind = skind; |
| 9356 | char *res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9357 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9358 | if (kind1 < rkind) { |
| 9359 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9360 | if (!buf1) goto error; |
| 9361 | release1 = 1; |
| 9362 | } |
| 9363 | n = anylib_count(rkind, sbuf, slen, buf1, len1, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9364 | if (n == 0) |
| 9365 | goto nothing; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9366 | if (kind2 < rkind) { |
| 9367 | buf2 = _PyUnicode_AsKind(str2, rkind); |
| 9368 | if (!buf2) goto error; |
| 9369 | release2 = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9370 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9371 | else if (kind2 > rkind) { |
| 9372 | rkind = kind2; |
| 9373 | sbuf = _PyUnicode_AsKind(self, rkind); |
| 9374 | if (!sbuf) goto error; |
| 9375 | srelease = 1; |
| 9376 | if (release1) PyMem_Free(buf1); |
| 9377 | buf1 = _PyUnicode_AsKind(str1, rkind); |
| 9378 | if (!buf1) goto error; |
| 9379 | release1 = 1; |
| 9380 | } |
| 9381 | /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) - |
| 9382 | PyUnicode_GET_LENGTH(str1))); */ |
| 9383 | product = n * (len2-len1); |
| 9384 | if ((product / (len2-len1)) != n) { |
| 9385 | PyErr_SetString(PyExc_OverflowError, |
| 9386 | "replace string is too long"); |
| 9387 | goto error; |
| 9388 | } |
| 9389 | new_size = slen + product; |
| 9390 | if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) { |
| 9391 | PyErr_SetString(PyExc_OverflowError, |
| 9392 | "replace string is too long"); |
| 9393 | goto error; |
| 9394 | } |
| 9395 | res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size)); |
| 9396 | if (!res) |
| 9397 | goto error; |
| 9398 | ires = i = 0; |
| 9399 | if (len1 > 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9400 | while (n-- > 0) { |
| 9401 | /* look for next match */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9402 | j = anylib_find(rkind, |
| 9403 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9404 | slen-i, buf1, len1, i); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 9405 | if (j == -1) |
| 9406 | break; |
| 9407 | else if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9408 | /* copy unchanged part [i:j] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9409 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9410 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9411 | PyUnicode_KIND_SIZE(rkind, j-i)); |
| 9412 | ires += j - i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9413 | } |
| 9414 | /* copy substitution string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9415 | if (len2 > 0) { |
| 9416 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9417 | buf2, |
| 9418 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9419 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9420 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9421 | i = j + len1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9422 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9423 | if (i < slen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9424 | /* copy tail [i:] */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9425 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9426 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9427 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9428 | } else { |
| 9429 | /* interleave */ |
| 9430 | while (n > 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9431 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9432 | buf2, |
| 9433 | PyUnicode_KIND_SIZE(rkind, len2)); |
| 9434 | ires += len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9435 | if (--n <= 0) |
| 9436 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9437 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9438 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9439 | PyUnicode_KIND_SIZE(rkind, 1)); |
| 9440 | ires++; |
| 9441 | i++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9442 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9443 | memcpy(res + PyUnicode_KIND_SIZE(rkind, ires), |
| 9444 | sbuf + PyUnicode_KIND_SIZE(rkind, i), |
| 9445 | PyUnicode_KIND_SIZE(rkind, slen-i)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9446 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9447 | u = PyUnicode_FromKindAndData(rkind, res, new_size); |
Martin v. Löwis | 0b1d348 | 2011-10-01 16:35:40 +0200 | [diff] [blame] | 9448 | PyMem_Free(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9449 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9450 | if (srelease) |
| 9451 | PyMem_FREE(sbuf); |
| 9452 | if (release1) |
| 9453 | PyMem_FREE(buf1); |
| 9454 | if (release2) |
| 9455 | PyMem_FREE(buf2); |
| 9456 | return u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9457 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9458 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9459 | /* nothing to replace; return original string (when possible) */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9460 | if (srelease) |
| 9461 | PyMem_FREE(sbuf); |
| 9462 | if (release1) |
| 9463 | PyMem_FREE(buf1); |
| 9464 | if (release2) |
| 9465 | PyMem_FREE(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9466 | if (PyUnicode_CheckExact(self)) { |
| 9467 | Py_INCREF(self); |
| 9468 | return (PyObject *) self; |
| 9469 | } |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 9470 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9471 | error: |
| 9472 | if (srelease && sbuf) |
| 9473 | PyMem_FREE(sbuf); |
| 9474 | if (release1 && buf1) |
| 9475 | PyMem_FREE(buf1); |
| 9476 | if (release2 && buf2) |
| 9477 | PyMem_FREE(buf2); |
| 9478 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9479 | } |
| 9480 | |
| 9481 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 9482 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9483 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9484 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9485 | \n\ |
| 9486 | 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] | 9487 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9488 | |
| 9489 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9490 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9491 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9492 | return fixup(self, fixtitle); |
| 9493 | } |
| 9494 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9495 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9496 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9497 | \n\ |
| 9498 | 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] | 9499 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9500 | |
| 9501 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9502 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9503 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9504 | return fixup(self, fixcapitalize); |
| 9505 | } |
| 9506 | |
| 9507 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9508 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9509 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9510 | \n\ |
| 9511 | 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] | 9512 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9513 | |
| 9514 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 9515 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9516 | { |
| 9517 | PyObject *list; |
| 9518 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9519 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9521 | /* Split into words */ |
| 9522 | list = split(self, NULL, -1); |
| 9523 | if (!list) |
| 9524 | return NULL; |
| 9525 | |
| 9526 | /* Capitalize each word */ |
| 9527 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 9528 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9529 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9530 | if (item == NULL) |
| 9531 | goto onError; |
| 9532 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 9533 | PyList_SET_ITEM(list, i, item); |
| 9534 | } |
| 9535 | |
| 9536 | /* Join the words to form a new string */ |
| 9537 | item = PyUnicode_Join(NULL, list); |
| 9538 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9539 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9540 | Py_DECREF(list); |
| 9541 | return (PyObject *)item; |
| 9542 | } |
| 9543 | #endif |
| 9544 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9545 | /* Argument converter. Coerces to a single unicode character */ |
| 9546 | |
| 9547 | static int |
| 9548 | convert_uc(PyObject *obj, void *addr) |
| 9549 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9550 | Py_UCS4 *fillcharloc = (Py_UCS4 *)addr; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9551 | PyObject *uniobj; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9552 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9553 | uniobj = PyUnicode_FromObject(obj); |
| 9554 | if (uniobj == NULL) { |
| 9555 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9556 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9557 | return 0; |
| 9558 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9559 | if (PyUnicode_GET_LENGTH(uniobj) != 1) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9560 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9561 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9562 | Py_DECREF(uniobj); |
| 9563 | return 0; |
| 9564 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9565 | *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9566 | Py_DECREF(uniobj); |
| 9567 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9568 | } |
| 9569 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9570 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9571 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9572 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9573 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9574 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9575 | |
| 9576 | static PyObject * |
| 9577 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 9578 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9579 | Py_ssize_t marg, left; |
| 9580 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9581 | Py_UCS4 fillchar = ' '; |
| 9582 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9583 | 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] | 9584 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9585 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9586 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9587 | return NULL; |
| 9588 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9589 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9590 | Py_INCREF(self); |
| 9591 | return (PyObject*) self; |
| 9592 | } |
| 9593 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9594 | marg = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9595 | left = marg / 2 + (marg & width & 1); |
| 9596 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 9597 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9598 | } |
| 9599 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9600 | #if 0 |
| 9601 | |
| 9602 | /* This code should go into some future Unicode collation support |
| 9603 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 9604 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9605 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9606 | /* speedy UTF-16 code point order comparison */ |
| 9607 | /* gleaned from: */ |
| 9608 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 9609 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9610 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9611 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9612 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9613 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 9614 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 9615 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9616 | }; |
| 9617 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9618 | static int |
| 9619 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9620 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9621 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9622 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9623 | Py_UNICODE *s1 = str1->str; |
| 9624 | Py_UNICODE *s2 = str2->str; |
| 9625 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9626 | len1 = str1->_base._base.length; |
| 9627 | len2 = str2->_base._base.length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9629 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9630 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9631 | |
| 9632 | c1 = *s1++; |
| 9633 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9634 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9635 | if (c1 > (1<<11) * 26) |
| 9636 | c1 += utf16Fixup[c1>>11]; |
| 9637 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9638 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9639 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9640 | |
| 9641 | if (c1 != c2) |
| 9642 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9643 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 9644 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9645 | } |
| 9646 | |
| 9647 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9648 | } |
| 9649 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9650 | #else |
| 9651 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9652 | /* This function assumes that str1 and str2 are readied by the caller. */ |
| 9653 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9654 | static int |
| 9655 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 9656 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9657 | int kind1, kind2; |
| 9658 | void *data1, *data2; |
| 9659 | Py_ssize_t len1, len2, i; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9660 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9661 | kind1 = PyUnicode_KIND(str1); |
| 9662 | kind2 = PyUnicode_KIND(str2); |
| 9663 | data1 = PyUnicode_DATA(str1); |
| 9664 | data2 = PyUnicode_DATA(str2); |
| 9665 | len1 = PyUnicode_GET_LENGTH(str1); |
| 9666 | len2 = PyUnicode_GET_LENGTH(str2); |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9667 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9668 | for (i = 0; i < len1 && i < len2; ++i) { |
| 9669 | Py_UCS4 c1, c2; |
| 9670 | c1 = PyUnicode_READ(kind1, data1, i); |
| 9671 | c2 = PyUnicode_READ(kind2, data2, i); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 9672 | |
| 9673 | if (c1 != c2) |
| 9674 | return (c1 < c2) ? -1 : 1; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 9675 | } |
| 9676 | |
| 9677 | return (len1 < len2) ? -1 : (len1 != len2); |
| 9678 | } |
| 9679 | |
| 9680 | #endif |
| 9681 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9682 | int |
| 9683 | PyUnicode_Compare(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9684 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9685 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9686 | if (PyUnicode_READY(left) == -1 || |
| 9687 | PyUnicode_READY(right) == -1) |
| 9688 | return -1; |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9689 | return unicode_compare((PyUnicodeObject *)left, |
| 9690 | (PyUnicodeObject *)right); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9691 | } |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 9692 | PyErr_Format(PyExc_TypeError, |
| 9693 | "Can't compare %.100s and %.100s", |
| 9694 | left->ob_type->tp_name, |
| 9695 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9696 | return -1; |
| 9697 | } |
| 9698 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9699 | int |
| 9700 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 9701 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9702 | Py_ssize_t i; |
| 9703 | int kind; |
| 9704 | void *data; |
| 9705 | Py_UCS4 chr; |
| 9706 | |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 9707 | assert(_PyUnicode_CHECK(uni)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9708 | if (PyUnicode_READY(uni) == -1) |
| 9709 | return -1; |
| 9710 | kind = PyUnicode_KIND(uni); |
| 9711 | data = PyUnicode_DATA(uni); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9712 | /* Compare Unicode string and source character set string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9713 | for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++) |
| 9714 | if (chr != str[i]) |
| 9715 | return (chr < (unsigned char)(str[i])) ? -1 : 1; |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 9716 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 9717 | to C strings identical up to that point. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9718 | if (PyUnicode_GET_LENGTH(uni) != i || chr) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9719 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9720 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9721 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9722 | return 0; |
| 9723 | } |
| 9724 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9725 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9726 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9727 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9728 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9729 | PyObject * |
| 9730 | PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9731 | { |
| 9732 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9733 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9734 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 9735 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9736 | if (PyUnicode_READY(left) == -1 || |
| 9737 | PyUnicode_READY(right) == -1) |
| 9738 | return NULL; |
| 9739 | if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) || |
| 9740 | PyUnicode_KIND(left) != PyUnicode_KIND(right)) { |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9741 | if (op == Py_EQ) { |
| 9742 | Py_INCREF(Py_False); |
| 9743 | return Py_False; |
| 9744 | } |
| 9745 | if (op == Py_NE) { |
| 9746 | Py_INCREF(Py_True); |
| 9747 | return Py_True; |
| 9748 | } |
| 9749 | } |
| 9750 | if (left == right) |
| 9751 | result = 0; |
| 9752 | else |
| 9753 | result = unicode_compare((PyUnicodeObject *)left, |
| 9754 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9755 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 9756 | /* Convert the return value to a Boolean */ |
| 9757 | switch (op) { |
| 9758 | case Py_EQ: |
| 9759 | v = TEST_COND(result == 0); |
| 9760 | break; |
| 9761 | case Py_NE: |
| 9762 | v = TEST_COND(result != 0); |
| 9763 | break; |
| 9764 | case Py_LE: |
| 9765 | v = TEST_COND(result <= 0); |
| 9766 | break; |
| 9767 | case Py_GE: |
| 9768 | v = TEST_COND(result >= 0); |
| 9769 | break; |
| 9770 | case Py_LT: |
| 9771 | v = TEST_COND(result == -1); |
| 9772 | break; |
| 9773 | case Py_GT: |
| 9774 | v = TEST_COND(result == 1); |
| 9775 | break; |
| 9776 | default: |
| 9777 | PyErr_BadArgument(); |
| 9778 | return NULL; |
| 9779 | } |
| 9780 | Py_INCREF(v); |
| 9781 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9782 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9783 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 9784 | Py_RETURN_NOTIMPLEMENTED; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9785 | } |
| 9786 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9787 | int |
| 9788 | PyUnicode_Contains(PyObject *container, PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9789 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9790 | PyObject *str, *sub; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9791 | int kind1, kind2, kind; |
| 9792 | void *buf1, *buf2; |
| 9793 | Py_ssize_t len1, len2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9794 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9795 | |
| 9796 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9797 | sub = PyUnicode_FromObject(element); |
| 9798 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9799 | PyErr_Format(PyExc_TypeError, |
| 9800 | "'in <string>' requires string as left operand, not %s", |
| 9801 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9802 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9803 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9804 | if (PyUnicode_READY(sub) == -1) |
| 9805 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9806 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9807 | str = PyUnicode_FromObject(container); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 9808 | if (!str || PyUnicode_READY(str) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9809 | Py_DECREF(sub); |
| 9810 | return -1; |
| 9811 | } |
| 9812 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9813 | kind1 = PyUnicode_KIND(str); |
| 9814 | kind2 = PyUnicode_KIND(sub); |
| 9815 | kind = kind1 > kind2 ? kind1 : kind2; |
| 9816 | buf1 = PyUnicode_DATA(str); |
| 9817 | buf2 = PyUnicode_DATA(sub); |
| 9818 | if (kind1 != kind) |
| 9819 | buf1 = _PyUnicode_AsKind((PyObject*)str, kind); |
| 9820 | if (!buf1) { |
| 9821 | Py_DECREF(sub); |
| 9822 | return -1; |
| 9823 | } |
| 9824 | if (kind2 != kind) |
| 9825 | buf2 = _PyUnicode_AsKind((PyObject*)sub, kind); |
| 9826 | if (!buf2) { |
| 9827 | Py_DECREF(sub); |
| 9828 | if (kind1 != kind) PyMem_Free(buf1); |
| 9829 | return -1; |
| 9830 | } |
| 9831 | len1 = PyUnicode_GET_LENGTH(str); |
| 9832 | len2 = PyUnicode_GET_LENGTH(sub); |
| 9833 | |
| 9834 | switch(kind) { |
| 9835 | case PyUnicode_1BYTE_KIND: |
| 9836 | result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9837 | break; |
| 9838 | case PyUnicode_2BYTE_KIND: |
| 9839 | result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9840 | break; |
| 9841 | case PyUnicode_4BYTE_KIND: |
| 9842 | result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1; |
| 9843 | break; |
| 9844 | default: |
| 9845 | result = -1; |
| 9846 | assert(0); |
| 9847 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9848 | |
| 9849 | Py_DECREF(str); |
| 9850 | Py_DECREF(sub); |
| 9851 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9852 | if (kind1 != kind) |
| 9853 | PyMem_Free(buf1); |
| 9854 | if (kind2 != kind) |
| 9855 | PyMem_Free(buf2); |
| 9856 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9857 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 9858 | } |
| 9859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9860 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 9861 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 9862 | PyObject * |
| 9863 | PyUnicode_Concat(PyObject *left, PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9864 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9865 | PyObject *u = NULL, *v = NULL, *w; |
| 9866 | Py_UCS4 maxchar; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9867 | |
| 9868 | /* Coerce the two arguments */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9869 | u = PyUnicode_FromObject(left); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9870 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9871 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9872 | v = PyUnicode_FromObject(right); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9873 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9874 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9875 | |
| 9876 | /* Shortcuts */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9877 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9878 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9879 | return u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9880 | } |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 9881 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9882 | Py_DECREF(u); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9883 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9884 | } |
| 9885 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9886 | maxchar = PyUnicode_MAX_CHAR_VALUE(u); |
Victor Stinner | ff9e50f | 2011-09-28 22:17:19 +0200 | [diff] [blame] | 9887 | maxchar = Py_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9888 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9889 | /* Concat the two Unicode strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9890 | w = PyUnicode_New( |
| 9891 | PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v), |
| 9892 | maxchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9893 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9894 | goto onError; |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9895 | if (PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)) < 0) |
| 9896 | goto onError; |
Victor Stinner | 157f83f | 2011-09-28 21:41:31 +0200 | [diff] [blame] | 9897 | if (PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 9898 | v, 0, |
| 9899 | PyUnicode_GET_LENGTH(v)) < 0) |
| 9900 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9901 | Py_DECREF(u); |
| 9902 | Py_DECREF(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 9903 | return w; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9904 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9905 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9906 | Py_XDECREF(u); |
| 9907 | Py_XDECREF(v); |
| 9908 | return NULL; |
| 9909 | } |
| 9910 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9911 | void |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9912 | PyUnicode_Append(PyObject **p_left, PyObject *right) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9913 | { |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9914 | PyObject *left, *res; |
| 9915 | |
| 9916 | if (p_left == NULL) { |
| 9917 | if (!PyErr_Occurred()) |
| 9918 | PyErr_BadInternalCall(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9919 | return; |
| 9920 | } |
Victor Stinner | 23e5668 | 2011-10-03 03:54:37 +0200 | [diff] [blame] | 9921 | left = *p_left; |
| 9922 | if (right == NULL || !PyUnicode_Check(left)) { |
| 9923 | if (!PyErr_Occurred()) |
| 9924 | PyErr_BadInternalCall(); |
| 9925 | goto error; |
| 9926 | } |
| 9927 | |
| 9928 | if (PyUnicode_CheckExact(left) && left != unicode_empty |
| 9929 | && PyUnicode_CheckExact(right) && right != unicode_empty |
| 9930 | && unicode_resizable(left) |
| 9931 | && (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left) |
| 9932 | || _PyUnicode_WSTR(left) != NULL)) |
| 9933 | { |
| 9934 | Py_ssize_t u_len, v_len, new_len, copied; |
| 9935 | |
| 9936 | /* FIXME: don't make wstr string ready */ |
| 9937 | if (PyUnicode_READY(left)) |
| 9938 | goto error; |
| 9939 | if (PyUnicode_READY(right)) |
| 9940 | goto error; |
| 9941 | |
| 9942 | /* FIXME: support ascii+latin1, PyASCIIObject => PyCompactUnicodeObject */ |
| 9943 | if (PyUnicode_MAX_CHAR_VALUE(right) <= PyUnicode_MAX_CHAR_VALUE(left)) |
| 9944 | { |
| 9945 | u_len = PyUnicode_GET_LENGTH(left); |
| 9946 | v_len = PyUnicode_GET_LENGTH(right); |
| 9947 | if (u_len > PY_SSIZE_T_MAX - v_len) { |
| 9948 | PyErr_SetString(PyExc_OverflowError, |
| 9949 | "strings are too large to concat"); |
| 9950 | goto error; |
| 9951 | } |
| 9952 | new_len = u_len + v_len; |
| 9953 | |
| 9954 | /* Now we own the last reference to 'left', so we can resize it |
| 9955 | * in-place. |
| 9956 | */ |
| 9957 | if (unicode_resize(&left, new_len) != 0) { |
| 9958 | /* XXX if _PyUnicode_Resize() fails, 'left' has been |
| 9959 | * deallocated so it cannot be put back into |
| 9960 | * 'variable'. The MemoryError is raised when there |
| 9961 | * is no value in 'variable', which might (very |
| 9962 | * remotely) be a cause of incompatibilities. |
| 9963 | */ |
| 9964 | goto error; |
| 9965 | } |
| 9966 | /* copy 'right' into the newly allocated area of 'left' */ |
| 9967 | copied = PyUnicode_CopyCharacters(left, u_len, |
| 9968 | right, 0, |
| 9969 | v_len); |
| 9970 | assert(0 <= copied); |
| 9971 | *p_left = left; |
| 9972 | return; |
| 9973 | } |
| 9974 | } |
| 9975 | |
| 9976 | res = PyUnicode_Concat(left, right); |
| 9977 | if (res == NULL) |
| 9978 | goto error; |
| 9979 | Py_DECREF(left); |
| 9980 | *p_left = res; |
| 9981 | return; |
| 9982 | |
| 9983 | error: |
| 9984 | Py_DECREF(*p_left); |
| 9985 | *p_left = NULL; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9986 | } |
| 9987 | |
| 9988 | void |
| 9989 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 9990 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9991 | PyUnicode_Append(pleft, right); |
| 9992 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 9993 | } |
| 9994 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9995 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9996 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9997 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9998 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 9999 | 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] | 10000 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10001 | |
| 10002 | static PyObject * |
| 10003 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 10004 | { |
| 10005 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10006 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10007 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10008 | PyObject *result; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10009 | int kind1, kind2, kind; |
| 10010 | void *buf1, *buf2; |
| 10011 | Py_ssize_t len1, len2, iresult; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10012 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10013 | if (!stringlib_parse_args_finds_unicode("count", args, &substring, |
| 10014 | &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10015 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10016 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10017 | kind1 = PyUnicode_KIND(self); |
| 10018 | kind2 = PyUnicode_KIND(substring); |
| 10019 | kind = kind1 > kind2 ? kind1 : kind2; |
| 10020 | buf1 = PyUnicode_DATA(self); |
| 10021 | buf2 = PyUnicode_DATA(substring); |
| 10022 | if (kind1 != kind) |
| 10023 | buf1 = _PyUnicode_AsKind((PyObject*)self, kind); |
| 10024 | if (!buf1) { |
| 10025 | Py_DECREF(substring); |
| 10026 | return NULL; |
| 10027 | } |
| 10028 | if (kind2 != kind) |
| 10029 | buf2 = _PyUnicode_AsKind((PyObject*)substring, kind); |
| 10030 | if (!buf2) { |
| 10031 | Py_DECREF(substring); |
| 10032 | if (kind1 != kind) PyMem_Free(buf1); |
| 10033 | return NULL; |
| 10034 | } |
| 10035 | len1 = PyUnicode_GET_LENGTH(self); |
| 10036 | len2 = PyUnicode_GET_LENGTH(substring); |
| 10037 | |
| 10038 | ADJUST_INDICES(start, end, len1); |
| 10039 | switch(kind) { |
| 10040 | case PyUnicode_1BYTE_KIND: |
| 10041 | iresult = ucs1lib_count( |
| 10042 | ((Py_UCS1*)buf1) + start, end - start, |
| 10043 | buf2, len2, PY_SSIZE_T_MAX |
| 10044 | ); |
| 10045 | break; |
| 10046 | case PyUnicode_2BYTE_KIND: |
| 10047 | iresult = ucs2lib_count( |
| 10048 | ((Py_UCS2*)buf1) + start, end - start, |
| 10049 | buf2, len2, PY_SSIZE_T_MAX |
| 10050 | ); |
| 10051 | break; |
| 10052 | case PyUnicode_4BYTE_KIND: |
| 10053 | iresult = ucs4lib_count( |
| 10054 | ((Py_UCS4*)buf1) + start, end - start, |
| 10055 | buf2, len2, PY_SSIZE_T_MAX |
| 10056 | ); |
| 10057 | break; |
| 10058 | default: |
| 10059 | assert(0); iresult = 0; |
| 10060 | } |
| 10061 | |
| 10062 | result = PyLong_FromSsize_t(iresult); |
| 10063 | |
| 10064 | if (kind1 != kind) |
| 10065 | PyMem_Free(buf1); |
| 10066 | if (kind2 != kind) |
| 10067 | PyMem_Free(buf2); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10068 | |
| 10069 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10070 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10071 | return result; |
| 10072 | } |
| 10073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10074 | PyDoc_STRVAR(encode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 10075 | "S.encode(encoding='utf-8', errors='strict') -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10076 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 10077 | Encode S using the codec registered for encoding. Default encoding\n\ |
| 10078 | 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] | 10079 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 10080 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 10081 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 10082 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10083 | |
| 10084 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10085 | unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10086 | { |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10087 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10088 | char *encoding = NULL; |
| 10089 | char *errors = NULL; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 10090 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 10091 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
| 10092 | kwlist, &encoding, &errors)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10093 | return NULL; |
Georg Brandl | 3b9406b | 2010-12-03 07:54:09 +0000 | [diff] [blame] | 10094 | return PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 10095 | } |
| 10096 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10097 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10098 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10099 | \n\ |
| 10100 | 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] | 10101 | 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] | 10102 | |
| 10103 | static PyObject* |
| 10104 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 10105 | { |
| 10106 | Py_UNICODE *e; |
| 10107 | Py_UNICODE *p; |
| 10108 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10109 | Py_UNICODE *qe; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10110 | Py_ssize_t i, j, incr, wstr_length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10111 | PyUnicodeObject *u; |
| 10112 | int tabsize = 8; |
| 10113 | |
| 10114 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10115 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10116 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10117 | if (PyUnicode_AsUnicodeAndSize((PyObject *)self, &wstr_length) == NULL) |
| 10118 | return NULL; |
| 10119 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10120 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10121 | i = 0; /* chars up to and including most recent \n or \r */ |
| 10122 | 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] | 10123 | e = _PyUnicode_WSTR(self) + wstr_length; /* end of input */ |
| 10124 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10125 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10126 | if (tabsize > 0) { |
| 10127 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 10128 | if (j > PY_SSIZE_T_MAX - incr) |
| 10129 | goto overflow1; |
| 10130 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10131 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10132 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10133 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10134 | if (j > PY_SSIZE_T_MAX - 1) |
| 10135 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10136 | j++; |
| 10137 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10138 | if (i > PY_SSIZE_T_MAX - j) |
| 10139 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10140 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10141 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10142 | } |
| 10143 | } |
| 10144 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10145 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10146 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10147 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10148 | /* Second pass: create output string and fill it */ |
| 10149 | u = _PyUnicode_New(i + j); |
| 10150 | if (!u) |
| 10151 | return NULL; |
| 10152 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10153 | j = 0; /* same as in first pass */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10154 | q = _PyUnicode_WSTR(u); /* next output char */ |
| 10155 | qe = _PyUnicode_WSTR(u) + PyUnicode_GET_SIZE(u); /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10156 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10157 | for (p = _PyUnicode_WSTR(self); p < e; p++) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10158 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10159 | if (tabsize > 0) { |
| 10160 | i = tabsize - (j % tabsize); |
| 10161 | j += i; |
| 10162 | while (i--) { |
| 10163 | if (q >= qe) |
| 10164 | goto overflow2; |
| 10165 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10166 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10167 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10168 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10169 | else { |
| 10170 | if (q >= qe) |
| 10171 | goto overflow2; |
| 10172 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10173 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10174 | if (*p == '\n' || *p == '\r') |
| 10175 | j = 0; |
| 10176 | } |
| 10177 | |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 10178 | if (_PyUnicode_READY_REPLACE(&u)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10179 | Py_DECREF(u); |
| 10180 | return NULL; |
| 10181 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10182 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10183 | |
| 10184 | overflow2: |
| 10185 | Py_DECREF(u); |
| 10186 | overflow1: |
| 10187 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 10188 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10189 | } |
| 10190 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10191 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10192 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10193 | \n\ |
| 10194 | Return the lowest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 10195 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10196 | arguments start and end are interpreted as in slice notation.\n\ |
| 10197 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10198 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10199 | |
| 10200 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10201 | unicode_find(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10202 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10203 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10204 | Py_ssize_t start; |
| 10205 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10206 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10207 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10208 | if (!stringlib_parse_args_finds_unicode("find", args, &substring, |
| 10209 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10210 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10211 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10212 | if (PyUnicode_READY(self) == -1) |
| 10213 | return NULL; |
| 10214 | if (PyUnicode_READY(substring) == -1) |
| 10215 | return NULL; |
| 10216 | |
| 10217 | result = any_find_slice( |
| 10218 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 10219 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10220 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10221 | |
| 10222 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10223 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10224 | if (result == -2) |
| 10225 | return NULL; |
| 10226 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10227 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10228 | } |
| 10229 | |
| 10230 | static PyObject * |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10231 | unicode_getitem(PyObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10232 | { |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10233 | Py_UCS4 ch = PyUnicode_ReadChar(self, index); |
| 10234 | if (ch == (Py_UCS4)-1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10235 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10236 | return PyUnicode_FromOrdinal(ch); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10237 | } |
| 10238 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10239 | /* Believe it or not, this produces the same value for ASCII strings |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10240 | as bytes_hash(). */ |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 10241 | static Py_hash_t |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 10242 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10243 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10244 | Py_ssize_t len; |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 10245 | Py_uhash_t x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10246 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10247 | if (_PyUnicode_HASH(self) != -1) |
| 10248 | return _PyUnicode_HASH(self); |
| 10249 | if (PyUnicode_READY(self) == -1) |
| 10250 | return -1; |
| 10251 | len = PyUnicode_GET_LENGTH(self); |
| 10252 | |
| 10253 | /* The hash function as a macro, gets expanded three times below. */ |
| 10254 | #define HASH(P) \ |
| 10255 | x = (Py_uhash_t)*P << 7; \ |
| 10256 | while (--len >= 0) \ |
| 10257 | x = (1000003*x) ^ (Py_uhash_t)*P++; |
| 10258 | |
| 10259 | switch (PyUnicode_KIND(self)) { |
| 10260 | case PyUnicode_1BYTE_KIND: { |
| 10261 | const unsigned char *c = PyUnicode_1BYTE_DATA(self); |
| 10262 | HASH(c); |
| 10263 | break; |
| 10264 | } |
| 10265 | case PyUnicode_2BYTE_KIND: { |
| 10266 | const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self); |
| 10267 | HASH(s); |
| 10268 | break; |
| 10269 | } |
| 10270 | default: { |
| 10271 | Py_UCS4 *l; |
| 10272 | assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND && |
| 10273 | "Impossible switch case in unicode_hash"); |
| 10274 | l = PyUnicode_4BYTE_DATA(self); |
| 10275 | HASH(l); |
| 10276 | break; |
| 10277 | } |
| 10278 | } |
| 10279 | x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self); |
| 10280 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10281 | if (x == -1) |
| 10282 | x = -2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10283 | _PyUnicode_HASH(self) = x; |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 10284 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10285 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10286 | #undef HASH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10287 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10288 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10289 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10290 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10291 | 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] | 10292 | |
| 10293 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10294 | unicode_index(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10295 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10296 | Py_ssize_t result; |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10297 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 10298 | Py_ssize_t start; |
| 10299 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10300 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 10301 | if (!stringlib_parse_args_finds_unicode("index", args, &substring, |
| 10302 | &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10303 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10304 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10305 | if (PyUnicode_READY(self) == -1) |
| 10306 | return NULL; |
| 10307 | if (PyUnicode_READY(substring) == -1) |
| 10308 | return NULL; |
| 10309 | |
| 10310 | result = any_find_slice( |
| 10311 | ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice, |
| 10312 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10313 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10314 | |
| 10315 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10316 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10317 | if (result == -2) |
| 10318 | return NULL; |
| 10319 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10320 | if (result < 0) { |
| 10321 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 10322 | return NULL; |
| 10323 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10324 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10325 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10326 | } |
| 10327 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10328 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10329 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10330 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10331 | 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] | 10332 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10333 | |
| 10334 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10335 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10336 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10337 | Py_ssize_t i, length; |
| 10338 | int kind; |
| 10339 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10340 | int cased; |
| 10341 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10342 | if (PyUnicode_READY(self) == -1) |
| 10343 | return NULL; |
| 10344 | length = PyUnicode_GET_LENGTH(self); |
| 10345 | kind = PyUnicode_KIND(self); |
| 10346 | data = PyUnicode_DATA(self); |
| 10347 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10348 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10349 | if (length == 1) |
| 10350 | return PyBool_FromLong( |
| 10351 | Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10352 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10353 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10354 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10355 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10356 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10357 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10358 | for (i = 0; i < length; i++) { |
| 10359 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10360 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10361 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10362 | return PyBool_FromLong(0); |
| 10363 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 10364 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10365 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10366 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10367 | } |
| 10368 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10369 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10370 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10371 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10372 | 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] | 10373 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10374 | |
| 10375 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10376 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10377 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10378 | Py_ssize_t i, length; |
| 10379 | int kind; |
| 10380 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10381 | int cased; |
| 10382 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10383 | if (PyUnicode_READY(self) == -1) |
| 10384 | return NULL; |
| 10385 | length = PyUnicode_GET_LENGTH(self); |
| 10386 | kind = PyUnicode_KIND(self); |
| 10387 | data = PyUnicode_DATA(self); |
| 10388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10389 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10390 | if (length == 1) |
| 10391 | return PyBool_FromLong( |
| 10392 | Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10393 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10394 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10395 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10396 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10397 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10398 | cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10399 | for (i = 0; i < length; i++) { |
| 10400 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10401 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10402 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 10403 | return PyBool_FromLong(0); |
| 10404 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 10405 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10406 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10407 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10408 | } |
| 10409 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10410 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10411 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10412 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10413 | Return True if S is a titlecased string and there is at least one\n\ |
| 10414 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 10415 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 10416 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10417 | |
| 10418 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10419 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10420 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10421 | Py_ssize_t i, length; |
| 10422 | int kind; |
| 10423 | void *data; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10424 | int cased, previous_is_cased; |
| 10425 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10426 | if (PyUnicode_READY(self) == -1) |
| 10427 | return NULL; |
| 10428 | length = PyUnicode_GET_LENGTH(self); |
| 10429 | kind = PyUnicode_KIND(self); |
| 10430 | data = PyUnicode_DATA(self); |
| 10431 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10432 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10433 | if (length == 1) { |
| 10434 | Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10435 | return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) || |
| 10436 | (Py_UNICODE_ISUPPER(ch) != 0)); |
| 10437 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10438 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10439 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10440 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10441 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10442 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10443 | cased = 0; |
| 10444 | previous_is_cased = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10445 | for (i = 0; i < length; i++) { |
| 10446 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 10447 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10448 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 10449 | if (previous_is_cased) |
| 10450 | return PyBool_FromLong(0); |
| 10451 | previous_is_cased = 1; |
| 10452 | cased = 1; |
| 10453 | } |
| 10454 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 10455 | if (!previous_is_cased) |
| 10456 | return PyBool_FromLong(0); |
| 10457 | previous_is_cased = 1; |
| 10458 | cased = 1; |
| 10459 | } |
| 10460 | else |
| 10461 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10462 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10463 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10464 | } |
| 10465 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10466 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10467 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10468 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10469 | Return True if all characters in S are whitespace\n\ |
| 10470 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10471 | |
| 10472 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10473 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10474 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10475 | Py_ssize_t i, length; |
| 10476 | int kind; |
| 10477 | void *data; |
| 10478 | |
| 10479 | if (PyUnicode_READY(self) == -1) |
| 10480 | return NULL; |
| 10481 | length = PyUnicode_GET_LENGTH(self); |
| 10482 | kind = PyUnicode_KIND(self); |
| 10483 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10484 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10485 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10486 | if (length == 1) |
| 10487 | return PyBool_FromLong( |
| 10488 | Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10489 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10490 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10491 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10492 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10493 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10494 | for (i = 0; i < length; i++) { |
| 10495 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10496 | if (!Py_UNICODE_ISSPACE(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10497 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10498 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10499 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10500 | } |
| 10501 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10502 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10503 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10504 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10505 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10506 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10507 | |
| 10508 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10509 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10510 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10511 | Py_ssize_t i, length; |
| 10512 | int kind; |
| 10513 | void *data; |
| 10514 | |
| 10515 | if (PyUnicode_READY(self) == -1) |
| 10516 | return NULL; |
| 10517 | length = PyUnicode_GET_LENGTH(self); |
| 10518 | kind = PyUnicode_KIND(self); |
| 10519 | data = PyUnicode_DATA(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10520 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10521 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10522 | if (length == 1) |
| 10523 | return PyBool_FromLong( |
| 10524 | Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0))); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10525 | |
| 10526 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10527 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10528 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10529 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10530 | for (i = 0; i < length; i++) { |
| 10531 | if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10532 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10533 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10534 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10535 | } |
| 10536 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10537 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10538 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10539 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10540 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10541 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10542 | |
| 10543 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10544 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10545 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10546 | int kind; |
| 10547 | void *data; |
| 10548 | Py_ssize_t len, i; |
| 10549 | |
| 10550 | if (PyUnicode_READY(self) == -1) |
| 10551 | return NULL; |
| 10552 | |
| 10553 | kind = PyUnicode_KIND(self); |
| 10554 | data = PyUnicode_DATA(self); |
| 10555 | len = PyUnicode_GET_LENGTH(self); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10556 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10557 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10558 | if (len == 1) { |
| 10559 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10560 | return PyBool_FromLong(Py_UNICODE_ISALNUM(ch)); |
| 10561 | } |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10562 | |
| 10563 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10564 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10565 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10566 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10567 | for (i = 0; i < len; i++) { |
| 10568 | const Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10569 | if (!Py_UNICODE_ISALNUM(ch)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10570 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10571 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10572 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 10573 | } |
| 10574 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10575 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10576 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10577 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10578 | 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] | 10579 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10580 | |
| 10581 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10582 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10583 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10584 | Py_ssize_t i, length; |
| 10585 | int kind; |
| 10586 | void *data; |
| 10587 | |
| 10588 | if (PyUnicode_READY(self) == -1) |
| 10589 | return NULL; |
| 10590 | length = PyUnicode_GET_LENGTH(self); |
| 10591 | kind = PyUnicode_KIND(self); |
| 10592 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10593 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10594 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10595 | if (length == 1) |
| 10596 | return PyBool_FromLong( |
| 10597 | Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10598 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10599 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10600 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10601 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10602 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10603 | for (i = 0; i < length; i++) { |
| 10604 | if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10605 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10606 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10607 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10608 | } |
| 10609 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10610 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10611 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10612 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 10613 | Return True if all characters in S are digits\n\ |
| 10614 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10615 | |
| 10616 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10617 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10618 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10619 | Py_ssize_t i, length; |
| 10620 | int kind; |
| 10621 | void *data; |
| 10622 | |
| 10623 | if (PyUnicode_READY(self) == -1) |
| 10624 | return NULL; |
| 10625 | length = PyUnicode_GET_LENGTH(self); |
| 10626 | kind = PyUnicode_KIND(self); |
| 10627 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10629 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10630 | if (length == 1) { |
| 10631 | const Py_UCS4 ch = PyUnicode_READ(kind, data, 0); |
| 10632 | return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch)); |
| 10633 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10634 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10635 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10636 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10637 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10638 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10639 | for (i = 0; i < length; i++) { |
| 10640 | if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10641 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10642 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10643 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10644 | } |
| 10645 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10646 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10647 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10648 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10649 | 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] | 10650 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10651 | |
| 10652 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10653 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10654 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10655 | Py_ssize_t i, length; |
| 10656 | int kind; |
| 10657 | void *data; |
| 10658 | |
| 10659 | if (PyUnicode_READY(self) == -1) |
| 10660 | return NULL; |
| 10661 | length = PyUnicode_GET_LENGTH(self); |
| 10662 | kind = PyUnicode_KIND(self); |
| 10663 | data = PyUnicode_DATA(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10664 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10665 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10666 | if (length == 1) |
| 10667 | return PyBool_FromLong( |
| 10668 | Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10669 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10670 | /* Special case for empty strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10671 | if (length == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10672 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 10673 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10674 | for (i = 0; i < length; i++) { |
| 10675 | if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10676 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10677 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 10678 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10679 | } |
| 10680 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10681 | int |
| 10682 | PyUnicode_IsIdentifier(PyObject *self) |
| 10683 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10684 | int kind; |
| 10685 | void *data; |
| 10686 | Py_ssize_t i; |
Ezio Melotti | 93e7afc | 2011-08-22 14:08:38 +0300 | [diff] [blame] | 10687 | Py_UCS4 first; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10688 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10689 | if (PyUnicode_READY(self) == -1) { |
| 10690 | Py_FatalError("identifier not ready"); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10691 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10692 | } |
| 10693 | |
| 10694 | /* Special case for empty strings */ |
| 10695 | if (PyUnicode_GET_LENGTH(self) == 0) |
| 10696 | return 0; |
| 10697 | kind = PyUnicode_KIND(self); |
| 10698 | data = PyUnicode_DATA(self); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10699 | |
| 10700 | /* PEP 3131 says that the first character must be in |
| 10701 | XID_Start and subsequent characters in XID_Continue, |
| 10702 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10703 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10704 | letters, digits, underscore). However, given the current |
| 10705 | definition of XID_Start and XID_Continue, it is sufficient |
| 10706 | to check just for these, except that _ must be allowed |
| 10707 | as starting an identifier. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10708 | first = PyUnicode_READ(kind, data, 0); |
Benjamin Peterson | f413b80 | 2011-08-12 22:17:18 -0500 | [diff] [blame] | 10709 | if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10710 | return 0; |
| 10711 | |
Benjamin Peterson | 9c6e6a0 | 2011-09-28 08:09:05 -0400 | [diff] [blame] | 10712 | for (i = 1; i < PyUnicode_GET_LENGTH(self); i++) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10713 | if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i))) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10714 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10715 | return 1; |
| 10716 | } |
| 10717 | |
| 10718 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10719 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 10720 | \n\ |
| 10721 | Return True if S is a valid identifier according\n\ |
| 10722 | to the language definition."); |
| 10723 | |
| 10724 | static PyObject* |
| 10725 | unicode_isidentifier(PyObject *self) |
| 10726 | { |
| 10727 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 10728 | } |
| 10729 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10730 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10731 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10732 | \n\ |
| 10733 | Return True if all characters in S are considered\n\ |
| 10734 | printable in repr() or S is empty, False otherwise."); |
| 10735 | |
| 10736 | static PyObject* |
| 10737 | unicode_isprintable(PyObject *self) |
| 10738 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10739 | Py_ssize_t i, length; |
| 10740 | int kind; |
| 10741 | void *data; |
| 10742 | |
| 10743 | if (PyUnicode_READY(self) == -1) |
| 10744 | return NULL; |
| 10745 | length = PyUnicode_GET_LENGTH(self); |
| 10746 | kind = PyUnicode_KIND(self); |
| 10747 | data = PyUnicode_DATA(self); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10748 | |
| 10749 | /* Shortcut for single character strings */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10750 | if (length == 1) |
| 10751 | return PyBool_FromLong( |
| 10752 | Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0))); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10753 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10754 | for (i = 0; i < length; i++) { |
| 10755 | if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 10756 | Py_RETURN_FALSE; |
| 10757 | } |
| 10758 | } |
| 10759 | Py_RETURN_TRUE; |
| 10760 | } |
| 10761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10762 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 10763 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10764 | \n\ |
| 10765 | 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] | 10766 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10767 | |
| 10768 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10769 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10770 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10771 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10772 | } |
| 10773 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 10774 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10775 | unicode_length(PyUnicodeObject *self) |
| 10776 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10777 | if (PyUnicode_READY(self) == -1) |
| 10778 | return -1; |
| 10779 | return PyUnicode_GET_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10780 | } |
| 10781 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10782 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10783 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10784 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 10785 | 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] | 10786 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10787 | |
| 10788 | static PyObject * |
| 10789 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 10790 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10791 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10792 | Py_UCS4 fillchar = ' '; |
| 10793 | |
| 10794 | if (PyUnicode_READY(self) == -1) |
| 10795 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 10796 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10797 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10798 | return NULL; |
| 10799 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10800 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10801 | Py_INCREF(self); |
| 10802 | return (PyObject*) self; |
| 10803 | } |
| 10804 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10805 | return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10806 | } |
| 10807 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10808 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10809 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10810 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10811 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10812 | |
| 10813 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 10814 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10815 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10816 | return fixup(self, fixlower); |
| 10817 | } |
| 10818 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10819 | #define LEFTSTRIP 0 |
| 10820 | #define RIGHTSTRIP 1 |
| 10821 | #define BOTHSTRIP 2 |
| 10822 | |
| 10823 | /* Arrays indexed by above */ |
| 10824 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 10825 | |
| 10826 | #define STRIPNAME(i) (stripformat[i]+3) |
| 10827 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10828 | /* externally visible for str.strip(unicode) */ |
| 10829 | PyObject * |
| 10830 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 10831 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10832 | void *data; |
| 10833 | int kind; |
| 10834 | Py_ssize_t i, j, len; |
| 10835 | BLOOM_MASK sepmask; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10836 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10837 | if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1) |
| 10838 | return NULL; |
| 10839 | |
| 10840 | kind = PyUnicode_KIND(self); |
| 10841 | data = PyUnicode_DATA(self); |
| 10842 | len = PyUnicode_GET_LENGTH(self); |
| 10843 | sepmask = make_bloom_mask(PyUnicode_KIND(sepobj), |
| 10844 | PyUnicode_DATA(sepobj), |
| 10845 | PyUnicode_GET_LENGTH(sepobj)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10846 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10847 | i = 0; |
| 10848 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10849 | while (i < len && |
| 10850 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10851 | i++; |
| 10852 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10853 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10854 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10855 | j = len; |
| 10856 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10857 | do { |
| 10858 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10859 | } while (j >= i && |
| 10860 | BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10861 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10862 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10863 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10864 | return PyUnicode_Substring((PyObject*)self, i, j); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10865 | } |
| 10866 | |
| 10867 | PyObject* |
| 10868 | PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end) |
| 10869 | { |
| 10870 | unsigned char *data; |
| 10871 | int kind; |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10872 | Py_ssize_t length; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10873 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10874 | if (PyUnicode_READY(self) == -1) |
| 10875 | return NULL; |
| 10876 | |
| 10877 | end = Py_MIN(end, PyUnicode_GET_LENGTH(self)); |
| 10878 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10879 | if (start == 0 && end == PyUnicode_GET_LENGTH(self)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10880 | { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10881 | if (PyUnicode_CheckExact(self)) { |
| 10882 | Py_INCREF(self); |
| 10883 | return self; |
| 10884 | } |
| 10885 | else |
| 10886 | return PyUnicode_Copy(self); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10887 | } |
| 10888 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10889 | length = end - start; |
| 10890 | if (length == 1) |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 10891 | return unicode_getitem(self, start); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10892 | |
Victor Stinner | de636f3 | 2011-10-01 03:55:54 +0200 | [diff] [blame] | 10893 | if (start < 0 || end < 0) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10894 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 10895 | return NULL; |
| 10896 | } |
| 10897 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10898 | kind = PyUnicode_KIND(self); |
| 10899 | data = PyUnicode_1BYTE_DATA(self); |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 10900 | return PyUnicode_FromKindAndData(kind, |
| 10901 | data + PyUnicode_KIND_SIZE(kind, start), |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10902 | length); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10903 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10904 | |
| 10905 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10906 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10907 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10908 | int kind; |
| 10909 | void *data; |
| 10910 | Py_ssize_t len, i, j; |
| 10911 | |
| 10912 | if (PyUnicode_READY(self) == -1) |
| 10913 | return NULL; |
| 10914 | |
| 10915 | kind = PyUnicode_KIND(self); |
| 10916 | data = PyUnicode_DATA(self); |
| 10917 | len = PyUnicode_GET_LENGTH(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10918 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10919 | i = 0; |
| 10920 | if (striptype != RIGHTSTRIP) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10921 | while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10922 | i++; |
| 10923 | } |
| 10924 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10925 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10926 | j = len; |
| 10927 | if (striptype != LEFTSTRIP) { |
| 10928 | do { |
| 10929 | j--; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 10930 | } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j))); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10931 | j++; |
| 10932 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10933 | |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 10934 | return PyUnicode_Substring((PyObject*)self, i, j); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 10935 | } |
| 10936 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10937 | |
| 10938 | static PyObject * |
| 10939 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 10940 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10941 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10942 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10943 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 10944 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10945 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10946 | if (sep != NULL && sep != Py_None) { |
| 10947 | if (PyUnicode_Check(sep)) |
| 10948 | return _PyUnicode_XStrip(self, striptype, sep); |
| 10949 | else { |
| 10950 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10951 | "%s arg must be None or str", |
| 10952 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10953 | return NULL; |
| 10954 | } |
| 10955 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10956 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10957 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10958 | } |
| 10959 | |
| 10960 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10961 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10962 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10963 | \n\ |
| 10964 | Return a copy of the string S with leading and trailing\n\ |
| 10965 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10966 | 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] | 10967 | |
| 10968 | static PyObject * |
| 10969 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 10970 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10971 | if (PyTuple_GET_SIZE(args) == 0) |
| 10972 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 10973 | else |
| 10974 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10975 | } |
| 10976 | |
| 10977 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10978 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10979 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10980 | \n\ |
| 10981 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10982 | 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] | 10983 | |
| 10984 | static PyObject * |
| 10985 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 10986 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 10987 | if (PyTuple_GET_SIZE(args) == 0) |
| 10988 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 10989 | else |
| 10990 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10991 | } |
| 10992 | |
| 10993 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 10994 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10995 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 10996 | \n\ |
| 10997 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 10998 | 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] | 10999 | |
| 11000 | static PyObject * |
| 11001 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 11002 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11003 | if (PyTuple_GET_SIZE(args) == 0) |
| 11004 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 11005 | else |
| 11006 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 11007 | } |
| 11008 | |
| 11009 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11010 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11011 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11012 | { |
| 11013 | PyUnicodeObject *u; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11014 | Py_ssize_t nchars, n; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11015 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11016 | if (len < 1) { |
| 11017 | Py_INCREF(unicode_empty); |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 11018 | return unicode_empty; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 11019 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11020 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 11021 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11022 | /* no repeat, return original string */ |
| 11023 | Py_INCREF(str); |
| 11024 | return (PyObject*) str; |
| 11025 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 11026 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11027 | if (PyUnicode_READY(str) == -1) |
| 11028 | return NULL; |
| 11029 | |
Victor Stinner | c759f3e | 2011-10-01 03:09:58 +0200 | [diff] [blame] | 11030 | if (PyUnicode_GET_LENGTH(str) > PY_SSIZE_T_MAX / len) { |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11031 | PyErr_SetString(PyExc_OverflowError, |
| 11032 | "repeated string is too long"); |
| 11033 | return NULL; |
| 11034 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11035 | nchars = len * PyUnicode_GET_LENGTH(str); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11036 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11037 | u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11038 | if (!u) |
| 11039 | return NULL; |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11040 | assert(PyUnicode_KIND(u) == PyUnicode_KIND(str)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11041 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11042 | if (PyUnicode_GET_LENGTH(str) == 1) { |
| 11043 | const int kind = PyUnicode_KIND(str); |
| 11044 | const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0); |
| 11045 | void *to = PyUnicode_DATA(u); |
Victor Stinner | 67ca64c | 2011-10-01 02:47:29 +0200 | [diff] [blame] | 11046 | if (kind == PyUnicode_1BYTE_KIND) |
| 11047 | memset(to, (unsigned char)fill_char, len); |
| 11048 | else { |
| 11049 | for (n = 0; n < len; ++n) |
| 11050 | PyUnicode_WRITE(kind, to, n, fill_char); |
| 11051 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11052 | } |
| 11053 | else { |
| 11054 | /* number of characters copied this far */ |
| 11055 | Py_ssize_t done = PyUnicode_GET_LENGTH(str); |
| 11056 | const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str); |
| 11057 | char *to = (char *) PyUnicode_DATA(u); |
| 11058 | Py_MEMCPY(to, PyUnicode_DATA(str), |
| 11059 | PyUnicode_GET_LENGTH(str) * char_size); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11060 | while (done < nchars) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11061 | n = (done <= nchars-done) ? done : nchars-done; |
| 11062 | Py_MEMCPY(to + (done * char_size), to, n * char_size); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11063 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11064 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11065 | } |
| 11066 | |
| 11067 | return (PyObject*) u; |
| 11068 | } |
| 11069 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11070 | PyObject * |
| 11071 | PyUnicode_Replace(PyObject *obj, |
| 11072 | PyObject *subobj, |
| 11073 | PyObject *replobj, |
| 11074 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11075 | { |
| 11076 | PyObject *self; |
| 11077 | PyObject *str1; |
| 11078 | PyObject *str2; |
| 11079 | PyObject *result; |
| 11080 | |
| 11081 | self = PyUnicode_FromObject(obj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11082 | if (self == NULL || PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11083 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11084 | str1 = PyUnicode_FromObject(subobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11085 | if (str1 == NULL || PyUnicode_READY(str1) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11086 | Py_DECREF(self); |
| 11087 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11088 | } |
| 11089 | str2 = PyUnicode_FromObject(replobj); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11090 | if (str2 == NULL || PyUnicode_READY(str2)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11091 | Py_DECREF(self); |
| 11092 | Py_DECREF(str1); |
| 11093 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11094 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11095 | result = replace(self, str1, str2, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11096 | Py_DECREF(self); |
| 11097 | Py_DECREF(str1); |
| 11098 | Py_DECREF(str2); |
| 11099 | return result; |
| 11100 | } |
| 11101 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11102 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | c1897e7 | 2010-06-26 18:50:39 +0000 | [diff] [blame] | 11103 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11104 | \n\ |
| 11105 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 11106 | old replaced by new. If the optional argument count is\n\ |
| 11107 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11108 | |
| 11109 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11110 | unicode_replace(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11111 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11112 | PyObject *str1; |
| 11113 | PyObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11114 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11115 | PyObject *result; |
| 11116 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11117 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11118 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11119 | if (!PyUnicode_READY(self) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11120 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11121 | str1 = PyUnicode_FromObject(str1); |
| 11122 | if (str1 == NULL || PyUnicode_READY(str1) == -1) |
| 11123 | return NULL; |
| 11124 | str2 = PyUnicode_FromObject(str2); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11125 | if (str2 == NULL || PyUnicode_READY(str2) == -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11126 | Py_DECREF(str1); |
| 11127 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 11128 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11129 | |
| 11130 | result = replace(self, str1, str2, maxcount); |
| 11131 | |
| 11132 | Py_DECREF(str1); |
| 11133 | Py_DECREF(str2); |
| 11134 | return result; |
| 11135 | } |
| 11136 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11137 | static PyObject * |
| 11138 | unicode_repr(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11139 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11140 | PyObject *repr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11141 | Py_ssize_t isize; |
| 11142 | Py_ssize_t osize, squote, dquote, i, o; |
| 11143 | Py_UCS4 max, quote; |
| 11144 | int ikind, okind; |
| 11145 | void *idata, *odata; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11146 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11147 | if (PyUnicode_READY(unicode) == -1) |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11148 | return NULL; |
| 11149 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11150 | isize = PyUnicode_GET_LENGTH(unicode); |
| 11151 | idata = PyUnicode_DATA(unicode); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11152 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11153 | /* Compute length of output, quote characters, and |
| 11154 | maximum character */ |
| 11155 | osize = 2; /* quotes */ |
| 11156 | max = 127; |
| 11157 | squote = dquote = 0; |
| 11158 | ikind = PyUnicode_KIND(unicode); |
| 11159 | for (i = 0; i < isize; i++) { |
| 11160 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
| 11161 | switch (ch) { |
| 11162 | case '\'': squote++; osize++; break; |
| 11163 | case '"': dquote++; osize++; break; |
| 11164 | case '\\': case '\t': case '\r': case '\n': |
| 11165 | osize += 2; break; |
| 11166 | default: |
| 11167 | /* Fast-path ASCII */ |
| 11168 | if (ch < ' ' || ch == 0x7f) |
| 11169 | osize += 4; /* \xHH */ |
| 11170 | else if (ch < 0x7f) |
| 11171 | osize++; |
| 11172 | else if (Py_UNICODE_ISPRINTABLE(ch)) { |
| 11173 | osize++; |
| 11174 | max = ch > max ? ch : max; |
| 11175 | } |
| 11176 | else if (ch < 0x100) |
| 11177 | osize += 4; /* \xHH */ |
| 11178 | else if (ch < 0x10000) |
| 11179 | osize += 6; /* \uHHHH */ |
| 11180 | else |
| 11181 | osize += 10; /* \uHHHHHHHH */ |
| 11182 | } |
| 11183 | } |
| 11184 | |
| 11185 | quote = '\''; |
| 11186 | if (squote) { |
| 11187 | if (dquote) |
| 11188 | /* Both squote and dquote present. Use squote, |
| 11189 | and escape them */ |
| 11190 | osize += squote; |
| 11191 | else |
| 11192 | quote = '"'; |
| 11193 | } |
| 11194 | |
| 11195 | repr = PyUnicode_New(osize, max); |
| 11196 | if (repr == NULL) |
| 11197 | return NULL; |
| 11198 | okind = PyUnicode_KIND(repr); |
| 11199 | odata = PyUnicode_DATA(repr); |
| 11200 | |
| 11201 | PyUnicode_WRITE(okind, odata, 0, quote); |
| 11202 | PyUnicode_WRITE(okind, odata, osize-1, quote); |
| 11203 | |
| 11204 | for (i = 0, o = 1; i < isize; i++) { |
| 11205 | Py_UCS4 ch = PyUnicode_READ(ikind, idata, i); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11206 | |
| 11207 | /* Escape quotes and backslashes */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11208 | if ((ch == quote) || (ch == '\\')) { |
| 11209 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11210 | PyUnicode_WRITE(okind, odata, o++, ch); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11211 | continue; |
| 11212 | } |
| 11213 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11214 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11215 | if (ch == '\t') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11216 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11217 | PyUnicode_WRITE(okind, odata, o++, 't'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11218 | } |
| 11219 | else if (ch == '\n') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11220 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11221 | PyUnicode_WRITE(okind, odata, o++, 'n'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11222 | } |
| 11223 | else if (ch == '\r') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11224 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11225 | PyUnicode_WRITE(okind, odata, o++, 'r'); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11226 | } |
| 11227 | |
| 11228 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11229 | else if (ch < ' ' || ch == 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11230 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11231 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11232 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11233 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11234 | } |
| 11235 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11236 | /* Copy ASCII characters as-is */ |
| 11237 | else if (ch < 0x7F) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11238 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11239 | } |
| 11240 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11241 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11242 | else { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11243 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11244 | (categories Z* and C* except ASCII space) |
| 11245 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11246 | if (!Py_UNICODE_ISPRINTABLE(ch)) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11247 | /* Map 8-bit characters to '\xhh' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11248 | if (ch <= 0xff) { |
| 11249 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11250 | PyUnicode_WRITE(okind, odata, o++, 'x'); |
| 11251 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]); |
| 11252 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11253 | } |
| 11254 | /* Map 21-bit characters to '\U00xxxxxx' */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11255 | else if (ch >= 0x10000) { |
| 11256 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11257 | PyUnicode_WRITE(okind, odata, o++, 'U'); |
| 11258 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]); |
| 11259 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]); |
| 11260 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]); |
| 11261 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]); |
| 11262 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11263 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11264 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11265 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11266 | } |
| 11267 | /* Map 16-bit characters to '\uxxxx' */ |
| 11268 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11269 | PyUnicode_WRITE(okind, odata, o++, '\\'); |
| 11270 | PyUnicode_WRITE(okind, odata, o++, 'u'); |
| 11271 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]); |
| 11272 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]); |
| 11273 | PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]); |
| 11274 | PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11275 | } |
| 11276 | } |
| 11277 | /* Copy characters as-is */ |
| 11278 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11279 | PyUnicode_WRITE(okind, odata, o++, ch); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 11280 | } |
| 11281 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11282 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11283 | /* Closing quote already added at the beginning */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 11284 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11285 | } |
| 11286 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11287 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11288 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11289 | \n\ |
| 11290 | Return the highest index in S where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 11291 | such that sub is contained within S[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11292 | arguments start and end are interpreted as in slice notation.\n\ |
| 11293 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11294 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11295 | |
| 11296 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11297 | unicode_rfind(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11298 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11299 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11300 | Py_ssize_t start; |
| 11301 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11302 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11303 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11304 | if (!stringlib_parse_args_finds_unicode("rfind", args, &substring, |
| 11305 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11306 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11307 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11308 | if (PyUnicode_READY(self) == -1) |
| 11309 | return NULL; |
| 11310 | if (PyUnicode_READY(substring) == -1) |
| 11311 | return NULL; |
| 11312 | |
| 11313 | result = any_find_slice( |
| 11314 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 11315 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11316 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11317 | |
| 11318 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11319 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11320 | if (result == -2) |
| 11321 | return NULL; |
| 11322 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11323 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11324 | } |
| 11325 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11326 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11327 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11328 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11329 | 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] | 11330 | |
| 11331 | static PyObject * |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11332 | unicode_rindex(PyObject *self, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11333 | { |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11334 | PyUnicodeObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 11335 | Py_ssize_t start; |
| 11336 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11337 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11338 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11339 | if (!stringlib_parse_args_finds_unicode("rindex", args, &substring, |
| 11340 | &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11341 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11342 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11343 | if (PyUnicode_READY(self) == -1) |
| 11344 | return NULL; |
| 11345 | if (PyUnicode_READY(substring) == -1) |
| 11346 | return NULL; |
| 11347 | |
| 11348 | result = any_find_slice( |
| 11349 | ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice, |
| 11350 | self, (PyObject*)substring, start, end |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11351 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11352 | |
| 11353 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11354 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11355 | if (result == -2) |
| 11356 | return NULL; |
| 11357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11358 | if (result < 0) { |
| 11359 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 11360 | return NULL; |
| 11361 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11362 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11363 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11364 | } |
| 11365 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11366 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11367 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11368 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11369 | 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] | 11370 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11371 | |
| 11372 | static PyObject * |
| 11373 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 11374 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11375 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11376 | Py_UCS4 fillchar = ' '; |
| 11377 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11378 | 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] | 11379 | return NULL; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 11380 | |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11381 | if (PyUnicode_READY(self) == -1) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11382 | return NULL; |
| 11383 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11384 | if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11385 | Py_INCREF(self); |
| 11386 | return (PyObject*) self; |
| 11387 | } |
| 11388 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11389 | return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11390 | } |
| 11391 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11392 | PyObject * |
| 11393 | PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11394 | { |
| 11395 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 11396 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11397 | s = PyUnicode_FromObject(s); |
| 11398 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11399 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11400 | if (sep != NULL) { |
| 11401 | sep = PyUnicode_FromObject(sep); |
| 11402 | if (sep == NULL) { |
| 11403 | Py_DECREF(s); |
| 11404 | return NULL; |
| 11405 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11406 | } |
| 11407 | |
| 11408 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11409 | |
| 11410 | Py_DECREF(s); |
| 11411 | Py_XDECREF(sep); |
| 11412 | return result; |
| 11413 | } |
| 11414 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11415 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11416 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11417 | \n\ |
| 11418 | Return a list of the words in S, using sep as the\n\ |
| 11419 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 11420 | 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] | 11421 | whitespace string is a separator and empty strings are\n\ |
| 11422 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11423 | |
| 11424 | static PyObject* |
| 11425 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 11426 | { |
| 11427 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11428 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11429 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11430 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11431 | return NULL; |
| 11432 | |
| 11433 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11434 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11435 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11436 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11437 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11438 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11439 | } |
| 11440 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11441 | PyObject * |
| 11442 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 11443 | { |
| 11444 | PyObject* str_obj; |
| 11445 | PyObject* sep_obj; |
| 11446 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11447 | int kind1, kind2, kind; |
| 11448 | void *buf1 = NULL, *buf2 = NULL; |
| 11449 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11450 | |
| 11451 | str_obj = PyUnicode_FromObject(str_in); |
Victor Stinner | e9a2935 | 2011-10-01 02:14:59 +0200 | [diff] [blame] | 11452 | if (!str_obj || PyUnicode_READY(str_obj) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11453 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11454 | sep_obj = PyUnicode_FromObject(sep_in); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11455 | if (!sep_obj || PyUnicode_READY(sep_obj) == -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11456 | Py_DECREF(str_obj); |
| 11457 | return NULL; |
| 11458 | } |
| 11459 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11460 | kind1 = PyUnicode_KIND(str_in); |
| 11461 | kind2 = PyUnicode_KIND(sep_obj); |
| 11462 | kind = kind1 > kind2 ? kind1 : kind2; |
| 11463 | buf1 = PyUnicode_DATA(str_in); |
| 11464 | if (kind1 != kind) |
| 11465 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11466 | if (!buf1) |
| 11467 | goto onError; |
| 11468 | buf2 = PyUnicode_DATA(sep_obj); |
| 11469 | if (kind2 != kind) |
| 11470 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11471 | if (!buf2) |
| 11472 | goto onError; |
| 11473 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11474 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11475 | |
| 11476 | switch(PyUnicode_KIND(str_in)) { |
| 11477 | case PyUnicode_1BYTE_KIND: |
| 11478 | out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11479 | break; |
| 11480 | case PyUnicode_2BYTE_KIND: |
| 11481 | out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11482 | break; |
| 11483 | case PyUnicode_4BYTE_KIND: |
| 11484 | out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11485 | break; |
| 11486 | default: |
| 11487 | assert(0); |
| 11488 | out = 0; |
| 11489 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11490 | |
| 11491 | Py_DECREF(sep_obj); |
| 11492 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11493 | if (kind1 != kind) |
| 11494 | PyMem_Free(buf1); |
| 11495 | if (kind2 != kind) |
| 11496 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11497 | |
| 11498 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11499 | onError: |
| 11500 | Py_DECREF(sep_obj); |
| 11501 | Py_DECREF(str_obj); |
| 11502 | if (kind1 != kind && buf1) |
| 11503 | PyMem_Free(buf1); |
| 11504 | if (kind2 != kind && buf2) |
| 11505 | PyMem_Free(buf2); |
| 11506 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11507 | } |
| 11508 | |
| 11509 | |
| 11510 | PyObject * |
| 11511 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 11512 | { |
| 11513 | PyObject* str_obj; |
| 11514 | PyObject* sep_obj; |
| 11515 | PyObject* out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11516 | int kind1, kind2, kind; |
| 11517 | void *buf1 = NULL, *buf2 = NULL; |
| 11518 | Py_ssize_t len1, len2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11519 | |
| 11520 | str_obj = PyUnicode_FromObject(str_in); |
| 11521 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11522 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11523 | sep_obj = PyUnicode_FromObject(sep_in); |
| 11524 | if (!sep_obj) { |
| 11525 | Py_DECREF(str_obj); |
| 11526 | return NULL; |
| 11527 | } |
| 11528 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11529 | kind1 = PyUnicode_KIND(str_in); |
| 11530 | kind2 = PyUnicode_KIND(sep_obj); |
Georg Brandl | 4cb0de2 | 2011-09-28 21:49:49 +0200 | [diff] [blame] | 11531 | kind = Py_MAX(kind1, kind2); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11532 | buf1 = PyUnicode_DATA(str_in); |
| 11533 | if (kind1 != kind) |
| 11534 | buf1 = _PyUnicode_AsKind(str_in, kind); |
| 11535 | if (!buf1) |
| 11536 | goto onError; |
| 11537 | buf2 = PyUnicode_DATA(sep_obj); |
| 11538 | if (kind2 != kind) |
| 11539 | buf2 = _PyUnicode_AsKind(sep_obj, kind); |
| 11540 | if (!buf2) |
| 11541 | goto onError; |
| 11542 | len1 = PyUnicode_GET_LENGTH(str_obj); |
| 11543 | len2 = PyUnicode_GET_LENGTH(sep_obj); |
| 11544 | |
| 11545 | switch(PyUnicode_KIND(str_in)) { |
| 11546 | case PyUnicode_1BYTE_KIND: |
| 11547 | out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11548 | break; |
| 11549 | case PyUnicode_2BYTE_KIND: |
| 11550 | out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11551 | break; |
| 11552 | case PyUnicode_4BYTE_KIND: |
| 11553 | out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2); |
| 11554 | break; |
| 11555 | default: |
| 11556 | assert(0); |
| 11557 | out = 0; |
| 11558 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11559 | |
| 11560 | Py_DECREF(sep_obj); |
| 11561 | Py_DECREF(str_obj); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11562 | if (kind1 != kind) |
| 11563 | PyMem_Free(buf1); |
| 11564 | if (kind2 != kind) |
| 11565 | PyMem_Free(buf2); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11566 | |
| 11567 | return out; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11568 | onError: |
| 11569 | Py_DECREF(sep_obj); |
| 11570 | Py_DECREF(str_obj); |
| 11571 | if (kind1 != kind && buf1) |
| 11572 | PyMem_Free(buf1); |
| 11573 | if (kind2 != kind && buf2) |
| 11574 | PyMem_Free(buf2); |
| 11575 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11576 | } |
| 11577 | |
| 11578 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11579 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11580 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11581 | 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] | 11582 | 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] | 11583 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11584 | |
| 11585 | static PyObject* |
| 11586 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 11587 | { |
| 11588 | return PyUnicode_Partition((PyObject *)self, separator); |
| 11589 | } |
| 11590 | |
| 11591 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 11592 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11593 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 11594 | 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] | 11595 | 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] | 11596 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11597 | |
| 11598 | static PyObject* |
| 11599 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 11600 | { |
| 11601 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 11602 | } |
| 11603 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 11604 | PyObject * |
| 11605 | PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11606 | { |
| 11607 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11608 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11609 | s = PyUnicode_FromObject(s); |
| 11610 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11611 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11612 | if (sep != NULL) { |
| 11613 | sep = PyUnicode_FromObject(sep); |
| 11614 | if (sep == NULL) { |
| 11615 | Py_DECREF(s); |
| 11616 | return NULL; |
| 11617 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11618 | } |
| 11619 | |
| 11620 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 11621 | |
| 11622 | Py_DECREF(s); |
| 11623 | Py_XDECREF(sep); |
| 11624 | return result; |
| 11625 | } |
| 11626 | |
| 11627 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11628 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11629 | \n\ |
| 11630 | Return a list of the words in S, using sep as the\n\ |
| 11631 | delimiter string, starting at the end of the string and\n\ |
| 11632 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 11633 | splits are done. If sep is not specified, any whitespace string\n\ |
| 11634 | is a separator."); |
| 11635 | |
| 11636 | static PyObject* |
| 11637 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 11638 | { |
| 11639 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11640 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11641 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11642 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11643 | return NULL; |
| 11644 | |
| 11645 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11646 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11647 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11648 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11649 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11650 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 11651 | } |
| 11652 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11653 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11654 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11655 | \n\ |
| 11656 | 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] | 11657 | 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] | 11658 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11659 | |
| 11660 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11661 | unicode_splitlines(PyUnicodeObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11662 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11663 | static char *kwlist[] = {"keepends", 0}; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11664 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11665 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 11666 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 11667 | kwlist, &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11668 | return NULL; |
| 11669 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 11670 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11671 | } |
| 11672 | |
| 11673 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 11674 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11675 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 11676 | if (PyUnicode_CheckExact(self)) { |
| 11677 | Py_INCREF(self); |
| 11678 | return self; |
| 11679 | } else |
| 11680 | /* Subtype -- return genuine unicode string with the same value. */ |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 11681 | return PyUnicode_Copy(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11682 | } |
| 11683 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11684 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11685 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11686 | \n\ |
| 11687 | 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] | 11688 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11689 | |
| 11690 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11691 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11692 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11693 | return fixup(self, fixswapcase); |
| 11694 | } |
| 11695 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11696 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11697 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11698 | \n\ |
| 11699 | Return a translation table usable for str.translate().\n\ |
| 11700 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 11701 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11702 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11703 | If there are two arguments, they must be strings of equal length, and\n\ |
| 11704 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 11705 | character at the same position in y. If there is a third argument, it\n\ |
| 11706 | must be a string, whose characters will be mapped to None in the result."); |
| 11707 | |
| 11708 | static PyObject* |
| 11709 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 11710 | { |
| 11711 | PyObject *x, *y = NULL, *z = NULL; |
| 11712 | PyObject *new = NULL, *key, *value; |
| 11713 | Py_ssize_t i = 0; |
| 11714 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 11715 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11716 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 11717 | return NULL; |
| 11718 | new = PyDict_New(); |
| 11719 | if (!new) |
| 11720 | return NULL; |
| 11721 | if (y != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11722 | int x_kind, y_kind, z_kind; |
| 11723 | void *x_data, *y_data, *z_data; |
| 11724 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11725 | /* x must be a string too, of equal length */ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11726 | if (!PyUnicode_Check(x)) { |
| 11727 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 11728 | "be a string if there is a second argument"); |
| 11729 | goto err; |
| 11730 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11731 | if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11732 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 11733 | "arguments must have equal length"); |
| 11734 | goto err; |
| 11735 | } |
| 11736 | /* 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] | 11737 | x_kind = PyUnicode_KIND(x); |
| 11738 | y_kind = PyUnicode_KIND(y); |
| 11739 | x_data = PyUnicode_DATA(x); |
| 11740 | y_data = PyUnicode_DATA(y); |
| 11741 | for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) { |
| 11742 | key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i)); |
| 11743 | value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11744 | if (!key || !value) |
| 11745 | goto err; |
| 11746 | res = PyDict_SetItem(new, key, value); |
| 11747 | Py_DECREF(key); |
| 11748 | Py_DECREF(value); |
| 11749 | if (res < 0) |
| 11750 | goto err; |
| 11751 | } |
| 11752 | /* create entries for deleting chars in z */ |
| 11753 | if (z != NULL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11754 | z_kind = PyUnicode_KIND(z); |
| 11755 | z_data = PyUnicode_DATA(z); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11756 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11757 | key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11758 | if (!key) |
| 11759 | goto err; |
| 11760 | res = PyDict_SetItem(new, key, Py_None); |
| 11761 | Py_DECREF(key); |
| 11762 | if (res < 0) |
| 11763 | goto err; |
| 11764 | } |
| 11765 | } |
| 11766 | } else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11767 | int kind; |
| 11768 | void *data; |
| 11769 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11770 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 11771 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11772 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 11773 | "to maketrans it must be a dict"); |
| 11774 | goto err; |
| 11775 | } |
| 11776 | /* copy entries into the new dict, converting string keys to int keys */ |
| 11777 | while (PyDict_Next(x, &i, &key, &value)) { |
| 11778 | if (PyUnicode_Check(key)) { |
| 11779 | /* convert string keys to integer keys */ |
| 11780 | PyObject *newkey; |
| 11781 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 11782 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 11783 | "table must be of length 1"); |
| 11784 | goto err; |
| 11785 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11786 | kind = PyUnicode_KIND(key); |
| 11787 | data = PyUnicode_DATA(key); |
| 11788 | newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0)); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11789 | if (!newkey) |
| 11790 | goto err; |
| 11791 | res = PyDict_SetItem(new, newkey, value); |
| 11792 | Py_DECREF(newkey); |
| 11793 | if (res < 0) |
| 11794 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11795 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 11796 | /* just keep integer keys */ |
| 11797 | if (PyDict_SetItem(new, key, value) < 0) |
| 11798 | goto err; |
| 11799 | } else { |
| 11800 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 11801 | "be strings or integers"); |
| 11802 | goto err; |
| 11803 | } |
| 11804 | } |
| 11805 | } |
| 11806 | return new; |
| 11807 | err: |
| 11808 | Py_DECREF(new); |
| 11809 | return NULL; |
| 11810 | } |
| 11811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11812 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11813 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11814 | \n\ |
| 11815 | Return a copy of the string S, where all characters have been mapped\n\ |
| 11816 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 11817 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 11818 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 11819 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11820 | |
| 11821 | static PyObject* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11822 | unicode_translate(PyObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11823 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11824 | return _PyUnicode_TranslateCharmap(self, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11825 | } |
| 11826 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11827 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11828 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11829 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11830 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11831 | |
| 11832 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 11833 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11834 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11835 | return fixup(self, fixupper); |
| 11836 | } |
| 11837 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11838 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11839 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11840 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 11841 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 11842 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11843 | |
| 11844 | static PyObject * |
| 11845 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 11846 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11847 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11848 | PyUnicodeObject *u; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11849 | Py_ssize_t width; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11850 | int kind; |
| 11851 | void *data; |
| 11852 | Py_UCS4 chr; |
| 11853 | |
| 11854 | if (PyUnicode_READY(self) == -1) |
| 11855 | return NULL; |
| 11856 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11857 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11858 | return NULL; |
| 11859 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11860 | if (PyUnicode_GET_LENGTH(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 11861 | if (PyUnicode_CheckExact(self)) { |
| 11862 | Py_INCREF(self); |
| 11863 | return (PyObject*) self; |
| 11864 | } |
| 11865 | else |
Victor Stinner | 2219e0a | 2011-10-01 01:16:59 +0200 | [diff] [blame] | 11866 | return PyUnicode_Copy((PyObject*)self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11867 | } |
| 11868 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11869 | fill = width - _PyUnicode_LENGTH(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11870 | |
| 11871 | u = pad(self, fill, 0, '0'); |
| 11872 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 11873 | if (u == NULL) |
| 11874 | return NULL; |
| 11875 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11876 | kind = PyUnicode_KIND(u); |
| 11877 | data = PyUnicode_DATA(u); |
| 11878 | chr = PyUnicode_READ(kind, data, fill); |
| 11879 | |
| 11880 | if (chr == '+' || chr == '-') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11881 | /* move sign to beginning of string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11882 | PyUnicode_WRITE(kind, data, 0, chr); |
| 11883 | PyUnicode_WRITE(kind, data, fill, '0'); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11884 | } |
| 11885 | |
| 11886 | return (PyObject*) u; |
| 11887 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11888 | |
| 11889 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11890 | static PyObject * |
| 11891 | unicode__decimal2ascii(PyObject *self) |
| 11892 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11893 | return PyUnicode_TransformDecimalAndSpaceToASCII(self); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 11894 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11895 | #endif |
| 11896 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11897 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11898 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11899 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11900 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 11901 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11902 | With optional end, stop comparing S at that position.\n\ |
| 11903 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11904 | |
| 11905 | static PyObject * |
| 11906 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11907 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11908 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11909 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11910 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11911 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11912 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11913 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11914 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11915 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11916 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11917 | if (PyTuple_Check(subobj)) { |
| 11918 | Py_ssize_t i; |
| 11919 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11920 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11921 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11922 | if (substring == NULL) |
| 11923 | return NULL; |
| 11924 | result = tailmatch(self, substring, start, end, -1); |
| 11925 | Py_DECREF(substring); |
| 11926 | if (result) { |
| 11927 | Py_RETURN_TRUE; |
| 11928 | } |
| 11929 | } |
| 11930 | /* nothing matched */ |
| 11931 | Py_RETURN_FALSE; |
| 11932 | } |
| 11933 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11934 | if (substring == NULL) { |
| 11935 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11936 | PyErr_Format(PyExc_TypeError, "startswith first arg must be str or " |
| 11937 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11938 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11939 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11940 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11941 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11942 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11943 | } |
| 11944 | |
| 11945 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 11946 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11947 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11948 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 11949 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 11950 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11951 | With optional end, stop comparing S at that position.\n\ |
| 11952 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11953 | |
| 11954 | static PyObject * |
| 11955 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11956 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11957 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11958 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11959 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 11960 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11961 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11962 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11963 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 11964 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11965 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11966 | if (PyTuple_Check(subobj)) { |
| 11967 | Py_ssize_t i; |
| 11968 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 11969 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11970 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11971 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11972 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11973 | result = tailmatch(self, substring, start, end, +1); |
| 11974 | Py_DECREF(substring); |
| 11975 | if (result) { |
| 11976 | Py_RETURN_TRUE; |
| 11977 | } |
| 11978 | } |
| 11979 | Py_RETURN_FALSE; |
| 11980 | } |
| 11981 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11982 | if (substring == NULL) { |
| 11983 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 11984 | PyErr_Format(PyExc_TypeError, "endswith first arg must be str or " |
| 11985 | "a tuple of str, not %s", Py_TYPE(subobj)->tp_name); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11986 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 11987 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11988 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11989 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11990 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11991 | } |
| 11992 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 11993 | #include "stringlib/unicode_format.h" |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11994 | |
| 11995 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 11996 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 11997 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 11998 | Return a formatted version of S, using substitutions from args and kwargs.\n\ |
| 11999 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12000 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12001 | PyDoc_STRVAR(format_map__doc__, |
| 12002 | "S.format_map(mapping) -> str\n\ |
| 12003 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12004 | Return a formatted version of S, using substitutions from mapping.\n\ |
| 12005 | The substitutions are identified by braces ('{' and '}')."); |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12006 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12007 | static PyObject * |
| 12008 | unicode__format__(PyObject* self, PyObject* args) |
| 12009 | { |
| 12010 | PyObject *format_spec; |
| 12011 | |
| 12012 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 12013 | return NULL; |
| 12014 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12015 | return _PyUnicode_FormatAdvanced(self, format_spec, 0, |
| 12016 | PyUnicode_GET_LENGTH(format_spec)); |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12017 | } |
| 12018 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12019 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12020 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12021 | \n\ |
Eric Smith | 51d2fd9 | 2010-11-06 19:27:37 +0000 | [diff] [blame] | 12022 | Return a formatted version of S as described by format_spec."); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 12023 | |
| 12024 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12025 | unicode__sizeof__(PyUnicodeObject *v) |
| 12026 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12027 | Py_ssize_t size; |
| 12028 | |
| 12029 | /* If it's a compact object, account for base structure + |
| 12030 | character data. */ |
| 12031 | if (PyUnicode_IS_COMPACT_ASCII(v)) |
| 12032 | size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1; |
| 12033 | else if (PyUnicode_IS_COMPACT(v)) |
| 12034 | size = sizeof(PyCompactUnicodeObject) + |
| 12035 | (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v); |
| 12036 | else { |
| 12037 | /* If it is a two-block object, account for base object, and |
| 12038 | for character block if present. */ |
| 12039 | size = sizeof(PyUnicodeObject); |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12040 | if (_PyUnicode_DATA_ANY(v)) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12041 | size += (PyUnicode_GET_LENGTH(v) + 1) * |
| 12042 | PyUnicode_CHARACTER_SIZE(v); |
| 12043 | } |
| 12044 | /* 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] | 12045 | with the data pointer. Check if the data is not shared. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12046 | if (_PyUnicode_WSTR(v) && |
Victor Stinner | a3be613 | 2011-10-03 02:16:37 +0200 | [diff] [blame] | 12047 | (PyUnicode_DATA(v) != _PyUnicode_WSTR(v))) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12048 | size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t); |
Victor Stinner | 829c0ad | 2011-10-03 01:08:02 +0200 | [diff] [blame] | 12049 | if (_PyUnicode_HAS_UTF8_MEMORY(v)) |
Victor Stinner | e90fe6a | 2011-10-01 16:48:13 +0200 | [diff] [blame] | 12050 | size += PyUnicode_UTF8_LENGTH(v) + 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12051 | |
| 12052 | return PyLong_FromSsize_t(size); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12053 | } |
| 12054 | |
| 12055 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12056 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12057 | |
| 12058 | static PyObject * |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12059 | unicode_getnewargs(PyObject *v) |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12060 | { |
Victor Stinner | 034f6cf | 2011-09-30 02:26:44 +0200 | [diff] [blame] | 12061 | PyObject *copy = PyUnicode_Copy(v); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12062 | if (!copy) |
| 12063 | return NULL; |
| 12064 | return Py_BuildValue("(N)", copy); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 12065 | } |
| 12066 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12067 | static PyMethodDef unicode_methods[] = { |
| 12068 | |
| 12069 | /* Order is according to common usage: often used methods should |
| 12070 | appear first, since lookup is done sequentially. */ |
| 12071 | |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 12072 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12073 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 12074 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 12075 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12076 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 12077 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 12078 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 12079 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 12080 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 12081 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 12082 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12083 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12084 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 12085 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 12086 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12087 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12088 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 12089 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 12090 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12091 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12092 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 12093 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS | METH_KEYWORDS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 12094 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12095 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 12096 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 12097 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 12098 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 12099 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 12100 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 12101 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 12102 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 12103 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 12104 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 12105 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 12106 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 12107 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 12108 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 12109 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 12110 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12111 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 12112 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 12113 | {"format_map", (PyCFunction) do_string_format_map, METH_O, format_map__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 12114 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 12115 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 12116 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 12117 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 12118 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 12119 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12120 | #endif |
| 12121 | |
| 12122 | #if 0 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12123 | /* These methods are just used for debugging the implementation. */ |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 12124 | {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12125 | #endif |
| 12126 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12127 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12128 | {NULL, NULL} |
| 12129 | }; |
| 12130 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12131 | static PyObject * |
| 12132 | unicode_mod(PyObject *v, PyObject *w) |
| 12133 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 12134 | if (!PyUnicode_Check(v)) |
| 12135 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12136 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12137 | } |
| 12138 | |
| 12139 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12140 | 0, /*nb_add*/ |
| 12141 | 0, /*nb_subtract*/ |
| 12142 | 0, /*nb_multiply*/ |
| 12143 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 12144 | }; |
| 12145 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12146 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12147 | (lenfunc) unicode_length, /* sq_length */ |
| 12148 | PyUnicode_Concat, /* sq_concat */ |
| 12149 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 12150 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 12151 | 0, /* sq_slice */ |
| 12152 | 0, /* sq_ass_item */ |
| 12153 | 0, /* sq_ass_slice */ |
| 12154 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12155 | }; |
| 12156 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12157 | static PyObject* |
| 12158 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 12159 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12160 | if (PyUnicode_READY(self) == -1) |
| 12161 | return NULL; |
| 12162 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12163 | if (PyIndex_Check(item)) { |
| 12164 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12165 | if (i == -1 && PyErr_Occurred()) |
| 12166 | return NULL; |
| 12167 | if (i < 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12168 | i += PyUnicode_GET_LENGTH(self); |
Victor Stinner | 2fe5ced | 2011-10-02 00:25:40 +0200 | [diff] [blame] | 12169 | return unicode_getitem((PyObject*)self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12170 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12171 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12172 | const Py_UNICODE* source_buf; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12173 | Py_UNICODE* result_buf; |
| 12174 | PyObject* result; |
| 12175 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12176 | if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12177 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12178 | return NULL; |
| 12179 | } |
| 12180 | |
| 12181 | if (slicelength <= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12182 | return PyUnicode_New(0, 0); |
| 12183 | } else if (start == 0 && step == 1 && |
| 12184 | slicelength == PyUnicode_GET_LENGTH(self) && |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12185 | PyUnicode_CheckExact(self)) { |
| 12186 | Py_INCREF(self); |
| 12187 | return (PyObject *)self; |
| 12188 | } else if (step == 1) { |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12189 | return PyUnicode_Substring((PyObject*)self, |
| 12190 | start, start + slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12191 | } else { |
| 12192 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 12193 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 12194 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12195 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12196 | if (result_buf == NULL) |
| 12197 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12198 | |
| 12199 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 12200 | result_buf[i] = source_buf[cur]; |
| 12201 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12202 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12203 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 12204 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12205 | return result; |
| 12206 | } |
| 12207 | } else { |
| 12208 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 12209 | return NULL; |
| 12210 | } |
| 12211 | } |
| 12212 | |
| 12213 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12214 | (lenfunc)unicode_length, /* mp_length */ |
| 12215 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 12216 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 12217 | }; |
| 12218 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12219 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12220 | /* Helpers for PyUnicode_Format() */ |
| 12221 | |
| 12222 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12223 | 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] | 12224 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 12225 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12226 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12227 | (*p_argidx)++; |
| 12228 | if (arglen < 0) |
| 12229 | return args; |
| 12230 | else |
| 12231 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12232 | } |
| 12233 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12234 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12235 | return NULL; |
| 12236 | } |
| 12237 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12238 | /* 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] | 12239 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12240 | static PyObject * |
| 12241 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12242 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12243 | char *p; |
| 12244 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12245 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12247 | x = PyFloat_AsDouble(v); |
| 12248 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12249 | return NULL; |
| 12250 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12251 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12252 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12253 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12254 | p = PyOS_double_to_string(x, type, prec, |
| 12255 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12256 | if (p == NULL) |
| 12257 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12258 | result = PyUnicode_DecodeASCII(p, strlen(p), NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 12259 | PyMem_Free(p); |
| 12260 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12261 | } |
| 12262 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12263 | static PyObject* |
| 12264 | formatlong(PyObject *val, int flags, int prec, int type) |
| 12265 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12266 | char *buf; |
| 12267 | int len; |
| 12268 | PyObject *str; /* temporary string object. */ |
| 12269 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12270 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12271 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 12272 | if (!str) |
| 12273 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12274 | result = PyUnicode_DecodeASCII(buf, len, NULL); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12275 | Py_DECREF(str); |
| 12276 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12277 | } |
| 12278 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12279 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12280 | formatchar(Py_UCS4 *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12281 | size_t buflen, |
| 12282 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12283 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12284 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12285 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12286 | if (PyUnicode_GET_LENGTH(v) == 1) { |
| 12287 | buf[0] = PyUnicode_READ_CHAR(v, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12288 | buf[1] = '\0'; |
| 12289 | return 1; |
| 12290 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12291 | goto onError; |
| 12292 | } |
| 12293 | else { |
| 12294 | /* Integer input truncated to a character */ |
| 12295 | long x; |
| 12296 | x = PyLong_AsLong(v); |
| 12297 | if (x == -1 && PyErr_Occurred()) |
| 12298 | goto onError; |
| 12299 | |
| 12300 | if (x < 0 || x > 0x10ffff) { |
| 12301 | PyErr_SetString(PyExc_OverflowError, |
| 12302 | "%c arg not in range(0x110000)"); |
| 12303 | return -1; |
| 12304 | } |
| 12305 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12306 | buf[0] = (Py_UCS4) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12307 | buf[1] = '\0'; |
| 12308 | return 1; |
| 12309 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 12310 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12311 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12312 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12313 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 12314 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12315 | } |
| 12316 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12317 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12318 | 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] | 12319 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12320 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 12321 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 12322 | PyObject * |
| 12323 | PyUnicode_Format(PyObject *format, PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12324 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12325 | void *fmt; |
| 12326 | int fmtkind; |
| 12327 | PyObject *result; |
| 12328 | Py_UCS4 *res, *res0; |
| 12329 | Py_UCS4 max; |
| 12330 | int kind; |
| 12331 | Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12332 | int args_owned = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12333 | PyObject *dict = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12334 | PyUnicodeObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 12335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12336 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12337 | PyErr_BadInternalCall(); |
| 12338 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12339 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12340 | uformat = (PyUnicodeObject*)PyUnicode_FromObject(format); |
| 12341 | if (uformat == NULL || PyUnicode_READY(uformat) == -1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12342 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12343 | fmt = PyUnicode_DATA(uformat); |
| 12344 | fmtkind = PyUnicode_KIND(uformat); |
| 12345 | fmtcnt = PyUnicode_GET_LENGTH(uformat); |
| 12346 | fmtpos = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12347 | |
| 12348 | reslen = rescnt = fmtcnt + 100; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12349 | res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4)); |
| 12350 | if (res0 == NULL) { |
| 12351 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12352 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12353 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12354 | |
| 12355 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12356 | arglen = PyTuple_Size(args); |
| 12357 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12358 | } |
| 12359 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12360 | arglen = -1; |
| 12361 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12362 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 12363 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 12364 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12365 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12366 | |
| 12367 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12368 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12369 | if (--rescnt < 0) { |
| 12370 | rescnt = fmtcnt + 100; |
| 12371 | reslen += rescnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12372 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12373 | if (res0 == NULL){ |
| 12374 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12375 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12376 | } |
| 12377 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12378 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12379 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12380 | *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12381 | } |
| 12382 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12383 | /* Got a format specifier */ |
| 12384 | int flags = 0; |
| 12385 | Py_ssize_t width = -1; |
| 12386 | int prec = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12387 | Py_UCS4 c = '\0'; |
| 12388 | Py_UCS4 fill; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12389 | int isnumok; |
| 12390 | PyObject *v = NULL; |
| 12391 | PyObject *temp = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12392 | void *pbuf; |
| 12393 | Py_ssize_t pindex; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12394 | Py_UNICODE sign; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12395 | Py_ssize_t len, len1; |
| 12396 | Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12397 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12398 | fmtpos++; |
| 12399 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') { |
| 12400 | Py_ssize_t keystart; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12401 | Py_ssize_t keylen; |
| 12402 | PyObject *key; |
| 12403 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 12404 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12405 | if (dict == NULL) { |
| 12406 | PyErr_SetString(PyExc_TypeError, |
| 12407 | "format requires a mapping"); |
| 12408 | goto onError; |
| 12409 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12410 | ++fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12411 | --fmtcnt; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12412 | keystart = fmtpos; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12413 | /* Skip over balanced parentheses */ |
| 12414 | while (pcount > 0 && --fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12415 | if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12416 | --pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12417 | else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12418 | ++pcount; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12419 | fmtpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12420 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12421 | keylen = fmtpos - keystart - 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12422 | if (fmtcnt < 0 || pcount > 0) { |
| 12423 | PyErr_SetString(PyExc_ValueError, |
| 12424 | "incomplete format key"); |
| 12425 | goto onError; |
| 12426 | } |
Victor Stinner | 12bab6d | 2011-10-01 01:53:49 +0200 | [diff] [blame] | 12427 | key = PyUnicode_Substring((PyObject*)uformat, |
| 12428 | keystart, keystart + keylen); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12429 | if (key == NULL) |
| 12430 | goto onError; |
| 12431 | if (args_owned) { |
| 12432 | Py_DECREF(args); |
| 12433 | args_owned = 0; |
| 12434 | } |
| 12435 | args = PyObject_GetItem(dict, key); |
| 12436 | Py_DECREF(key); |
| 12437 | if (args == NULL) { |
| 12438 | goto onError; |
| 12439 | } |
| 12440 | args_owned = 1; |
| 12441 | arglen = -1; |
| 12442 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12443 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12444 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12445 | switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12446 | case '-': flags |= F_LJUST; continue; |
| 12447 | case '+': flags |= F_SIGN; continue; |
| 12448 | case ' ': flags |= F_BLANK; continue; |
| 12449 | case '#': flags |= F_ALT; continue; |
| 12450 | case '0': flags |= F_ZERO; continue; |
| 12451 | } |
| 12452 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12453 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12454 | if (c == '*') { |
| 12455 | v = getnextarg(args, arglen, &argidx); |
| 12456 | if (v == NULL) |
| 12457 | goto onError; |
| 12458 | if (!PyLong_Check(v)) { |
| 12459 | PyErr_SetString(PyExc_TypeError, |
| 12460 | "* wants int"); |
| 12461 | goto onError; |
| 12462 | } |
| 12463 | width = PyLong_AsLong(v); |
| 12464 | if (width == -1 && PyErr_Occurred()) |
| 12465 | goto onError; |
| 12466 | if (width < 0) { |
| 12467 | flags |= F_LJUST; |
| 12468 | width = -width; |
| 12469 | } |
| 12470 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12471 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12472 | } |
| 12473 | else if (c >= '0' && c <= '9') { |
| 12474 | width = c - '0'; |
| 12475 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12476 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12477 | if (c < '0' || c > '9') |
| 12478 | break; |
| 12479 | if ((width*10) / 10 != width) { |
| 12480 | PyErr_SetString(PyExc_ValueError, |
| 12481 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12482 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12483 | } |
| 12484 | width = width*10 + (c - '0'); |
| 12485 | } |
| 12486 | } |
| 12487 | if (c == '.') { |
| 12488 | prec = 0; |
| 12489 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12490 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12491 | if (c == '*') { |
| 12492 | v = getnextarg(args, arglen, &argidx); |
| 12493 | if (v == NULL) |
| 12494 | goto onError; |
| 12495 | if (!PyLong_Check(v)) { |
| 12496 | PyErr_SetString(PyExc_TypeError, |
| 12497 | "* wants int"); |
| 12498 | goto onError; |
| 12499 | } |
| 12500 | prec = PyLong_AsLong(v); |
| 12501 | if (prec == -1 && PyErr_Occurred()) |
| 12502 | goto onError; |
| 12503 | if (prec < 0) |
| 12504 | prec = 0; |
| 12505 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12506 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12507 | } |
| 12508 | else if (c >= '0' && c <= '9') { |
| 12509 | prec = c - '0'; |
| 12510 | while (--fmtcnt >= 0) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12511 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12512 | if (c < '0' || c > '9') |
| 12513 | break; |
| 12514 | if ((prec*10) / 10 != prec) { |
| 12515 | PyErr_SetString(PyExc_ValueError, |
| 12516 | "prec too big"); |
| 12517 | goto onError; |
| 12518 | } |
| 12519 | prec = prec*10 + (c - '0'); |
| 12520 | } |
| 12521 | } |
| 12522 | } /* prec */ |
| 12523 | if (fmtcnt >= 0) { |
| 12524 | if (c == 'h' || c == 'l' || c == 'L') { |
| 12525 | if (--fmtcnt >= 0) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12526 | c = PyUnicode_READ(fmtkind, fmt, fmtpos++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12527 | } |
| 12528 | } |
| 12529 | if (fmtcnt < 0) { |
| 12530 | PyErr_SetString(PyExc_ValueError, |
| 12531 | "incomplete format"); |
| 12532 | goto onError; |
| 12533 | } |
| 12534 | if (c != '%') { |
| 12535 | v = getnextarg(args, arglen, &argidx); |
| 12536 | if (v == NULL) |
| 12537 | goto onError; |
| 12538 | } |
| 12539 | sign = 0; |
| 12540 | fill = ' '; |
| 12541 | switch (c) { |
| 12542 | |
| 12543 | case '%': |
| 12544 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12545 | kind = PyUnicode_4BYTE_KIND; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12546 | /* presume that buffer length is at least 1 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12547 | PyUnicode_WRITE(kind, pbuf, 0, '%'); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12548 | len = 1; |
| 12549 | break; |
| 12550 | |
| 12551 | case 's': |
| 12552 | case 'r': |
| 12553 | case 'a': |
Victor Stinner | 808fc0a | 2010-03-22 12:50:40 +0000 | [diff] [blame] | 12554 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12555 | temp = v; |
| 12556 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12557 | } |
| 12558 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12559 | if (c == 's') |
| 12560 | temp = PyObject_Str(v); |
| 12561 | else if (c == 'r') |
| 12562 | temp = PyObject_Repr(v); |
| 12563 | else |
| 12564 | temp = PyObject_ASCII(v); |
| 12565 | if (temp == NULL) |
| 12566 | goto onError; |
| 12567 | if (PyUnicode_Check(temp)) |
| 12568 | /* nothing to do */; |
| 12569 | else { |
| 12570 | Py_DECREF(temp); |
| 12571 | PyErr_SetString(PyExc_TypeError, |
| 12572 | "%s argument has non-string str()"); |
| 12573 | goto onError; |
| 12574 | } |
| 12575 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12576 | if (PyUnicode_READY(temp) == -1) { |
| 12577 | Py_CLEAR(temp); |
| 12578 | goto onError; |
| 12579 | } |
| 12580 | pbuf = PyUnicode_DATA(temp); |
| 12581 | kind = PyUnicode_KIND(temp); |
| 12582 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12583 | if (prec >= 0 && len > prec) |
| 12584 | len = prec; |
| 12585 | break; |
| 12586 | |
| 12587 | case 'i': |
| 12588 | case 'd': |
| 12589 | case 'u': |
| 12590 | case 'o': |
| 12591 | case 'x': |
| 12592 | case 'X': |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12593 | isnumok = 0; |
| 12594 | if (PyNumber_Check(v)) { |
| 12595 | PyObject *iobj=NULL; |
| 12596 | |
| 12597 | if (PyLong_Check(v)) { |
| 12598 | iobj = v; |
| 12599 | Py_INCREF(iobj); |
| 12600 | } |
| 12601 | else { |
| 12602 | iobj = PyNumber_Long(v); |
| 12603 | } |
| 12604 | if (iobj!=NULL) { |
| 12605 | if (PyLong_Check(iobj)) { |
| 12606 | isnumok = 1; |
Senthil Kumaran | 9ebe08d | 2011-07-03 21:03:16 -0700 | [diff] [blame] | 12607 | temp = formatlong(iobj, flags, prec, (c == 'i'? 'd': c)); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12608 | Py_DECREF(iobj); |
| 12609 | if (!temp) |
| 12610 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12611 | if (PyUnicode_READY(temp) == -1) { |
| 12612 | Py_CLEAR(temp); |
| 12613 | goto onError; |
| 12614 | } |
| 12615 | pbuf = PyUnicode_DATA(temp); |
| 12616 | kind = PyUnicode_KIND(temp); |
| 12617 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12618 | sign = 1; |
| 12619 | } |
| 12620 | else { |
| 12621 | Py_DECREF(iobj); |
| 12622 | } |
| 12623 | } |
| 12624 | } |
| 12625 | if (!isnumok) { |
| 12626 | PyErr_Format(PyExc_TypeError, |
| 12627 | "%%%c format: a number is required, " |
| 12628 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 12629 | goto onError; |
| 12630 | } |
| 12631 | if (flags & F_ZERO) |
| 12632 | fill = '0'; |
| 12633 | break; |
| 12634 | |
| 12635 | case 'e': |
| 12636 | case 'E': |
| 12637 | case 'f': |
| 12638 | case 'F': |
| 12639 | case 'g': |
| 12640 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 12641 | temp = formatfloat(v, flags, prec, c); |
| 12642 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12643 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12644 | if (PyUnicode_READY(temp) == -1) { |
| 12645 | Py_CLEAR(temp); |
| 12646 | goto onError; |
| 12647 | } |
| 12648 | pbuf = PyUnicode_DATA(temp); |
| 12649 | kind = PyUnicode_KIND(temp); |
| 12650 | len = PyUnicode_GET_LENGTH(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12651 | sign = 1; |
| 12652 | if (flags & F_ZERO) |
| 12653 | fill = '0'; |
| 12654 | break; |
| 12655 | |
| 12656 | case 'c': |
| 12657 | pbuf = formatbuf; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12658 | kind = PyUnicode_4BYTE_KIND; |
Victor Stinner | b9dcffb | 2011-09-29 00:39:24 +0200 | [diff] [blame] | 12659 | len = formatchar(pbuf, Py_ARRAY_LENGTH(formatbuf), v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12660 | if (len < 0) |
| 12661 | goto onError; |
| 12662 | break; |
| 12663 | |
| 12664 | default: |
| 12665 | PyErr_Format(PyExc_ValueError, |
| 12666 | "unsupported format character '%c' (0x%x) " |
| 12667 | "at index %zd", |
| 12668 | (31<=c && c<=126) ? (char)c : '?', |
| 12669 | (int)c, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12670 | fmtpos - 1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12671 | goto onError; |
| 12672 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12673 | /* pbuf is initialized here. */ |
| 12674 | pindex = 0; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12675 | if (sign) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12676 | if (PyUnicode_READ(kind, pbuf, pindex) == '-' || |
| 12677 | PyUnicode_READ(kind, pbuf, pindex) == '+') { |
| 12678 | sign = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12679 | len--; |
| 12680 | } |
| 12681 | else if (flags & F_SIGN) |
| 12682 | sign = '+'; |
| 12683 | else if (flags & F_BLANK) |
| 12684 | sign = ' '; |
| 12685 | else |
| 12686 | sign = 0; |
| 12687 | } |
| 12688 | if (width < len) |
| 12689 | width = len; |
| 12690 | if (rescnt - (sign != 0) < width) { |
| 12691 | reslen -= rescnt; |
| 12692 | rescnt = width + fmtcnt + 100; |
| 12693 | reslen += rescnt; |
| 12694 | if (reslen < 0) { |
| 12695 | Py_XDECREF(temp); |
| 12696 | PyErr_NoMemory(); |
| 12697 | goto onError; |
| 12698 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12699 | res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4)); |
| 12700 | if (res0 == 0) { |
| 12701 | PyErr_NoMemory(); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12702 | Py_XDECREF(temp); |
| 12703 | goto onError; |
| 12704 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12705 | res = res0 + reslen - rescnt; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12706 | } |
| 12707 | if (sign) { |
| 12708 | if (fill != ' ') |
| 12709 | *res++ = sign; |
| 12710 | rescnt--; |
| 12711 | if (width > len) |
| 12712 | width--; |
| 12713 | } |
| 12714 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12715 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12716 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12717 | if (fill != ' ') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12718 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12719 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12720 | } |
| 12721 | rescnt -= 2; |
| 12722 | width -= 2; |
| 12723 | if (width < 0) |
| 12724 | width = 0; |
| 12725 | len -= 2; |
| 12726 | } |
| 12727 | if (width > len && !(flags & F_LJUST)) { |
| 12728 | do { |
| 12729 | --rescnt; |
| 12730 | *res++ = fill; |
| 12731 | } while (--width > len); |
| 12732 | } |
| 12733 | if (fill == ' ') { |
| 12734 | if (sign) |
| 12735 | *res++ = sign; |
| 12736 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12737 | assert(PyUnicode_READ(kind, pbuf, pindex) == '0'); |
| 12738 | assert(PyUnicode_READ(kind, pbuf, pindex+1) == c); |
| 12739 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12740 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12741 | } |
| 12742 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12743 | /* Copy all characters, preserving len */ |
| 12744 | len1 = len; |
| 12745 | while (len1--) { |
| 12746 | *res++ = PyUnicode_READ(kind, pbuf, pindex++); |
| 12747 | rescnt--; |
| 12748 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12749 | while (--width >= len) { |
| 12750 | --rescnt; |
| 12751 | *res++ = ' '; |
| 12752 | } |
| 12753 | if (dict && (argidx < arglen) && c != '%') { |
| 12754 | PyErr_SetString(PyExc_TypeError, |
| 12755 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 12756 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12757 | goto onError; |
| 12758 | } |
| 12759 | Py_XDECREF(temp); |
| 12760 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12761 | } /* until end */ |
| 12762 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12763 | PyErr_SetString(PyExc_TypeError, |
| 12764 | "not all arguments converted during string formatting"); |
| 12765 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12766 | } |
| 12767 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12768 | |
| 12769 | for (max=0, res = res0; res < res0+reslen-rescnt; res++) |
| 12770 | if (*res > max) |
| 12771 | max = *res; |
| 12772 | result = PyUnicode_New(reslen - rescnt, max); |
| 12773 | if (!result) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12774 | goto onError; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12775 | kind = PyUnicode_KIND(result); |
| 12776 | for (res = res0; res < res0+reslen-rescnt; res++) |
| 12777 | PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res); |
| 12778 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12779 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12780 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12781 | } |
| 12782 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12783 | return (PyObject *)result; |
| 12784 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12785 | onError: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12786 | PyMem_Free(res0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12787 | Py_DECREF(uformat); |
| 12788 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12789 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12790 | } |
| 12791 | return NULL; |
| 12792 | } |
| 12793 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 12794 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12795 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 12796 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12797 | static PyObject * |
| 12798 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12799 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12800 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12801 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 12802 | char *encoding = NULL; |
| 12803 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12804 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12805 | if (type != &PyUnicode_Type) |
| 12806 | return unicode_subtype_new(type, args, kwds); |
| 12807 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12808 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12809 | return NULL; |
| 12810 | if (x == NULL) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12811 | return (PyObject *)PyUnicode_New(0, 0); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12812 | if (encoding == NULL && errors == NULL) |
| 12813 | return PyObject_Str(x); |
| 12814 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12815 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12816 | } |
| 12817 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12818 | static PyObject * |
| 12819 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 12820 | { |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12821 | PyUnicodeObject *unicode, *self; |
| 12822 | Py_ssize_t length, char_size; |
| 12823 | int share_wstr, share_utf8; |
| 12824 | unsigned int kind; |
| 12825 | void *data; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12826 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12827 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12828 | |
| 12829 | unicode = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 12830 | if (unicode == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12831 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 12832 | assert(_PyUnicode_CHECK(unicode)); |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 12833 | if (_PyUnicode_READY_REPLACE(&unicode)) |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12834 | return NULL; |
| 12835 | |
| 12836 | self = (PyUnicodeObject *) type->tp_alloc(type, 0); |
| 12837 | if (self == NULL) { |
| 12838 | Py_DECREF(unicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12839 | return NULL; |
| 12840 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12841 | kind = PyUnicode_KIND(unicode); |
| 12842 | length = PyUnicode_GET_LENGTH(unicode); |
| 12843 | |
| 12844 | _PyUnicode_LENGTH(self) = length; |
| 12845 | _PyUnicode_HASH(self) = _PyUnicode_HASH(unicode); |
| 12846 | _PyUnicode_STATE(self).interned = 0; |
| 12847 | _PyUnicode_STATE(self).kind = kind; |
| 12848 | _PyUnicode_STATE(self).compact = 0; |
| 12849 | _PyUnicode_STATE(self).ascii = 0; |
| 12850 | _PyUnicode_STATE(self).ready = 1; |
| 12851 | _PyUnicode_WSTR(self) = NULL; |
| 12852 | _PyUnicode_UTF8_LENGTH(self) = 0; |
| 12853 | _PyUnicode_UTF8(self) = NULL; |
| 12854 | _PyUnicode_WSTR_LENGTH(self) = 0; |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12855 | _PyUnicode_DATA_ANY(self) = NULL; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12856 | |
| 12857 | share_utf8 = 0; |
| 12858 | share_wstr = 0; |
| 12859 | if (kind == PyUnicode_1BYTE_KIND) { |
| 12860 | char_size = 1; |
| 12861 | if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128) |
| 12862 | share_utf8 = 1; |
| 12863 | } |
| 12864 | else if (kind == PyUnicode_2BYTE_KIND) { |
| 12865 | char_size = 2; |
| 12866 | if (sizeof(wchar_t) == 2) |
| 12867 | share_wstr = 1; |
| 12868 | } |
| 12869 | else { |
| 12870 | assert(kind == PyUnicode_4BYTE_KIND); |
| 12871 | char_size = 4; |
| 12872 | if (sizeof(wchar_t) == 4) |
| 12873 | share_wstr = 1; |
| 12874 | } |
| 12875 | |
| 12876 | /* Ensure we won't overflow the length. */ |
| 12877 | if (length > (PY_SSIZE_T_MAX / char_size - 1)) { |
| 12878 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12879 | goto onError; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12880 | } |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12881 | data = PyObject_MALLOC((length + 1) * char_size); |
| 12882 | if (data == NULL) { |
| 12883 | PyErr_NoMemory(); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12884 | goto onError; |
| 12885 | } |
| 12886 | |
Victor Stinner | c3c7415 | 2011-10-02 20:39:55 +0200 | [diff] [blame] | 12887 | _PyUnicode_DATA_ANY(self) = data; |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12888 | if (share_utf8) { |
| 12889 | _PyUnicode_UTF8_LENGTH(self) = length; |
| 12890 | _PyUnicode_UTF8(self) = data; |
| 12891 | } |
| 12892 | if (share_wstr) { |
| 12893 | _PyUnicode_WSTR_LENGTH(self) = length; |
| 12894 | _PyUnicode_WSTR(self) = (wchar_t *)data; |
| 12895 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12896 | |
Victor Stinner | 07ac3eb | 2011-10-01 16:16:43 +0200 | [diff] [blame] | 12897 | Py_MEMCPY(data, PyUnicode_DATA(unicode), |
| 12898 | PyUnicode_KIND_SIZE(kind, length + 1)); |
| 12899 | Py_DECREF(unicode); |
| 12900 | return (PyObject *)self; |
| 12901 | |
| 12902 | onError: |
| 12903 | Py_DECREF(unicode); |
| 12904 | Py_DECREF(self); |
| 12905 | return NULL; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 12906 | } |
| 12907 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12908 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12909 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12910 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 12911 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 12912 | encoding defaults to the current default string encoding.\n\ |
| 12913 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 12914 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 12915 | static PyObject *unicode_iter(PyObject *seq); |
| 12916 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12917 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 12918 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12919 | "str", /* tp_name */ |
| 12920 | sizeof(PyUnicodeObject), /* tp_size */ |
| 12921 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12922 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12923 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 12924 | 0, /* tp_print */ |
| 12925 | 0, /* tp_getattr */ |
| 12926 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 12927 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12928 | unicode_repr, /* tp_repr */ |
| 12929 | &unicode_as_number, /* tp_as_number */ |
| 12930 | &unicode_as_sequence, /* tp_as_sequence */ |
| 12931 | &unicode_as_mapping, /* tp_as_mapping */ |
| 12932 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 12933 | 0, /* tp_call*/ |
| 12934 | (reprfunc) unicode_str, /* tp_str */ |
| 12935 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 12936 | 0, /* tp_setattro */ |
| 12937 | 0, /* tp_as_buffer */ |
| 12938 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12939 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 12940 | unicode_doc, /* tp_doc */ |
| 12941 | 0, /* tp_traverse */ |
| 12942 | 0, /* tp_clear */ |
| 12943 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 12944 | 0, /* tp_weaklistoffset */ |
| 12945 | unicode_iter, /* tp_iter */ |
| 12946 | 0, /* tp_iternext */ |
| 12947 | unicode_methods, /* tp_methods */ |
| 12948 | 0, /* tp_members */ |
| 12949 | 0, /* tp_getset */ |
| 12950 | &PyBaseObject_Type, /* tp_base */ |
| 12951 | 0, /* tp_dict */ |
| 12952 | 0, /* tp_descr_get */ |
| 12953 | 0, /* tp_descr_set */ |
| 12954 | 0, /* tp_dictoffset */ |
| 12955 | 0, /* tp_init */ |
| 12956 | 0, /* tp_alloc */ |
| 12957 | unicode_new, /* tp_new */ |
| 12958 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12959 | }; |
| 12960 | |
| 12961 | /* Initialize the Unicode implementation */ |
| 12962 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 12963 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12964 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12965 | int i; |
| 12966 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12967 | /* XXX - move this array to unicodectype.c ? */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12968 | Py_UCS2 linebreak[] = { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12969 | 0x000A, /* LINE FEED */ |
| 12970 | 0x000D, /* CARRIAGE RETURN */ |
| 12971 | 0x001C, /* FILE SEPARATOR */ |
| 12972 | 0x001D, /* GROUP SEPARATOR */ |
| 12973 | 0x001E, /* RECORD SEPARATOR */ |
| 12974 | 0x0085, /* NEXT LINE */ |
| 12975 | 0x2028, /* LINE SEPARATOR */ |
| 12976 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 12977 | }; |
| 12978 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 12979 | /* Init the implementation */ |
Victor Stinner | a464fc1 | 2011-10-02 20:39:30 +0200 | [diff] [blame] | 12980 | unicode_empty = PyUnicode_New(0, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12981 | if (!unicode_empty) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12982 | Py_FatalError("Can't create empty string"); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12983 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 12984 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12985 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 12986 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 12987 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12988 | |
| 12989 | /* initialize the linebreak bloom filter */ |
| 12990 | bloom_linebreak = make_bloom_mask( |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 12991 | PyUnicode_2BYTE_KIND, linebreak, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 12992 | Py_ARRAY_LENGTH(linebreak)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12993 | |
| 12994 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 12995 | } |
| 12996 | |
| 12997 | /* Finalize the Unicode implementation */ |
| 12998 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 12999 | int |
| 13000 | PyUnicode_ClearFreeList(void) |
| 13001 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13002 | return 0; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13003 | } |
| 13004 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13005 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 13006 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13007 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13008 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13009 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 13010 | Py_XDECREF(unicode_empty); |
| 13011 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 13012 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13013 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13014 | if (unicode_latin1[i]) { |
| 13015 | Py_DECREF(unicode_latin1[i]); |
| 13016 | unicode_latin1[i] = NULL; |
| 13017 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 13018 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 13019 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 13020 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 13021 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13022 | void |
| 13023 | PyUnicode_InternInPlace(PyObject **p) |
| 13024 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13025 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 13026 | PyObject *t; |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13027 | #ifdef Py_DEBUG |
| 13028 | assert(s != NULL); |
| 13029 | assert(_PyUnicode_CHECK(s)); |
| 13030 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13031 | if (s == NULL || !PyUnicode_Check(s)) |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13032 | return; |
| 13033 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13034 | /* If it's a subclass, we don't really know what putting |
| 13035 | it in the interned dict might do. */ |
| 13036 | if (!PyUnicode_CheckExact(s)) |
| 13037 | return; |
| 13038 | if (PyUnicode_CHECK_INTERNED(s)) |
| 13039 | return; |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13040 | if (_PyUnicode_READY_REPLACE(p)) { |
Victor Stinner | 4fae54c | 2011-10-03 02:01:52 +0200 | [diff] [blame] | 13041 | assert(0 && "PyUnicode_READY fail in PyUnicode_InternInPlace"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13042 | return; |
| 13043 | } |
Victor Stinner | 1b4f9ce | 2011-10-03 13:28:14 +0200 | [diff] [blame] | 13044 | s = (PyUnicodeObject *)(*p); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13045 | if (interned == NULL) { |
| 13046 | interned = PyDict_New(); |
| 13047 | if (interned == NULL) { |
| 13048 | PyErr_Clear(); /* Don't leave an exception */ |
| 13049 | return; |
| 13050 | } |
| 13051 | } |
| 13052 | /* It might be that the GetItem call fails even |
| 13053 | though the key is present in the dictionary, |
| 13054 | namely when this happens during a stack overflow. */ |
| 13055 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13056 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13057 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13058 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13059 | if (t) { |
| 13060 | Py_INCREF(t); |
| 13061 | Py_DECREF(*p); |
| 13062 | *p = t; |
| 13063 | return; |
| 13064 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13065 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13066 | PyThreadState_GET()->recursion_critical = 1; |
| 13067 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 13068 | PyErr_Clear(); |
| 13069 | PyThreadState_GET()->recursion_critical = 0; |
| 13070 | return; |
| 13071 | } |
| 13072 | PyThreadState_GET()->recursion_critical = 0; |
| 13073 | /* The two references in interned are not counted by refcnt. |
| 13074 | The deallocator will take care of this */ |
| 13075 | Py_REFCNT(s) -= 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13076 | _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13077 | } |
| 13078 | |
| 13079 | void |
| 13080 | PyUnicode_InternImmortal(PyObject **p) |
| 13081 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13082 | PyUnicodeObject *u = (PyUnicodeObject *)*p; |
| 13083 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13084 | PyUnicode_InternInPlace(p); |
| 13085 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13086 | _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13087 | Py_INCREF(*p); |
| 13088 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13089 | } |
| 13090 | |
| 13091 | PyObject * |
| 13092 | PyUnicode_InternFromString(const char *cp) |
| 13093 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13094 | PyObject *s = PyUnicode_FromString(cp); |
| 13095 | if (s == NULL) |
| 13096 | return NULL; |
| 13097 | PyUnicode_InternInPlace(&s); |
| 13098 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13099 | } |
| 13100 | |
Alexander Belopolsky | 4001847 | 2011-02-26 01:02:56 +0000 | [diff] [blame] | 13101 | void |
| 13102 | _Py_ReleaseInternedUnicodeStrings(void) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13103 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13104 | PyObject *keys; |
| 13105 | PyUnicodeObject *s; |
| 13106 | Py_ssize_t i, n; |
| 13107 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13108 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13109 | if (interned == NULL || !PyDict_Check(interned)) |
| 13110 | return; |
| 13111 | keys = PyDict_Keys(interned); |
| 13112 | if (keys == NULL || !PyList_Check(keys)) { |
| 13113 | PyErr_Clear(); |
| 13114 | return; |
| 13115 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13116 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13117 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 13118 | detector, interned unicode strings are not forcibly deallocated; |
| 13119 | rather, we give them their stolen references back, and then clear |
| 13120 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13121 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13122 | n = PyList_GET_SIZE(keys); |
| 13123 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13124 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13125 | for (i = 0; i < n; i++) { |
| 13126 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13127 | if (PyUnicode_READY(s) == -1) |
| 13128 | fprintf(stderr, "could not ready string\n"); |
| 13129 | switch (PyUnicode_CHECK_INTERNED(s)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13130 | case SSTATE_NOT_INTERNED: |
| 13131 | /* XXX Shouldn't happen */ |
| 13132 | break; |
| 13133 | case SSTATE_INTERNED_IMMORTAL: |
| 13134 | Py_REFCNT(s) += 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13135 | immortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13136 | break; |
| 13137 | case SSTATE_INTERNED_MORTAL: |
| 13138 | Py_REFCNT(s) += 2; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13139 | mortal_size += PyUnicode_GET_LENGTH(s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13140 | break; |
| 13141 | default: |
| 13142 | Py_FatalError("Inconsistent interned string state."); |
| 13143 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13144 | _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13145 | } |
| 13146 | fprintf(stderr, "total size of all interned strings: " |
| 13147 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 13148 | "mortal/immortal\n", mortal_size, immortal_size); |
| 13149 | Py_DECREF(keys); |
| 13150 | PyDict_Clear(interned); |
| 13151 | Py_DECREF(interned); |
| 13152 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 13153 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13154 | |
| 13155 | |
| 13156 | /********************* Unicode Iterator **************************/ |
| 13157 | |
| 13158 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13159 | PyObject_HEAD |
| 13160 | Py_ssize_t it_index; |
| 13161 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13162 | } unicodeiterobject; |
| 13163 | |
| 13164 | static void |
| 13165 | unicodeiter_dealloc(unicodeiterobject *it) |
| 13166 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13167 | _PyObject_GC_UNTRACK(it); |
| 13168 | Py_XDECREF(it->it_seq); |
| 13169 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13170 | } |
| 13171 | |
| 13172 | static int |
| 13173 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 13174 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13175 | Py_VISIT(it->it_seq); |
| 13176 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13177 | } |
| 13178 | |
| 13179 | static PyObject * |
| 13180 | unicodeiter_next(unicodeiterobject *it) |
| 13181 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13182 | PyUnicodeObject *seq; |
| 13183 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13184 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13185 | assert(it != NULL); |
| 13186 | seq = it->it_seq; |
| 13187 | if (seq == NULL) |
| 13188 | return NULL; |
Victor Stinner | 910337b | 2011-10-03 03:20:16 +0200 | [diff] [blame] | 13189 | assert(_PyUnicode_CHECK(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13190 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13191 | if (it->it_index < PyUnicode_GET_LENGTH(seq)) { |
| 13192 | int kind = PyUnicode_KIND(seq); |
| 13193 | void *data = PyUnicode_DATA(seq); |
| 13194 | Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index); |
| 13195 | item = PyUnicode_FromOrdinal(chr); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13196 | if (item != NULL) |
| 13197 | ++it->it_index; |
| 13198 | return item; |
| 13199 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13200 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13201 | Py_DECREF(seq); |
| 13202 | it->it_seq = NULL; |
| 13203 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13204 | } |
| 13205 | |
| 13206 | static PyObject * |
| 13207 | unicodeiter_len(unicodeiterobject *it) |
| 13208 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13209 | Py_ssize_t len = 0; |
| 13210 | if (it->it_seq) |
| 13211 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 13212 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13213 | } |
| 13214 | |
| 13215 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 13216 | |
| 13217 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13218 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 13219 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13220 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13221 | }; |
| 13222 | |
| 13223 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13224 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 13225 | "str_iterator", /* tp_name */ |
| 13226 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 13227 | 0, /* tp_itemsize */ |
| 13228 | /* methods */ |
| 13229 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 13230 | 0, /* tp_print */ |
| 13231 | 0, /* tp_getattr */ |
| 13232 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 13233 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13234 | 0, /* tp_repr */ |
| 13235 | 0, /* tp_as_number */ |
| 13236 | 0, /* tp_as_sequence */ |
| 13237 | 0, /* tp_as_mapping */ |
| 13238 | 0, /* tp_hash */ |
| 13239 | 0, /* tp_call */ |
| 13240 | 0, /* tp_str */ |
| 13241 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 13242 | 0, /* tp_setattro */ |
| 13243 | 0, /* tp_as_buffer */ |
| 13244 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 13245 | 0, /* tp_doc */ |
| 13246 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 13247 | 0, /* tp_clear */ |
| 13248 | 0, /* tp_richcompare */ |
| 13249 | 0, /* tp_weaklistoffset */ |
| 13250 | PyObject_SelfIter, /* tp_iter */ |
| 13251 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 13252 | unicodeiter_methods, /* tp_methods */ |
| 13253 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13254 | }; |
| 13255 | |
| 13256 | static PyObject * |
| 13257 | unicode_iter(PyObject *seq) |
| 13258 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13259 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13260 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13261 | if (!PyUnicode_Check(seq)) { |
| 13262 | PyErr_BadInternalCall(); |
| 13263 | return NULL; |
| 13264 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13265 | if (PyUnicode_READY(seq) == -1) |
| 13266 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 13267 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 13268 | if (it == NULL) |
| 13269 | return NULL; |
| 13270 | it->it_index = 0; |
| 13271 | Py_INCREF(seq); |
| 13272 | it->it_seq = (PyUnicodeObject *)seq; |
| 13273 | _PyObject_GC_TRACK(it); |
| 13274 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 13275 | } |
| 13276 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13277 | #define UNIOP(x) Py_UNICODE_##x |
| 13278 | #define UNIOP_t Py_UNICODE |
| 13279 | #include "uniops.h" |
| 13280 | #undef UNIOP |
| 13281 | #undef UNIOP_t |
| 13282 | #define UNIOP(x) Py_UCS4_##x |
| 13283 | #define UNIOP_t Py_UCS4 |
| 13284 | #include "uniops.h" |
| 13285 | #undef UNIOP |
| 13286 | #undef UNIOP_t |
Victor Stinner | 331ea92 | 2010-08-10 16:37:20 +0000 | [diff] [blame] | 13287 | |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13288 | Py_UNICODE* |
Victor Stinner | 4640860 | 2010-09-03 16:18:00 +0000 | [diff] [blame] | 13289 | PyUnicode_AsUnicodeCopy(PyObject *object) |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13290 | { |
| 13291 | PyUnicodeObject *unicode = (PyUnicodeObject *)object; |
| 13292 | Py_UNICODE *copy; |
| 13293 | Py_ssize_t size; |
| 13294 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 13295 | if (!PyUnicode_Check(unicode)) { |
| 13296 | PyErr_BadArgument(); |
| 13297 | return NULL; |
| 13298 | } |
Victor Stinner | 71133ff | 2010-09-01 23:43:53 +0000 | [diff] [blame] | 13299 | /* Ensure we won't overflow the size. */ |
| 13300 | if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 13301 | PyErr_NoMemory(); |
| 13302 | return NULL; |
| 13303 | } |
| 13304 | size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ |
| 13305 | size *= sizeof(Py_UNICODE); |
| 13306 | copy = PyMem_Malloc(size); |
| 13307 | if (copy == NULL) { |
| 13308 | PyErr_NoMemory(); |
| 13309 | return NULL; |
| 13310 | } |
| 13311 | memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); |
| 13312 | return copy; |
| 13313 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 13314 | |
Georg Brandl | 66c221e | 2010-10-14 07:04:07 +0000 | [diff] [blame] | 13315 | /* A _string module, to export formatter_parser and formatter_field_name_split |
| 13316 | to the string.Formatter class implemented in Python. */ |
| 13317 | |
| 13318 | static PyMethodDef _string_methods[] = { |
| 13319 | {"formatter_field_name_split", (PyCFunction) formatter_field_name_split, |
| 13320 | METH_O, PyDoc_STR("split the argument as a field name")}, |
| 13321 | {"formatter_parser", (PyCFunction) formatter_parser, |
| 13322 | METH_O, PyDoc_STR("parse the argument as a format string")}, |
| 13323 | {NULL, NULL} |
| 13324 | }; |
| 13325 | |
| 13326 | static struct PyModuleDef _string_module = { |
| 13327 | PyModuleDef_HEAD_INIT, |
| 13328 | "_string", |
| 13329 | PyDoc_STR("string helper module"), |
| 13330 | 0, |
| 13331 | _string_methods, |
| 13332 | NULL, |
| 13333 | NULL, |
| 13334 | NULL, |
| 13335 | NULL |
| 13336 | }; |
| 13337 | |
| 13338 | PyMODINIT_FUNC |
| 13339 | PyInit__string(void) |
| 13340 | { |
| 13341 | return PyModule_Create(&_string_module); |
| 13342 | } |
| 13343 | |
| 13344 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13345 | #ifdef __cplusplus |
| 13346 | } |
| 13347 | #endif |